rename vars; add tests

This commit is contained in:
rolux 2012-05-25 09:35:20 +02:00
parent 26093367ec
commit 42923bedf1

View file

@ -16,15 +16,15 @@ Ox.hsl <f> Takes RGB values and returns HSL values
[120, 1, 0.5] [120, 1, 0.5]
@*/ @*/
Ox.hsl = function(rgb) { Ox.hsl = function(rgb) {
var hsl = [0, 0, 0], max, min;
if (arguments.length == 3) { if (arguments.length == 3) {
rgb = Ox.toArray(arguments); rgb = Ox.toArray(arguments);
} }
rgb = Ox.clone(rgb).map(function(val) { rgb = Ox.clone(rgb).map(function(value) {
return val / 255; return value / 255;
}); });
var max = Ox.max(rgb), max = Ox.max(rgb);
min = Ox.min(rgb), min = Ox.min(rgb);
hsl = [0, 0, 0];
hsl[2] = 0.5 * (max + min); hsl[2] = 0.5 * (max + min);
if (max == min) { if (max == min) {
hsl[0] = 0; hsl[0] = 0;
@ -63,13 +63,12 @@ Ox.rgb <f> Takes HSL values and returns RGB values
@*/ @*/
Ox.rgb = function(hsl) { Ox.rgb = function(hsl) {
var rgb = [0, 0, 0], v1, v2, v3;
if (arguments.length == 3) { if (arguments.length == 3) {
hsl = Ox.toArray(arguments); hsl = Ox.toArray(arguments);
} }
hsl = Ox.clone(hsl); hsl = Ox.clone(hsl);
hsl[0] /= 360; hsl[0] /= 360;
var rgb = [0, 0, 0],
v1, v2, v3;
if (hsl[1] == 0) { if (hsl[1] == 0) {
rgb = [hsl[2], hsl[2], hsl[2]]; rgb = [hsl[2], hsl[2], hsl[2]];
} else { } else {
@ -97,25 +96,29 @@ Ox.rgb = function(hsl) {
} }
}); });
} }
return rgb.map(function(v) { return rgb.map(function(value) {
return v * 255; return value * 255;
}); });
}; };
/*@ /*@
Ox.toHex <f> Format RGB array as hex value Ox.toHex <f> Format RGB array as hex value
> Ox.toHex([192, 128, 64])
'C08040'
@*/ @*/
Ox.toHex = function(rgb) { Ox.toHex = function(rgb) {
return rgb.map(function(val) { return rgb.map(function(value) {
return Ox.pad(val.toString(16).toUpperCase(), 2); return Ox.pad(value.toString(16).toUpperCase(), 2);
}).join(''); }).join('');
}; };
/*@ /*@
Ox.toRGB <f> Format hex value as RGB array Ox.toRGB <f> Format hex value as RGB array
> Ox.toRGB('C08040')
[192, 128, 64]
@*/ @*/
Ox.toRGB = function(hex) { Ox.toRGB = function(hex) {
return Ox.range(3).map(function(i) { return Ox.range(3).map(function(index) {
return parseInt(hex.substr(i * 2, 2), 16); return parseInt(hex.substr(index * 2, 2), 16);
}); });
}; };