Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Lch color space

  • Loading branch information...
commit b0fb024e63c95e43db1f2218d98313a15342a18a 1 parent 73119db
@bluej100 bluej100 authored
View
2  component.json
@@ -1,7 +1,7 @@
{
"name": "color-convert",
"description": "Plain color conversion functions",
- "version": "0.3.2",
+ "version": "0.3.3",
"author": "Heather Arthur <fayearthur@gmail.com>",
"repository": "harthur/color-convert",
"keywords": ["color", "colour", "rgb"],
View
49 conversions.js
@@ -7,6 +7,7 @@ module.exports = {
rgb2keyword: rgb2keyword,
rgb2xyz: rgb2xyz,
rgb2lab: rgb2lab,
+ rgb2lch: rgb2lch,
hsl2rgb: hsl2rgb,
hsl2hsv: hsl2hsv,
@@ -32,9 +33,15 @@ module.exports = {
xyz2rgb: xyz2rgb,
xyz2lab: xyz2lab,
+ xyz2lch: xyz2lch,
lab2xyz: lab2xyz,
lab2rgb: lab2rgb,
+ lab2lch: lab2lch,
+
+ lch2lab: lch2lab,
+ lch2xyz: lch2xyz,
+ lch2rgb: lch2rgb,
}
@@ -162,6 +169,9 @@ function rgb2lab(rgb) {
return [l, a, b];
}
+function rgb2lch(args) {
+ return lab2lch(rgb2lab(args));
+}
function hsl2rgb(hsl) {
var h = hsl[0] / 360,
@@ -345,6 +355,10 @@ function xyz2lab(xyz) {
return [l, a, b];
}
+function xyz2lch(args) {
+ return lab2lch(xyz2lab(args));
+}
+
function lab2xyz(lab) {
var l = lab[0],
a = lab[1],
@@ -366,10 +380,45 @@ function lab2xyz(lab) {
return [x, y, z];
}
+function lab2lch(lab) {
+ var l = lab[0],
+ a = lab[1],
+ b = lab[2],
+ hr, h, c;
+
+ hr = Math.atan2(b, a);
+ h = hr * 360 / 2 / Math.PI;
+ if (h < 0) {
+ h += 360;
+ }
+ c = Math.sqrt(a * a + b * b);
+ return [l, c, h];
+}
+
function lab2rgb(args) {
return xyz2rgb(lab2xyz(args));
}
+function lch2lab(lch) {
+ var l = lch[0],
+ c = lch[1],
+ h = lch[2],
+ a, b, hr;
+
+ hr = h / 360 * 2 * Math.PI;
+ a = c * Math.cos(hr);
+ b = c * Math.sin(hr);
+ return [l, a, b];
+}
+
+function lch2xyz(args) {
+ return lab2xyz(lch2lab(args));
+}
+
+function lch2rgb(args) {
+ return lab2rgb(lch2lab(args));
+}
+
function keyword2rgb(keyword) {
return cssKeywords[keyword];
}
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "color-convert",
"description": "Plain color conversion functions",
- "version": "0.3.2",
+ "version": "0.3.3",
"author": "Heather Arthur <fayearthur@gmail.com>",
"repository": {
"type": "git",
View
7 test/basic.js
@@ -7,6 +7,7 @@ assert.deepEqual(convert.rgb2cmyk([140, 200, 100]), [30, 0, 50, 22]);
assert.deepEqual(convert.rgb2keyword([255, 228, 196]), "bisque");
assert.deepEqual(convert.rgb2xyz([92, 191, 84]), [25, 40, 15]);
assert.deepEqual(convert.rgb2lab([92, 191, 84]), [70, -50, 45]);
+assert.deepEqual(convert.rgb2lch([92, 191, 84]), [70, 67, 138]);
assert.deepEqual(convert.hsl2rgb([96, 48, 59]), [140, 201, 100]);
assert.deepEqual(convert.hsl2hsv([96, 48, 59]), [96, 50, 79]); // colorpicker says [96,50,79]
@@ -33,9 +34,15 @@ assert.deepEqual(convert.keyword2xyz("blue"), [18, 7, 95]);
assert.deepEqual(convert.xyz2rgb([25, 40, 15]), [97, 190, 85]);
assert.deepEqual(convert.xyz2rgb([50, 100, 100]), [0, 255, 241]);
assert.deepEqual(convert.xyz2lab([25, 40, 15]), [69, -48, 44]);
+assert.deepEqual(convert.xyz2lch([25, 40, 15]), [69, 65, 137]);
assert.deepEqual(convert.lab2xyz([69, -48, 44]), [25, 39, 15]);
assert.deepEqual(convert.lab2rgb([75, 20, -30]), [194, 175, 240]);
+assert.deepEqual(convert.lab2lch([69, -48, 44]), [69, 65, 137]);
+
+assert.deepEqual(convert.lch2lab([69, 65, 137]), [69, -48, 44]);
+assert.deepEqual(convert.lch2xyz([69, 65, 137]), [25, 39, 15]);
+assert.deepEqual(convert.lch2rgb([69, 65, 137]), [98, 188, 83]);
// non-array arguments
assert.deepEqual(convert.hsl2rgb(96, 48, 59), [140, 201, 100]);
Please sign in to comment.
Something went wrong with that request. Please try again.