Skip to content

Commit

Permalink
Save a few bytes.
Browse files Browse the repository at this point in the history
mbostock committed Mar 24, 2014

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent abbe1c7 commit d3bbf39
Showing 3 changed files with 27 additions and 33 deletions.
31 changes: 14 additions & 17 deletions d3.js
Original file line number Diff line number Diff line change
@@ -1620,7 +1620,7 @@
return v < 16 ? "0" + Math.max(0, v).toString(16) : Math.min(255, v).toString(16);
}
function d3_rgb_parse(format, rgb, hsl) {
var r = 0, g = 0, b = 0, value, m1, m2, name;
var r = 0, g = 0, b = 0, m1, m2, color;
m1 = /([a-z]+)\((.*)\)/i.exec(format);
if (m1) {
m2 = m1[2].split(",");
@@ -1636,22 +1636,19 @@
}
}
}
if (name = d3_rgb_names.get(format)) return rgb(name.r, name.g, name.b);
if (format != null && format.charAt(0) === "#") {
value = parseInt(format.substring(1), 16);
if (!isNaN(value)) {
if (format.length === 4) {
r = (value & 3840) >> 4;
r = r >> 4 | r;
g = value & 240;
g = g >> 4 | g;
b = value & 15;
b = b << 4 | b;
} else if (format.length === 7) {
r = (value & 16711680) >> 16;
g = (value & 65280) >> 8;
b = value & 255;
}
if (color = d3_rgb_names.get(format)) return rgb(color.r, color.g, color.b);
if (format != null && format.charAt(0) === "#" && !isNaN(color = parseInt(format.substring(1), 16))) {
if (format.length === 4) {
r = (color & 3840) >> 4;
r = r >> 4 | r;
g = color & 240;
g = g >> 4 | g;
b = color & 15;
b = b << 4 | b;
} else if (format.length === 7) {
r = (color & 16711680) >> 16;
g = (color & 65280) >> 8;
b = color & 255;
}
}
return rgb(r, g, b);
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

27 changes: 12 additions & 15 deletions src/color/rgb.js
Original file line number Diff line number Diff line change
@@ -67,10 +67,9 @@ function d3_rgb_parse(format, rgb, hsl) {
var r = 0, // red channel; int in [0, 255]
g = 0, // green channel; int in [0, 255]
b = 0, // blue channel; int in [0, 255]
value,
m1, // CSS color specification match
m2, // CSS color specification type (e.g., rgb)
name;
color;

/* Handle hsl, rgb. */
m1 = /([a-z]+)\((.*)\)/i.exec(format);
@@ -95,23 +94,21 @@ function d3_rgb_parse(format, rgb, hsl) {
}

/* Named colors. */
if (name = d3_rgb_names.get(format)) return rgb(name.r, name.g, name.b);
if (color = d3_rgb_names.get(format)) return rgb(color.r, color.g, color.b);

/* Hexadecimal colors: #rgb and #rrggbb. */
if (format != null && format.charAt(0) === "#") {
value = parseInt(format.substring(1), 16);
if (!isNaN(value)) {
if (format.length === 4) {
r = (value & 0xf00) >> 4; r = (r >> 4) | r;
g = (value & 0xf0); g = (g >> 4) | g;
b = (value & 0xf); b = (b << 4) | b;
} else if (format.length === 7) {
r = (value & 0xff0000) >> 16;
g = (value & 0xff00) >> 8;
b = (value & 0xff);
}
if (format != null && format.charAt(0) === "#" && !isNaN(color = parseInt(format.substring(1), 16))) {
if (format.length === 4) {
r = (color & 0xf00) >> 4; r = (r >> 4) | r;
g = (color & 0xf0); g = (g >> 4) | g;
b = (color & 0xf); b = (b << 4) | b;
} else if (format.length === 7) {
r = (color & 0xff0000) >> 16;
g = (color & 0xff00) >> 8;
b = (color & 0xff);
}
}

return rgb(r, g, b);
}

0 comments on commit d3bbf39

Please sign in to comment.