registry_sprite_sprite_gradient.js
/**
* @module SpriteGradient
* @fileOverview Contains SpriteGradient class.
*/
/**
* @class
* Gradient for sprites.
*/
class SpriteGradient {
/**
* Create a new SpriteGradient.
* @param {{}|{number: Color}} values Initial values of gradient
* @constructor
*/
constructor(values = {}) {
const keys = Object.keys(values).map(Number).sort((a, b) => a - b);
this._points = [];
this._sprites = [];
for (const key of keys) {
this._points.push(key);
this._sprites.push($$.reg.sprites.get(values[key.toString()]));
}
}
/**
* Set a Color at a point
* @param {number} point Point to set the color at (0-1)
* @param {Color} color Color to set at the point
*/
set(point, color) {
this._points.push(point);
this._sprites.push(color);
}
/**
* Get the color on the gradient at a specified point.
* @param {number} point Point along the gradient (0-1)
* @returns {Sprite} Resulting color at the specified point along the gradient
*/
get(point) {
if (this._points.length === 0)
return null;
if (this._points.length === 1)
return this._sprites[0];
if (point < this._points[0])
return this._sprites[0];
if (point > this._points[this._points.length - 1])
return this._sprites[this._sprites.length - 1];
let last = 0;
for (let i = 1; i < this._points.length; i++) {
if (point < this._points[i])
return this._sprites[last];
last = i;
}
}
}
export default SpriteGradient;