Skip to content

Commit

Permalink
add chroma and hue set and get to cielab
Browse files Browse the repository at this point in the history
  • Loading branch information
justincormack committed Mar 4, 2012
1 parent 9a0de8b commit f8bbbde
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
31 changes: 31 additions & 0 deletions d3.v2.js
Expand Up @@ -1565,6 +1565,30 @@ d3_Cielab.prototype.darker = function(k) {
return d3_cielab(Math.max(0, this.l - 18 * (arguments.length ? k : 1)), this.a, this.b);
};

d3_Cielab.prototype.hue = function(h) {
var a = this.a;
var b = this.b;

if (arguments.length) {
return d3_cielab_lch(this.l, this.chroma(), h)
}

var h = Math.atan2(b, a);

return h > 0 ? (h / Math.PI) * 180 : 360 - (Math.abs(h) / Math.PI) * 180;
}

d3_Cielab.prototype.chroma = function(c) {
var a = this.a;
var b = this.b;

if (arguments.length) {
return d3_cielab_lch(this.l, c, this.hue())
}

return Math.sqrt(a * a + b * b);
}

d3_Cielab.prototype.toString = function() {
return this.rgb().toString();
};
Expand Down Expand Up @@ -1605,6 +1629,13 @@ function d3_cielab_cielch(l, a, b) {
return d3_cielch(l, c, h);
}

function d3_cielab_lch(l, c, h) {
var hr = h * (Math.PI / 180);
var a = Math.cos(hr) * c;
var b = Math.sin(hr) * c;
return d3_cielab(l, a, b);
}

d3.cielch = function(l, c, h) {
return arguments.length === 1
? (l instanceof d3_Cielch ? d3_cielch(l.l, l.c, l.h)
Expand Down
8 changes: 4 additions & 4 deletions d3.v2.min.js

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions src/core/cielab.js
Expand Up @@ -32,6 +32,30 @@ d3_Cielab.prototype.darker = function(k) {
return d3_cielab(Math.max(0, this.l - 18 * (arguments.length ? k : 1)), this.a, this.b);
};

d3_Cielab.prototype.hue = function(h) {
var a = this.a;
var b = this.b;

if (arguments.length) {
return d3_cielab_lch(this.l, this.chroma(), h)
}

var h = Math.atan2(b, a);

return h > 0 ? (h / Math.PI) * 180 : 360 - (Math.abs(h) / Math.PI) * 180;
}

d3_Cielab.prototype.chroma = function(c) {
var a = this.a;
var b = this.b;

if (arguments.length) {
return d3_cielab_lch(this.l, c, this.hue())
}

return Math.sqrt(a * a + b * b);
}

d3_Cielab.prototype.toString = function() {
return this.rgb().toString();
};
Expand Down Expand Up @@ -72,3 +96,10 @@ function d3_cielab_cielch(l, a, b) {
return d3_cielch(l, c, h);
}

function d3_cielab_lch(l, c, h) {
var hr = h * (Math.PI / 180);
var a = Math.cos(hr) * c;
var b = Math.sin(hr) * c;
return d3_cielab(l, a, b);
}

12 changes: 12 additions & 0 deletions test/core/cielab-test.js
Expand Up @@ -38,6 +38,18 @@ suite.addBatch({
assert.equal(brown.darker().l, brown.l - 18);
assert.equal(brown.darker(2).l, brown.l - 36);
},
"can get chroma value": function(lab) {
assert.equal(lab("#048F07").chroma(), 76.67961238453204);
},
"can get hue value": function(lab) {
assert.equal(lab("#048F07").hue(), 136.39481492184387);
},
"can set chroma value": function(lab) {
assert.equal(lab("red").chroma(14).chroma(), 14);
},
"can set hue value": function(lab) {
assert.equal(lab("red").hue(14).hue(), 14);
},
"string coercion returns hexadecimal format": function(lab) {
assert.strictEqual(lab("#abcdef") + "", "#abcdef");
assert.strictEqual(lab("moccasin") + "", "#ffe4b5");
Expand Down

0 comments on commit f8bbbde

Please sign in to comment.