r3-legacy/src/r3-color.js

133 lines
2.3 KiB
JavaScript

/**
* R3.Color
* @param apiComponent
* @constructor
*/
R3.Color = function(
apiComponent
) {
__DEREGISTER_COMPONENT__;
__RUNTIME_COMPONENT__;
__UPGRADE_TO_RUNTIME__;
};
R3.Color.prototype = Object.create(R3.Component.prototype);
R3.Color.prototype.constructor = R3.Color;
/**
* Creates an instance color
* @returns {*}
*/
R3.Color.prototype.createInstance = function() {
this.instance = this.graphics.Color(
this.r,
this.g,
this.b,
this.a
);
__CREATE_INSTANCE__;
};
/**
* Updates the instance color, calls updateInstance on the parent object
*/
R3.Color.prototype.updateInstance = function(property) {
if (property === 'r') {
this.instance.r = this.r;
return;
}
if (property === 'g') {
this.instance.g = this.g;
return;
}
if (property === 'b') {
this.instance.b = this.b;
return;
}
if (property === 'a') {
this.instance.a = this.a;
return;
}
__UPDATE_INSTANCE__;
};
/**
* Converts the current color to HTML hex format (ex. #ffffff)
* @returns {string}
*/
R3.Color.prototype.toHex = function() {
if (this.r < 0) {
this.r = 0;
}
if (this.g < 0) {
this.g = 0;
}
if (this.b < 0) {
this.b = 0;
}
if (this.r > 1) {
this.r = 1;
}
if (this.g > 1) {
this.g = 1;
}
if (this.b > 1) {
this.b = 1;
}
var rf = Math.floor(this.r >= 1? 255 : this.r * 256.0).toString(16);
var gf = Math.floor(this.g >= 1? 255 : this.g * 256.0).toString(16);
var bf = Math.floor(this.b >= 1? 255 : this.b * 256.0).toString(16);
if (rf.length < 2) {
rf = '0' + rf;
}
if (gf.length < 2) {
gf = '0' + gf;
}
if (bf.length < 2) {
bf = '0' + bf;
}
return '#' + rf + gf + bf;
};
/**
* Sets this object color to what the hex value is
* @param hex
* @returns {string}
*/
R3.Color.prototype.fromHex = function(hex) {
var matches = hex.match(new RegExp('#+(..)(..)(..)'));
this.r = parseInt(matches[1], 16) / 255.0;
this.g = parseInt(matches[2], 16) / 255.0;
this.b = parseInt(matches[3], 16) / 255.0;
this.instance.r = this.r;
this.instance.g = this.g;
this.instance.b = this.b;
};