Instantly share code, notes, and snippets.

@nutbread /Color.js Secret
Last active Sep 30, 2015

Embed
What would you like to do?
Color conversion for Javascript
var Color = (function () {
var rgb_to_hsv = function (r, g, b) {
var h = 0,
s = 0,
v = 0,
diff,
mult;
if (r > g) {
if (r > b) {
// r > (g,b)
v = r;
if (b > g) {
s = v - g;
mult = 6;
}
else {
s = v - b;
mult = 0;
}
diff = g - b;
}
else {
// b > r > g
v = b;
s = v - g;
diff = r - g;
mult = 4;
}
}
else {
if (g > b) {
// g > (b,r)
v = g;
s = v - (b > r ? r : b);
diff = b - r;
mult = 2;
}
else {
// b > g > r
v = b;
s = v - r;
diff = r - g;
mult = 4;
}
}
if (v > 0 && s > 0) {
h = ((mult * s + diff) / (6 * s));
s /= v;
}
return [ h , s , v ];
};
var hsv_to_rgb = function (h, s, v) {
// Grey
if (s === 0) {
return [ v , v , v ];
}
h = (h % 1.0) * 6.0;
var i = Math.floor(h),
f = h - i,
p = v * (1 - s),
q = v * (1 - s * f),
t = v * (1 - s * (1 - f));
switch (i) {
case 0:
return [ v , t , p ];
case 1:
return [ q , v , p ];
case 2:
return [ p , v , t ];
case 3:
return [ p , q , v ];
case 4:
return [ t , p , v ];
default: // case 5:
return [ v , p , q ];
}
};
var rgb_to_hsl = function (r, g, b) {
var max = Math.max(r, g, b),
min = Math.min(r, g, b),
diff = max - min,
l = max + min,
h = 0,
s = 0;
if (diff !== 0.0) {
s = diff / (l > 1 ? 2 - l : l);
if (max === r) {
h = (g - b) / diff;
if (g < b) h += 6.0;
}
else if (max === g) {
h = (b - r) / diff + 2.0;
}
else { // if (max === b) {
h = (r - g) / diff + 4.0;
}
h /= 6.0;
}
return [ h , s , l / 2.0 ];
};
var hsl_to_rgb = function (h, s, l) {
// Grey
if (s === 0) {
return [ l , l , l ];
}
h = (h % 1.0) * 6.0;
var i = Math.floor(h),
c = (1 - Math.abs(2 * l - 1)) * s,
x = c * (1 - Math.abs((h % 2) - 1)),
p = l - c / 2;
x += p;
c += p;
switch (i) {
case 0:
return [ c , x , p ];
case 1:
return [ x , c , p ];
case 2:
return [ p , c , x ];
case 3:
return [ p , x , c ];
case 4:
return [ x , p , c ];
default: // case 5:
return [ c , p , x ];
}
};
return {
rgb_to_hsv: rgb_to_hsv,
hsv_to_rgb: hsv_to_rgb,
rgb_to_hsl: rgb_to_hsl,
hsl_to_rgb: hsl_to_rgb,
};
})();
// this.Color = Color; // for node testing
var Color = require("./Color").Color;
var r, g, b, c1, c2, c3;
for (r = 0; r < 256; ++r) {
for (g = 0; g < 256; ++g) {
for (b = 0; b < 256; ++b) {
c1 = [ r / 255.0 , g / 255.0 , b / 255.0 ];
c2 = Color.rgb_to_hsl.apply(null, c1);
c3 = Color.hsl_to_rgb.apply(null, c2);
if (
Math.abs(c1[0] - c3[0]) > 0.001 ||
Math.abs(c1[1] - c3[1]) > 0.001 ||
Math.abs(c1[2] - c3[2]) > 0.001
) {
console.log(r, g, b);
console.log(c1);
console.log(c2);
console.log(c3);
r = g = b = 256;
}
}
}
}
for (r = 0; r < 256; ++r) {
for (g = 0; g < 256; ++g) {
for (b = 0; b < 256; ++b) {
c1 = [ r / 255.0 , g / 255.0 , b / 255.0 ];
c2 = Color.rgb_to_hsv.apply(null, c1);
c3 = Color.hsv_to_rgb.apply(null, c2);
if (
Math.abs(c1[0] - c3[0]) > 0.001 ||
Math.abs(c1[1] - c3[1]) > 0.001 ||
Math.abs(c1[2] - c3[2]) > 0.001
) {
console.log(r, g, b);
console.log(c1);
console.log(c2);
console.log(c3);
r = g = b = 256;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment