oxjs/source/Ox/js/Color.js

125 lines
3 KiB
JavaScript
Raw Permalink Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
/*@
Ox.hsl <f> Takes RGB values and returns HSL values
(rgb) <[n]> HSL values
(r, g, b) <[n]> HSL values
rgb <[n]> RGB values
r <n> red
g <n> green
b <n> blue
> Ox.hsl([0, 0, 0])
[0, 0, 0]
> Ox.hsl([255, 255, 255])
[0, 0, 1]
> Ox.hsl(0, 255, 0)
[120, 1, 0.5]
@*/
Ox.hsl = function(rgb) {
2012-05-25 07:35:20 +00:00
var hsl = [0, 0, 0], max, min;
if (arguments.length == 3) {
2013-12-01 13:57:52 +00:00
rgb = Ox.slice(arguments);
}
2012-05-25 07:35:20 +00:00
rgb = Ox.clone(rgb).map(function(value) {
return value / 255;
});
2012-05-25 07:35:20 +00:00
max = Ox.max(rgb);
min = Ox.min(rgb);
hsl[2] = 0.5 * (max + min);
if (max == min) {
hsl[0] = 0;
hsl[1] = 0;
} else {
if (max == rgb[0]) {
hsl[0] = (60 * (rgb[1] - rgb[2]) / (max - min) + 360) % 360;
} else if (max == rgb[1]) {
hsl[0] = 60 * (rgb[2] - rgb[0]) / (max - min) + 120;
} else if (max == rgb[2]) {
hsl[0] = 60 * (rgb[0] - rgb[1]) / (max - min) + 240;
}
if (hsl[2] <= 0.5) {
hsl[1] = (max - min) / (2 * hsl[2]);
} else {
hsl[1] = (max - min) / (2 - 2 * hsl[2]);
}
}
return hsl;
};
/*@
Ox.rgb <f> Takes HSL values and returns RGB values
(hsl) <[n]> RGB values
(h, s, l) <[n]> RGB values
hsl <[n]> HSL values
h <n> hue
s <n> saturation
l <n> lightness
> Ox.rgb([0, 0, 0])
[0, 0, 0]
> Ox.rgb([0, 0, 1])
[255, 255, 255]
> Ox.rgb(120, 1, 0.5)
[0, 255, 0]
@*/
Ox.rgb = function(hsl) {
2012-05-25 07:35:20 +00:00
var rgb = [0, 0, 0], v1, v2, v3;
if (arguments.length == 3) {
2013-12-01 13:57:52 +00:00
hsl = Ox.slice(arguments);
}
hsl = Ox.clone(hsl);
hsl[0] /= 360;
if (hsl[1] == 0) {
rgb = [hsl[2], hsl[2], hsl[2]];
} else {
if (hsl[2] < 0.5) {
v2 = hsl[2] * (1 + hsl[1]);
} else {
v2 = hsl[1] + hsl[2] - (hsl[1] * hsl[2]);
}
v1 = 2 * hsl[2] - v2;
rgb.forEach(function(v, i) {
v3 = hsl[0] + (1 - i) * 1/3;
if (v3 < 0) {
v3++;
} else if (v3 > 1) {
v3--;
}
if (v3 < 1/6) {
rgb[i] = v1 + ((v2 - v1) * 6 * v3);
} else if (v3 < 0.5) {
rgb[i] = v2;
} else if (v3 < 2/3) {
rgb[i] = v1 + ((v2 - v1) * 6 * (2/3 - v3));
} else {
rgb[i] = v1;
}
});
}
2012-05-25 07:35:20 +00:00
return rgb.map(function(value) {
2012-06-24 15:41:27 +00:00
return Math.round(value * 255);
});
2011-10-26 14:52:03 +00:00
};
/*@
Ox.toHex <f> Format RGB array as hex value
2012-05-25 07:35:20 +00:00
> Ox.toHex([192, 128, 64])
'C08040'
2011-10-26 14:52:03 +00:00
@*/
Ox.toHex = function(rgb) {
2012-05-25 07:35:20 +00:00
return rgb.map(function(value) {
return Ox.pad(value.toString(16).toUpperCase(), 'left', 2, '0');
2011-10-26 14:52:03 +00:00
}).join('');
};
/*@
Ox.toRGB <f> Format hex value as RGB array
2012-05-25 07:35:20 +00:00
> Ox.toRGB('C08040')
[192, 128, 64]
2011-10-26 14:52:03 +00:00
@*/
Ox.toRGB = function(hex) {
2012-05-25 07:35:20 +00:00
return Ox.range(3).map(function(index) {
return parseInt(hex.substr(index * 2, 2), 16);
2011-10-26 14:52:03 +00:00
});
};