Skip to content

Commit

Permalink
Copy constructors for d3.rgb and d3.hsl.
Browse files Browse the repository at this point in the history
Previously, these would work by coercing the input color to a string and then
parsing it. This is slow, but more importantly, this is a lossy process for HSL
colors due to the conversion to hexadecimal RGB format. This commit detects
instances of d3_Rgb and d3_Hsl on input and copies them efficiently.
  • Loading branch information
mbostock committed Oct 7, 2011
1 parent cc0ae76 commit 0b852dd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions d3.js
Expand Up @@ -867,7 +867,8 @@ function d3_uninterpolateClamp(a, b) {
}
d3.rgb = function(r, g, b) {
return arguments.length === 1
? d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb)
? (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);
};

Expand Down Expand Up @@ -1151,7 +1152,8 @@ for (var d3_rgb_name in d3_rgb_names) {
}
d3.hsl = function(h, s, l) {
return arguments.length === 1
? d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl)
? (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);
};

Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/core/hsl.js
@@ -1,6 +1,7 @@
d3.hsl = function(h, s, l) {
return arguments.length === 1
? d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl)
? (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);
};

Expand Down
3 changes: 2 additions & 1 deletion src/core/rgb.js
@@ -1,6 +1,7 @@
d3.rgb = function(r, g, b) {
return arguments.length === 1
? d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb)
? (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);
};

Expand Down

0 comments on commit 0b852dd

Please sign in to comment.