80 lines
1.6 KiB
JavaScript
80 lines
1.6 KiB
JavaScript
|
/**
|
||
|
* Runtime color for updating instance objects
|
||
|
* @param graphics GameLib.D3.Graphics
|
||
|
* @param parentObject GameLib.D3.*
|
||
|
* @param apiColor GameLib.API.Color
|
||
|
* @param grain Number
|
||
|
* @constructor
|
||
|
*/
|
||
|
GameLib.Color = function (graphics, parentObject, apiColor, grain) {
|
||
|
|
||
|
this.graphics = graphics;
|
||
|
this.graphics.isNotThreeThrow();
|
||
|
|
||
|
GameLib.API.Color.call(
|
||
|
this,
|
||
|
apiColor.r,
|
||
|
apiColor.g,
|
||
|
apiColor.b,
|
||
|
apiColor.a
|
||
|
);
|
||
|
|
||
|
this.parentObject = parentObject;
|
||
|
|
||
|
if (GameLib.Utils.UndefinedOrNull(grain)) {
|
||
|
grain = 0.001;
|
||
|
}
|
||
|
this.grain = grain;
|
||
|
|
||
|
this.instance = this.createInstance();
|
||
|
};
|
||
|
|
||
|
GameLib.Color.prototype = Object.create(GameLib.API.Color.prototype);
|
||
|
GameLib.Color.prototype.constructor = GameLib.Color;
|
||
|
|
||
|
/**
|
||
|
* Creates an instance color
|
||
|
* @param update
|
||
|
* @returns {*}
|
||
|
*/
|
||
|
GameLib.Color.prototype.createInstance = function(update) {
|
||
|
|
||
|
var instance = null;
|
||
|
|
||
|
if (update) {
|
||
|
instance = this.instance;
|
||
|
instance.r = this.r;
|
||
|
instance.g = this.g;
|
||
|
instance.b = this.b;
|
||
|
} else {
|
||
|
instance = new THREE.Color(this.r, this.g, this.b);
|
||
|
}
|
||
|
|
||
|
return instance;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Updates the instance color, calls updateInstance on the parent object
|
||
|
*/
|
||
|
GameLib.Color.prototype.updateInstance = function() {
|
||
|
|
||
|
this.createInstance(true);
|
||
|
|
||
|
if (this.parentObject.updateInstance) {
|
||
|
this.parentObject.updateInstance();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Converts runtime color to API Color
|
||
|
* @returns {GameLib.API.Color}
|
||
|
*/
|
||
|
GameLib.Color.prototype.toApiColor = function() {
|
||
|
return new GameLib.API.Color(
|
||
|
this.r,
|
||
|
this.g,
|
||
|
this.b,
|
||
|
this.a
|
||
|
);
|
||
|
};
|