Skip to content

Commit

Permalink
Merge github.com:hapticdata/toxiclibsjs
Browse files Browse the repository at this point in the history
  • Loading branch information
hapticdata committed Jun 28, 2011
2 parents 7492910 + fc6dd7b commit 328e102
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/color/TColor.js
Expand Up @@ -7,7 +7,16 @@
Java Version : http://toxiclibs.org Java Version : http://toxiclibs.org
*/ */



//http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
//TLDR: not as straightforward as i.toString(16)
var dec2hex = function(i) {
var result = "0000";
if (i >= 0 && i <= 15) { result = "000" + i.toString(16); }
else if (i >= 16 && i <= 255) { result = "00" + i.toString(16); }
else if (i >= 256 && i <= 4095) { result = "0" + i.toString(16); }
else if (i >= 4096 && i <= 65535) { result = i.toString(16); }
return result;
}


toxi.color.TColor = function(tcolor){ toxi.color.TColor = function(tcolor){
this.rgb = new Array(3); this.rgb = new Array(3);
Expand Down Expand Up @@ -196,9 +205,9 @@ toxi.color.TColor.prototype = {
equals: function(o) { equals: function(o) {
if (o !== undefined && o instanceof toxi.color.TColor) { if (o !== undefined && o instanceof toxi.color.TColor) {
var c = o; var c = o;
var dr = c.rgb[0] - rgb[0]; var dr = c.rgb[0] - this.rgb[0];
var dg = c.rgb[1] - rgb[1]; var dg = c.rgb[1] - this.rgb[1];
var db = c.rgb[2] - rgb[2]; var db = c.rgb[2] - this.rgb[2];
var da = c.alpha() - this._alpha; var da = c.alpha() - this._alpha;
var d = Math.sqrt(dr * dr + dg * dg + db * db + da * da); var d = Math.sqrt(dr * dr + dg * dg + db * db + da * da);
return d < toxi.color.TColor.EPS; return d < toxi.color.TColor.EPS;
Expand Down Expand Up @@ -338,7 +347,7 @@ toxi.color.TColor.prototype = {
} }


this.hsv[0] = (h % 360) / 360.0; this.hsv[0] = (h % 360) / 360.0;
return this.setHSV(hsv); return this.setHSV(this.hsv);


}, },


Expand Down Expand Up @@ -502,17 +511,11 @@ toxi.color.TColor.prototype = {
}, },


toHex: function() { toHex: function() {
var hexes = "", var hex = dec2hex(this.toARGB());
d; if (hex.length > 6) {
for (var i = 0; i < 3; i++) { hex = hex.substring(2);
d = parseInt(this.rgb[i] * 255).toString(16); }
if (d.length == 1) { return hex;
hexes += "0" + d;
} else {
hexes += d;
}
}
return hexes;
}, },


toHSVAArray: function(hsva) { toHSVAArray: function(hsva) {
Expand Down Expand Up @@ -890,13 +893,13 @@ toxi.color.TColor.rgbToCMYK = function(r, g, b,cmyk) {
* @param b * @param b
* @return hex string * @return hex string
*/ */
toxi.color.TColor.rgbToHex = function(r, g, b) { toxi.color.TColor.rgbToHex = function(r, g, b) {
var hex = (toxi.MathUtils.clip(r, 0, 1) * 0xff).toString(16) + var hex = dec2hex(toxi.MathUtils.clip(r, 0, 1) * 0xff) +
(toxi.MathUtils.clip(g, 0, 1) * 0xff).toString(16) + dec2hex(toxi.MathUtils.clip(g, 0, 1) * 0xff) +
(toxi.MathUtils.clip(b, 0, 1) * 0xff).toString(16); dec2hex(toxi.MathUtils.clip(b, 0, 1) * 0xff);


return hex; return hex;
} }


/** /**
* Converts the RGB values into an HSV array. * Converts the RGB values into an HSV array.
Expand Down
4 changes: 4 additions & 0 deletions src/core/math/MathUtils.js
Expand Up @@ -129,6 +129,10 @@ toxi.MathUtils.clipNormalized = function(a) {


toxi.MathUtils.cos = Math.cos; toxi.MathUtils.cos = Math.cos;


toxi.MathUtils.degrees = function(radians) {
return radians * this.RAD2DEG;
}

/** /**
* Fast cosine approximation. * Fast cosine approximation.
* *
Expand Down

0 comments on commit 328e102

Please sign in to comment.