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