Skip to content

Commit

Permalink
Use prototypes for d3.rgb() and d3.hsl().
Browse files Browse the repository at this point in the history
This turns out to be slightly faster than using structs (`{}`) and is bit
cleaner.  You can now also do:

    d3.rgb(r, g, b).hsl()

and:

    d3.hsl(h, s, l).rgb()

to convert between RGB and HSL.
  • Loading branch information
jasondavies committed May 5, 2011
1 parent 668cdaa commit a591160
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
29 changes: 22 additions & 7 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,14 +707,20 @@ d3.rgb = function(r, g, b) {
};

function d3_rgb(r, g, b) {
return {
r: r, g: g, b: b,
toString: d3_rgb_format,
brighter: d3_rgb_brighter,
darker: d3_rgb_darker
};
return new d3_Rgb(r, g, b);
}

function d3_Rgb(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}

d3_Rgb.prototype.toString = d3_rgb_format;
d3_Rgb.prototype.brighter = d3_rgb_brighter;
d3_Rgb.prototype.darker = d3_rgb_darker;
d3_Rgb.prototype.hsl = d3_rgb_hsl;

function d3_rgb_brighter(k) {
k = Math.pow(0.7, arguments.length ? k : 1);
var r = this.r,
Expand Down Expand Up @@ -991,9 +997,18 @@ d3.hsl = function(h, s, l) {
};

function d3_hsl(h, s, l) {
return {h: h, s: s, l: l, toString: d3_hsl_format};
return new d3_Hsl(h, s, l);
}

function d3_Hsl(h, s, l) {
this.h = h;
this.s = s;
this.l = l;
}

d3_Hsl.prototype.toString = d3_hsl_format;
d3_Hsl.prototype.hsl = d3_hsl_rgb;

/** @this d3_hsl */
function d3_hsl_format() {
return "hsl(" + this.h + "," + this.s * 100 + "%," + this.l * 100 + "%)";
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/core/hsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ d3.hsl = function(h, s, l) {
};

function d3_hsl(h, s, l) {
return {h: h, s: s, l: l, toString: d3_hsl_format};
return new d3_Hsl(h, s, l);
}

function d3_Hsl(h, s, l) {
this.h = h;
this.s = s;
this.l = l;
}

d3_Hsl.prototype.toString = d3_hsl_format;
d3_Hsl.prototype.hsl = d3_hsl_rgb;

/** @this d3_hsl */
function d3_hsl_format() {
return "hsl(" + this.h + "," + this.s * 100 + "%," + this.l * 100 + "%)";
Expand Down
18 changes: 12 additions & 6 deletions src/core/rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ d3.rgb = function(r, g, b) {
};

function d3_rgb(r, g, b) {
return {
r: r, g: g, b: b,
toString: d3_rgb_format,
brighter: d3_rgb_brighter,
darker: d3_rgb_darker
};
return new d3_Rgb(r, g, b);
}

function d3_Rgb(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}

d3_Rgb.prototype.toString = d3_rgb_format;
d3_Rgb.prototype.brighter = d3_rgb_brighter;
d3_Rgb.prototype.darker = d3_rgb_darker;
d3_Rgb.prototype.hsl = d3_rgb_hsl;

function d3_rgb_brighter(k) {
k = Math.pow(0.7, arguments.length ? k : 1);
var r = this.r,
Expand Down

0 comments on commit a591160

Please sign in to comment.