MCPcopy Create free account
hub / github.com/melonjs/melonJS / getColorAt

Method getColorAt

packages/melonjs/src/video/gradient.js:247–287  ·  view source on GitHub ↗

* Get the interpolated color at a given position along the gradient. * Useful for procedural effects like trails that need per-segment colors. * @param {number} position - position along the gradient (0.0–1.0) * @param {Color} out - output Color object to write into * @returns {Color} the ou

(position, out)

Source from the content-addressed store, hash-verified

245 * gradient.getColorAt(0.5, myColor); // myColor is now purple
246 */
247 getColorAt(position, out) {
248 if (!this._parsedStops) {
249 this._buildParsedStops();
250 }
251
252 const stops = this._parsedStops;
253 const len = stops.length;
254
255 // no stops defined
256 if (len === 0) {
257 return out;
258 }
259
260 // single stop or before first
261 if (len === 1 || position <= stops[0].offset) {
262 return out.copy(stops[0].color);
263 }
264
265 // at or past last stop
266 if (position >= stops[len - 1].offset) {
267 return out.copy(stops[len - 1].color);
268 }
269
270 // find surrounding stops and interpolate in float space
271 for (let i = 0; i < len - 1; i++) {
272 if (position >= stops[i].offset && position <= stops[i + 1].offset) {
273 const range = stops[i + 1].offset - stops[i].offset;
274 const frac = range > 0 ? (position - stops[i].offset) / range : 0;
275 const a = stops[i].color.toArray();
276 const b = stops[i + 1].color.toArray();
277 return out.setFloat(
278 a[0] + (b[0] - a[0]) * frac,
279 a[1] + (b[1] - a[1]) * frac,
280 a[2] + (b[2] - a[2]) * frac,
281 a[3] + (b[3] - a[3]) * frac,
282 );
283 }
284 }
285
286 return out;
287 }
288
289 /**
290 * Build the parsed Color cache from colorStops strings.

Callers 3

drawMethod · 0.80
trail.spec.jsFile · 0.80
gradient.spec.jsFile · 0.80

Calls 4

_buildParsedStopsMethod · 0.95
setFloatMethod · 0.80
copyMethod · 0.45
toArrayMethod · 0.45

Tested by

no test coverage detected