r3-legacy/src/game-lib-color.js

93 lines
1.9 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,
apiColor,
parentObject,
grain
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiColor)) {
apiColor = {};
}
GameLib.API.Color.call(
this,
apiColor.r,
apiColor.g,
apiColor.b,
apiColor.a
);
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
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 &&
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
);
};