Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

start of cielch support

  • Loading branch information...
commit 5395642d0b8c3ec51a981a840239ebd9fd95395a 1 parent 1597cce
@justincormack authored
View
1  Makefile
@@ -78,6 +78,7 @@ d3.core.js: \
src/core/hsl.js \
src/core/xyz.js \
src/core/cielab.js \
+ src/core/cielch.js \
src/core/selection.js \
src/core/selection-select.js \
src/core/selection-selectAll.js \
View
71 d3.v2.js
@@ -1197,6 +1197,10 @@ d3_Rgb.prototype.cielab = function() {
return this.xyz().cielab();
};
+d3_Rgb.prototype.cielch = function() {
+ return this.cielab().cielch();
+};
+
d3_Rgb.prototype.toString = function() {
return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
};
@@ -1495,6 +1499,10 @@ d3_Hsl.prototype.cielab = function() {
return this.xyz().cielab();
};
+d3_Hsl.prototype.cielch = function() {
+ return this.cielab().cielch();
+};
+
d3_Hsl.prototype.toString = function() {
return this.rgb().toString();
};
@@ -1560,10 +1568,9 @@ d3_Xyz.prototype.cielab = function() {
return d3_xyz_cielab(this.x, this.y, this.z);
};
-/* add brighter, darker */
-/*d3_Xyz.prototype.brighter = function(k) {
-
-}*/
+d3_Xyz.prototype.cielch = function() {
+ return this.cielab().cielch();
+};
d3_Xyz.prototype.toString = function() {
return this.rgb().toString();
@@ -1642,6 +1649,10 @@ d3_Cielab.prototype.xyz = function() {
return d3_cielab_xyz(this.l, this.a, this.b);
};
+d3_Cielab.prototype.cielch = function() {
+ return d3_cielab_cielch(this.l, this.a, this.b);
+};
+
/* 18 chosen to correspond roughly to RGB brighter/darker */
d3_Cielab.prototype.brighter = function(k) {
return d3_cielab(Math.min(100, this.l + 18 * (arguments.length ? k : 1)), this.a, this.b);
@@ -1668,6 +1679,58 @@ function d3_cielab_xyz(l, a, b) {
return d3_xyz(v(x) * 95.047, v(y) * 100.000, v(z) * 108.883);
}
+function d3_cielab_cielch(l, a, b) {
+ var h = Math.atan2(b, a);
+
+ h = h > 0 ? (h / Math.PI) * 180 : 360 - (Math.abs(h) / Math.PI) * 180;
+
+ c = Math.sqrt(a * a + b * b);
+
+ return d3_cielch(l, c, h);
+}
+
+d3.cielch = function(l, c, h) {
+ return arguments.length === 1
+ ? (l instanceof d3_Cielch ? d3_cielch(l.l, l.c, l.h)
+ : d3_rgb_parse("" + l, d3_rgb_xyz, d3_hsl_xyz).cielch())
+ : d3_cielch(+l, +c, +h);
+};
+
+function d3_cielch(l, c, h) {
+ return new d3_Cielch(l, c, h);
+}
+
+function d3_Cielch(l, c, h) {
+ this.l = l;
+ this.c = c;
+ this.h = h;
+}
+
+d3_Cielch.prototype.rgb = function() {
+ return this.cielab().rgb();
+};
+
+d3_Cielch.prototype.hsl = function() {
+ return this.cielab().hsl();
+};
+
+d3_Cielch.prototype.xyz = function() {
+ return this.cielab().xyz();
+};
+
+d3_Cielch.prototype.brighter = function(k) {
+ return d3_cielch(Math.min(100, this.l + 18 * (arguments.length ? k : 1)), this.c, this.h);
+};
+
+d3_Cielch.prototype.darker = function(k) {
+ return d3_cielch(Math.max(0, this.l - 18 * (arguments.length ? k : 1)), this.c, this.h);
+};
+
+d3_Cielch.prototype.toString = function() {
+ return this.rgb().toString();
+};
+
+
function d3_selection(groups) {
d3_arraySubclass(groups, d3_selectionPrototype);
return groups;
View
8 d3.v2.min.js
4 additions, 4 deletions not shown
View
14 src/core/cielab.js
@@ -27,6 +27,10 @@ d3_Cielab.prototype.xyz = function() {
return d3_cielab_xyz(this.l, this.a, this.b);
};
+d3_Cielab.prototype.cielch = function() {
+ return d3_cielab_cielch(this.l, this.a, this.b);
+};
+
/* 18 chosen to correspond roughly to RGB brighter/darker */
d3_Cielab.prototype.brighter = function(k) {
return d3_cielab(Math.min(100, this.l + 18 * (arguments.length ? k : 1)), this.a, this.b);
@@ -53,3 +57,13 @@ function d3_cielab_xyz(l, a, b) {
return d3_xyz(v(x) * 95.047, v(y) * 100.000, v(z) * 108.883);
}
+function d3_cielab_cielch(l, a, b) {
+ var h = Math.atan2(b, a);
+
+ h = h > 0 ? (h / Math.PI) * 180 : 360 - (Math.abs(h) / Math.PI) * 180;
+
+ c = Math.sqrt(a * a + b * b);
+
+ return d3_cielch(l, c, h);
+}
+
View
42 src/core/cielch.js
@@ -0,0 +1,42 @@
+d3.cielch = function(l, c, h) {
+ return arguments.length === 1
+ ? (l instanceof d3_Cielch ? d3_cielch(l.l, l.c, l.h)
+ : d3_rgb_parse("" + l, d3_rgb_xyz, d3_hsl_xyz).cielch())
+ : d3_cielch(+l, +c, +h);
+};
+
+function d3_cielch(l, c, h) {
+ return new d3_Cielch(l, c, h);
+}
+
+function d3_Cielch(l, c, h) {
+ this.l = l;
+ this.c = c;
+ this.h = h;
+}
+
+d3_Cielch.prototype.rgb = function() {
+ return this.cielab().rgb();
+};
+
+d3_Cielch.prototype.hsl = function() {
+ return this.cielab().hsl();
+};
+
+d3_Cielch.prototype.xyz = function() {
+ return this.cielab().xyz();
+};
+
+d3_Cielch.prototype.brighter = function(k) {
+ return d3_cielch(Math.min(100, this.l + 18 * (arguments.length ? k : 1)), this.c, this.h);
+};
+
+d3_Cielch.prototype.darker = function(k) {
+ return d3_cielch(Math.max(0, this.l - 18 * (arguments.length ? k : 1)), this.c, this.h);
+};
+
+d3_Cielch.prototype.toString = function() {
+ return this.rgb().toString();
+};
+
+
View
4 src/core/hsl.js
@@ -37,6 +37,10 @@ d3_Hsl.prototype.cielab = function() {
return this.xyz().cielab();
};
+d3_Hsl.prototype.cielch = function() {
+ return this.cielab().cielch();
+};
+
d3_Hsl.prototype.toString = function() {
return this.rgb().toString();
};
View
4 src/core/rgb.js
@@ -51,6 +51,10 @@ d3_Rgb.prototype.cielab = function() {
return this.xyz().cielab();
};
+d3_Rgb.prototype.cielch = function() {
+ return this.cielab().cielch();
+};
+
d3_Rgb.prototype.toString = function() {
return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
};
View
7 src/core/xyz.js
@@ -27,10 +27,9 @@ d3_Xyz.prototype.cielab = function() {
return d3_xyz_cielab(this.x, this.y, this.z);
};
-/* add brighter, darker */
-/*d3_Xyz.prototype.brighter = function(k) {
-
-}*/
+d3_Xyz.prototype.cielch = function() {
+ return this.cielab().cielch();
+};
d3_Xyz.prototype.toString = function() {
return this.rgb().toString();
View
5 test/core/cielab-test.js
@@ -11,7 +11,10 @@ suite.addBatch({
return d3.cielab;
},
"exposes l, a, and b properties": function(lab) {
- assert.labEqual(lab("#048F07"), 51.480406534539185, -55.524431964471155, 52.88478430006963);
+ var color = lab("#048F07");
+ assert.equal(color.l, 51.480406534539185);
+ assert.equal(color.a, -55.524431964471155);
+ assert.equal(color.b, 52.88478430006963);
},
"can convert from CieLAB": function(lab) {
assert.labEqual(lab(d3.cielab(40, 50, 60)), 40, 50, 60);
View
70 test/core/cielch-test.js
@@ -0,0 +1,70 @@
+require("../env");
+
+var vows = require("vows"),
+ assert = require("assert");
+
+var suite = vows.describe("d3.cielch");
+
+suite.addBatch({
+ "cielch": {
+ topic: function() {
+ return d3.cielch;
+ },
+ "exposes l, c, and h properties": function(lch) {
+ var color = lch("#048F07");
+ assert.equal(color.l, 51.480406534539185);
+ assert.equal(color.c, 76.67961238453204);
+ assert.equal(color.h, 136.39481492184387);
+ },
+ "can convert from Cielch": function(lch) {
+ assert.lchEqual(lch(d3.cielch(40, 50, 60)), 40, 50, 60);
+ },
+ "can convert from RGB": function(lch) {
+ assert.lchEqual(d3.rgb("#048F07").cielch(), 51.480406534539185, 76.67961238453204, 136.39481492184387);
+ },
+ "can convert from HSL": function(lch) {
+ assert.lchEqual(d3.hsl("#048F07").cielch(), 51.480406534539185, 76.67961238453204, 136.39481492184387);
+ },
+ "can convert from XYZ": function(lch) {
+ assert.lchEqual(d3.xyz("#048F07").cielch(), 51.480406534539185, 76.67961238453204, 136.39481492184387);
+ },
+ "can convert from CIELab": function(lch) {
+ assert.lchEqual(d3.cielab("#048F07").cielch(), 51.480406534539185, 76.67961238453204, 136.39481492184387);
+ },
+ "can initialize with RGB": function(lch) {
+ assert.lchEqual(lch(d3.rgb("#048F07")), 51.480406534539185, 76.67961238453204, 136.39481492184387);
+ },
+ "can convert to RGB": function(lch) {
+ assert.rgbEqual(lch(d3.rgb(30, 40, 50)).rgb(), 30, 40, 50);
+ },
+ "can convert to HSL": function(lch) {
+ assert.hslEqual(lch("red").hsl(), 0, 1, .5);
+ },
+ "can convert to XYZ": function(lch) {
+ assert.xyzEqual(lch("#abc").xyz(), 45.246971009995335, 48.44632879252148, 64.09304697440157);
+ },
+ "can convert to CIELab": function(lab) {
+ assert.labEqual(lch(d3.cielab("#048F07")), 51.480406534539185, -55.524431964471155, 52.88478430006963);
+ },
+ "can derive a brighter color": function(lch) {
+ var brown = lch("brown");
+ assert.equal(brown.brighter().l, brown.l + 18);
+ assert.equal(brown.brighter(2).l, brown.l + 36);
+ },
+ "can derive a darker color": function(lch) {
+ var brown = lch("brown");
+ assert.equal(brown.darker().l, brown.l - 18);
+ assert.equal(brown.darker(2).l, brown.l - 36);
+ },
+ "string coercion returns hexadecimal format": function(lch) {
+ assert.strictEqual(lch("#abcdef") + "", "#abcdef");
+ assert.strictEqual(lch("moccasin") + "", "#ffe4b5");
+ assert.strictEqual(lch("hsl(60, 100%, 20%)") + "", "#666600");
+ assert.strictEqual(lch("rgb(12, 34, 56)") + "", "#0c2238");
+ assert.strictEqual(lch(d3.rgb(12, 34, 56)) + "", "#0c2238");
+ assert.strictEqual(lch(d3.hsl(60, 1, .2)) + "", "#666600");
+ }
+ }
+});
+
+suite.export(module);
View
6 test/env-assert.js
@@ -42,6 +42,12 @@ assert.labEqual = function(actual, l, a, b, message) {
}
};
+assert.lchEqual = function(actual, l, c, h, message) {
+ if (Math.abs(actual.l - l) > 1e-6 || Math.abs(actual.c - c) > 1e-6 || Math.abs(actual.h - h) > 1e-6) {
+ assert.fail("lch(" + actual.l + "," + actual.c + "," + actual.h + ")", "lch(" + l + "," + c + "," + h + ")", message || "expected {expected}, got {actual}", null, assert.lchEqual);
+ }
+};
+
assert.pathEqual = function(actual, expected, message) {
if (!pathEqual(actual, expected)) {
assert.fail(formatPath(actual), formatPath(expected), message || "expected {expected}, got {actual}", null, assert.pathEqual);
Please sign in to comment.
Something went wrong with that request. Please try again.