* 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)
| 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. |
no test coverage detected