Permalink
Browse files

Add d3_Color base class.

  • Loading branch information...
1 parent e01f789 commit e294affffcf5333db3375b7c397cef9915721f39 @mbostock mbostock committed Sep 28, 2012
Showing with 54 additions and 53 deletions.
  1. +1 −0 Makefile
  2. +22 −23 d3.v2.js
  3. +4 −4 d3.v2.min.js
  4. +5 −0 src/core/color.js
  5. +5 −7 src/core/hcl.js
  6. +5 −7 src/core/hsl.js
  7. +1 −1 src/core/interpolate.js
  8. +5 −7 src/core/lab.js
  9. +6 −4 src/core/rgb.js
View
1 Makefile
@@ -80,6 +80,7 @@ d3.core.js: \
src/core/transform.js \
src/core/interpolate.js \
src/core/uninterpolate.js \
+ src/core/color.js \
src/core/rgb.js \
src/core/hsl.js \
src/core/hcl.js \
View
45 d3.v2.js
@@ -224,6 +224,7 @@
return Math.max(0, Math.min(1, (x - a) * b));
};
}
+ function d3_Color() {}
function d3_rgb(r, g, b) {
return new d3_Rgb(r, g, b);
}
@@ -3347,14 +3348,18 @@
}, function(a, b) {
return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + "");
}, function(a, b) {
- return (typeof b === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b);
+ return (typeof b === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Color) && d3.interpolateRgb(a, b);
}, function(a, b) {
return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b);
} ];
+ d3_Color.prototype.toString = function() {
+ return this.rgb() + "";
+ };
d3.rgb = function(r, g, b) {
return arguments.length === 1 ? r instanceof d3_Rgb ? d3_rgb(r.r, r.g, r.b) : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb) : d3_rgb(~~r, ~~g, ~~b);
};
- d3_Rgb.prototype.brighter = function(k) {
+ var d3_rgbPrototype = d3_Rgb.prototype = new d3_Color;
+ d3_rgbPrototype.brighter = function(k) {
k = Math.pow(.7, arguments.length ? k : 1);
var r = this.r, g = this.g, b = this.b, i = 30;
if (!r && !g && !b) return d3_rgb(i, i, i);
@@ -3363,14 +3368,14 @@
if (b && b < i) b = i;
return d3_rgb(Math.min(255, Math.floor(r / k)), Math.min(255, Math.floor(g / k)), Math.min(255, Math.floor(b / k)));
};
- d3_Rgb.prototype.darker = function(k) {
+ d3_rgbPrototype.darker = function(k) {
k = Math.pow(.7, arguments.length ? k : 1);
return d3_rgb(Math.floor(k * this.r), Math.floor(k * this.g), Math.floor(k * this.b));
};
- d3_Rgb.prototype.hsl = function() {
+ d3_rgbPrototype.hsl = function() {
return d3_rgb_hsl(this.r, this.g, this.b);
};
- d3_Rgb.prototype.toString = function() {
+ d3_rgbPrototype.toString = function() {
return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
};
var d3_rgb_names = d3.map({
@@ -3528,52 +3533,46 @@
d3.hsl = function(h, s, l) {
return arguments.length === 1 ? h instanceof d3_Hsl ? d3_hsl(h.h, h.s, h.l) : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl) : d3_hsl(+h, +s, +l);
};
- d3_Hsl.prototype.brighter = function(k) {
+ var d3_hslPrototype = d3_Hsl.prototype = new d3_Color;
+ d3_hslPrototype.brighter = function(k) {
k = Math.pow(.7, arguments.length ? k : 1);
return d3_hsl(this.h, this.s, this.l / k);
};
- d3_Hsl.prototype.darker = function(k) {
+ d3_hslPrototype.darker = function(k) {
k = Math.pow(.7, arguments.length ? k : 1);
return d3_hsl(this.h, this.s, k * this.l);
};
- d3_Hsl.prototype.rgb = function() {
+ d3_hslPrototype.rgb = function() {
return d3_hsl_rgb(this.h, this.s, this.l);
};
- d3_Hsl.prototype.toString = function() {
- return this.rgb().toString();
- };
d3.hcl = function(h, c, l) {
return arguments.length === 1 ? h instanceof d3_Hcl ? d3_hcl(h.h, h.c, h.l) : h instanceof d3_Lab ? d3_lab_hcl(h.l, h.a, h.b) : d3_lab_hcl((h = d3_rgb_lab((h = d3.rgb(h)).r, h.g, h.b)).l, h.a, h.b) : d3_hcl(+h, +c, +l);
};
- d3_Hcl.prototype.brighter = function(k) {
+ var d3_hclPrototype = d3_Hcl.prototype = new d3_Color;
+ d3_hclPrototype.brighter = function(k) {
return d3_hcl(this.h, this.c, Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)));
};
- d3_Hcl.prototype.darker = function(k) {
+ d3_hclPrototype.darker = function(k) {
return d3_hcl(this.h, this.c, Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)));
};
- d3_Hcl.prototype.rgb = function() {
+ d3_hclPrototype.rgb = function() {
return d3_hcl_lab(this.h, this.c, this.l).rgb();
};
- d3_Hcl.prototype.toString = function() {
- return this.rgb() + "";
- };
d3.lab = function(l, a, b) {
return arguments.length === 1 ? l instanceof d3_Lab ? d3_lab(l.l, l.a, l.b) : l instanceof d3_Hcl ? d3_hcl_lab(l.l, l.c, l.h) : d3_rgb_lab((l = d3.rgb(l)).r, l.g, l.b) : d3_lab(+l, +a, +b);
};
var d3_lab_K = 18;
var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883;
- d3_Lab.prototype.brighter = function(k) {
+ var d3_labPrototype = d3_Lab.prototype = new d3_Color;
+ d3_labPrototype.brighter = function(k) {
return d3_lab(Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)), this.a, this.b);
};
- d3_Lab.prototype.darker = function(k) {
+ d3_labPrototype.darker = function(k) {
return d3_lab(Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)), this.a, this.b);
};
- d3_Lab.prototype.rgb = function() {
+ d3_labPrototype.rgb = function() {
return d3_lab_rgb(this.l, this.a, this.b);
};
- d3_Lab.prototype.toString = function() {
- return this.rgb() + "";
- };
var d3_select = function(s, n) {
return n.querySelector(s);
}, d3_selectAll = function(s, n) {
View
8 d3.v2.min.js
4 additions, 4 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
5 src/core/color.js
@@ -0,0 +1,5 @@
+function d3_Color() {}
+
+d3_Color.prototype.toString = function() {
+ return this.rgb() + "";
+};
View
12 src/core/hcl.js
@@ -16,22 +16,20 @@ function d3_Hcl(h, c, l) {
this.l = l;
}
-d3_Hcl.prototype.brighter = function(k) {
+var d3_hclPrototype = d3_Hcl.prototype = new d3_Color;
+
+d3_hclPrototype.brighter = function(k) {
return d3_hcl(this.h, this.c, Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)));
};
-d3_Hcl.prototype.darker = function(k) {
+d3_hclPrototype.darker = function(k) {
return d3_hcl(this.h, this.c, Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)));
};
-d3_Hcl.prototype.rgb = function() {
+d3_hclPrototype.rgb = function() {
return d3_hcl_lab(this.h, this.c, this.l).rgb();
};
-d3_Hcl.prototype.toString = function() {
- return this.rgb() + "";
-};
-
function d3_hcl_lab(h, c, l) {
return d3_lab(l, Math.cos(h *= Math.PI / 180) * c, Math.sin(h) * c);
}
View
12 src/core/hsl.js
@@ -15,24 +15,22 @@ function d3_Hsl(h, s, l) {
this.l = l;
}
-d3_Hsl.prototype.brighter = function(k) {
+var d3_hslPrototype = d3_Hsl.prototype = new d3_Color;
+
+d3_hslPrototype.brighter = function(k) {
k = Math.pow(0.7, arguments.length ? k : 1);
return d3_hsl(this.h, this.s, this.l / k);
};
-d3_Hsl.prototype.darker = function(k) {
+d3_hslPrototype.darker = function(k) {
k = Math.pow(0.7, arguments.length ? k : 1);
return d3_hsl(this.h, this.s, k * this.l);
};
-d3_Hsl.prototype.rgb = function() {
+d3_hslPrototype.rgb = function() {
return d3_hsl_rgb(this.h, this.s, this.l);
};
-d3_Hsl.prototype.toString = function() {
- return this.rgb().toString();
-};
-
function d3_hsl_rgb(h, s, l) {
var m1,
m2;
View
2 src/core/interpolate.js
@@ -256,6 +256,6 @@ d3.interpolators = [
d3.interpolateObject,
function(a, b) { return b instanceof Array && d3.interpolateArray(a, b); },
function(a, b) { return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + ""); },
- function(a, b) { return (typeof b === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
+ function(a, b) { return (typeof b === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Color) && d3.interpolateRgb(a, b); },
function(a, b) { return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b); }
];
View
12 src/core/lab.js
@@ -24,22 +24,20 @@ var d3_lab_X = 0.950470,
d3_lab_Y = 1,
d3_lab_Z = 1.088830;
-d3_Lab.prototype.brighter = function(k) {
+var d3_labPrototype = d3_Lab.prototype = new d3_Color;
+
+d3_labPrototype.brighter = function(k) {
return d3_lab(Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)), this.a, this.b);
};
-d3_Lab.prototype.darker = function(k) {
+d3_labPrototype.darker = function(k) {
return d3_lab(Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)), this.a, this.b);
};
-d3_Lab.prototype.rgb = function() {
+d3_labPrototype.rgb = function() {
return d3_lab_rgb(this.l, this.a, this.b);
};
-d3_Lab.prototype.toString = function() {
- return this.rgb() + "";
-};
-
function d3_lab_rgb(l, a, b) {
var y = (l + 16) / 116,
x = y + a / 500,
View
10 src/core/rgb.js
@@ -15,7 +15,9 @@ function d3_Rgb(r, g, b) {
this.b = b;
}
-d3_Rgb.prototype.brighter = function(k) {
+var d3_rgbPrototype = d3_Rgb.prototype = new d3_Color;
+
+d3_rgbPrototype.brighter = function(k) {
k = Math.pow(0.7, arguments.length ? k : 1);
var r = this.r,
g = this.g,
@@ -31,19 +33,19 @@ d3_Rgb.prototype.brighter = function(k) {
Math.min(255, Math.floor(b / k)));
};
-d3_Rgb.prototype.darker = function(k) {
+d3_rgbPrototype.darker = function(k) {
k = Math.pow(0.7, arguments.length ? k : 1);
return d3_rgb(
Math.floor(k * this.r),
Math.floor(k * this.g),
Math.floor(k * this.b));
};
-d3_Rgb.prototype.hsl = function() {
+d3_rgbPrototype.hsl = function() {
return d3_rgb_hsl(this.r, this.g, this.b);
};
-d3_Rgb.prototype.toString = function() {
+d3_rgbPrototype.toString = function() {
return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
};

0 comments on commit e294aff

Please sign in to comment.