Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add lowercase aliases for colorbrewer palettes
  • Loading branch information
Gregor Aisch committed Mar 13, 2017
1 parent 9ef195d commit d77c2d4
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 11 deletions.
121 changes: 114 additions & 7 deletions chroma.js
Expand Up @@ -34,7 +34,7 @@
*/

(function() {
var Color, DEG2RAD, LAB_CONSTANTS, PI, PITHIRD, RAD2DEG, TWOPI, _guess_formats, _guess_formats_sorted, _input, _interpolators, abs, atan2, bezier, blend, blend_f, brewer, burn, chroma, clip_rgb, cmyk2rgb, colors, cos, css2rgb, darken, dodge, each, floor, hex2rgb, hsi2rgb, hsl2css, hsl2rgb, hsv2rgb, interpolate, interpolate_hsx, interpolate_lab, interpolate_num, interpolate_rgb, lab2lch, lab2rgb, lab_xyz, lch2lab, lch2rgb, lighten, limit, log, luminance_x, m, max, multiply, normal, num2rgb, overlay, pow, rgb2cmyk, rgb2css, rgb2hex, rgb2hsi, rgb2hsl, rgb2hsv, rgb2lab, rgb2lch, rgb2luminance, rgb2num, rgb2temperature, rgb2xyz, rgb_xyz, rnd, root, round, screen, sin, sqrt, temperature2rgb, type, unpack, w3cx11, xyz_lab, xyz_rgb,
var Color, DEG2RAD, LAB_CONSTANTS, PI, PITHIRD, RAD2DEG, TWOPI, _guess_formats, _guess_formats_sorted, _input, _interpolators, abs, atan2, bezier, blend, blend_f, brewer, burn, chroma, clip_rgb, cmyk2rgb, colors, cos, css2rgb, darken, dodge, each, floor, hcg2rgb, hex2rgb, hsi2rgb, hsl2css, hsl2rgb, hsv2rgb, interpolate, interpolate_hsx, interpolate_lab, interpolate_num, interpolate_rgb, lab2lch, lab2rgb, lab_xyz, lch2lab, lch2rgb, lighten, limit, log, luminance_x, m, max, multiply, normal, num2rgb, overlay, pow, rgb2cmyk, rgb2css, rgb2hcg, rgb2hex, rgb2hsi, rgb2hsl, rgb2hsv, rgb2lab, rgb2lch, rgb2luminance, rgb2num, rgb2temperature, rgb2xyz, rgb_xyz, rnd, root, round, screen, sin, sqrt, temperature2rgb, type, unpack, w3cx11, xyz_lab, xyz_rgb,
slice = [].slice;

type = (function() {
Expand Down Expand Up @@ -273,6 +273,7 @@
PuRd: ['#f7f4f9', '#e7e1ef', '#d4b9da', '#c994c7', '#df65b0', '#e7298a', '#ce1256', '#980043', '#67001f'],
Blues: ['#f7fbff', '#deebf7', '#c6dbef', '#9ecae1', '#6baed6', '#4292c6', '#2171b5', '#08519c', '#08306b'],
PuBuGn: ['#fff7fb', '#ece2f0', '#d0d1e6', '#a6bddb', '#67a9cf', '#3690c0', '#02818a', '#016c59', '#014636'],
Viridis: ['#440154', '#482777', '#3f4a8a', '#31678e', '#26838f', '#1f9d8a', '#6cce5a', '#b6de2b', '#fee825'],
Spectral: ['#9e0142', '#d53e4f', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#e6f598', '#abdda4', '#66c2a5', '#3288bd', '#5e4fa2'],
RdYlGn: ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850', '#006837'],
RdBu: ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#f7f7f7', '#d1e5f0', '#92c5de', '#4393c3', '#2166ac', '#053061'],
Expand All @@ -292,6 +293,15 @@
Pastel1: ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc', '#e5d8bd', '#fddaec', '#f2f2f2']
};

(function() {
var key, results;
results = [];
for (key in brewer) {
results.push(brewer[key.toLowerCase()] = brewer[key]);
}
return results;
})();


/**
X11 color names
Expand Down Expand Up @@ -1135,6 +1145,100 @@
}
});

hcg2rgb = function() {
var _c, _g, args, b, c, f, g, h, i, p, q, r, ref, ref1, ref2, ref3, ref4, ref5, t, v;
args = unpack(arguments);
h = args[0], c = args[1], _g = args[2];
c = c / 100;
g = g / 100 * 255;
_c = c * 255;
if (c === 0) {
r = g = b = _g;
} else {
if (h === 360) {
h = 0;
}
if (h > 360) {
h -= 360;
}
if (h < 0) {
h += 360;
}
h /= 60;
i = floor(h);
f = h - i;
p = _g * (1 - c);
q = p + _c * (1 - f);
t = p + _c * f;
v = p + _c;
switch (i) {
case 0:
ref = [v, t, p], r = ref[0], g = ref[1], b = ref[2];
break;
case 1:
ref1 = [q, v, p], r = ref1[0], g = ref1[1], b = ref1[2];
break;
case 2:
ref2 = [p, v, t], r = ref2[0], g = ref2[1], b = ref2[2];
break;
case 3:
ref3 = [p, q, v], r = ref3[0], g = ref3[1], b = ref3[2];
break;
case 4:
ref4 = [t, p, v], r = ref4[0], g = ref4[1], b = ref4[2];
break;
case 5:
ref5 = [v, p, q], r = ref5[0], g = ref5[1], b = ref5[2];
}
}
r = round(r);
g = round(g);
b = round(b);
return [r, g, b, args.length > 3 ? args[3] : 1];
};

rgb2hcg = function() {
var _g, b, c, delta, g, h, min, r, ref;
ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
min = Math.min(r, g, b);
max = Math.max(r, g, b);
delta = max - min;
c = delta * 100 / 255;
_g = min / (255 - delta) * 100;
if (delta === 0) {
h = Number.NaN;
} else {
if (r === max) {
h = (g - b) / delta;
}
if (g === max) {
h = 2 + (b - r) / delta;
}
if (b === max) {
h = 4 + (r - g) / delta;
}
h *= 60;
if (h < 0) {
h += 360;
}
}
return [h, c, _g];
};

chroma.hcg = function() {
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Color, slice.call(arguments).concat(['hcg']), function(){});
};

_input.hcg = hcg2rgb;

Color.prototype.hcg = function() {
return rgb2hcg(this._rgb);
};

css2rgb = function(css) {
var aa, ab, hsl, i, m, o, rgb, w;
css = css.toLowerCase();
Expand Down Expand Up @@ -1842,23 +1946,23 @@
_correctLightness = false;
_colorCache = {};
setColors = function(colors) {
var c, col, o, ref, ref1, ref2, w;
var c, col, o, ref, ref1, w;
if (colors == null) {
colors = ['#fff', '#000'];
}
if ((colors != null) && type(colors) === 'string' && (((ref = chroma.brewer) != null ? ref[colors] : void 0) != null)) {
colors = chroma.brewer[colors];
if ((colors != null) && type(colors) === 'string' && (chroma.brewer != null)) {
colors = chroma.brewer[colors] || chroma.brewer[colors.toLowerCase()] || colors;
}
if (type(colors) === 'array') {
colors = colors.slice(0);
for (c = o = 0, ref1 = colors.length - 1; 0 <= ref1 ? o <= ref1 : o >= ref1; c = 0 <= ref1 ? ++o : --o) {
for (c = o = 0, ref = colors.length - 1; 0 <= ref ? o <= ref : o >= ref; c = 0 <= ref ? ++o : --o) {
col = colors[c];
if (type(col) === "string") {
colors[c] = chroma(col);
}
}
_pos.length = 0;
for (c = w = 0, ref2 = colors.length - 1; 0 <= ref2 ? w <= ref2 : w >= ref2; c = 0 <= ref2 ? ++w : --w) {
for (c = w = 0, ref1 = colors.length - 1; 0 <= ref1 ? w <= ref1 : w >= ref1; c = 0 <= ref1 ? ++w : --w) {
_pos.push(c / (colors.length - 1));
}
}
Expand Down Expand Up @@ -2414,6 +2518,9 @@
} else if (m === 'hsv') {
xyz0 = col1.hsv();
xyz1 = col2.hsv();
} else if (m === 'hcg') {
xyz0 = col1.hcg();
xyz1 = col2.hcg();
} else if (m === 'hsi') {
xyz0 = col1.hsi();
xyz1 = col2.hsi();
Expand Down Expand Up @@ -2457,7 +2564,7 @@

_interpolators = _interpolators.concat((function() {
var len, o, ref, results;
ref = ['hsv', 'hsl', 'hsi', 'hcl', 'lch'];
ref = ['hsv', 'hsl', 'hsi', 'hcl', 'lch', 'hcg'];
results = [];
for (o = 0, len = ref.length; o < len; o++) {
m = ref[o];
Expand Down
4 changes: 2 additions & 2 deletions chroma.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/colors/colorbrewer.coffee
Expand Up @@ -62,3 +62,6 @@ chroma.brewer = brewer =
Paired: ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928']
Pastel2: ['#b3e2cd', '#fdcdac', '#cbd5e8', '#f4cae4', '#e6f5c9', '#fff2ae', '#f1e2cc', '#cccccc']
Pastel1: ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc', '#e5d8bd', '#fddaec', '#f2f2f2']

# add lowercase aliases for case-insensitive matches
do -> brewer[key.toLowerCase()] = brewer[key] for key of brewer
4 changes: 2 additions & 2 deletions src/scale.coffee
Expand Up @@ -26,8 +26,8 @@ chroma.scale = (colors, positions) ->
setColors = (colors) ->
if not colors?
colors = ['#fff', '#000']
if colors? and type(colors) == 'string' and chroma.brewer?[colors]?
colors = chroma.brewer[colors]
if colors? and type(colors) == 'string' and chroma.brewer?
colors = chroma.brewer[colors] || chroma.brewer[colors.toLowerCase()] || colors
if type(colors) == 'array'
# make a copy of the colors
colors = colors.slice(0)
Expand Down
8 changes: 8 additions & 0 deletions test/scales-test.coffee
Expand Up @@ -54,6 +54,13 @@ vows
'mid gray': (topic) -> assert.equal topic.f(50).hex(), '#ffffbf'
'ends black': (topic) -> assert.equal topic.f(100).hex(), '#006837'

'colorbrewer scale - lowercase':
topic:
f: chroma.scale 'rdylgn'
'starts white': (topic) -> assert.equal topic.f(0).hex(), '#a50026'
'mid gray': (topic) -> assert.equal topic.f(0.5).hex(), '#ffffbf'
'ends black': (topic) -> assert.equal topic.f(1).hex(), '#006837'

'colorbrewer scale - domained - classified':
topic:
f: chroma.scale('RdYlGn').domain([0, 100]).classes(5)
Expand Down Expand Up @@ -147,4 +154,5 @@ vows
f: chroma.scale(['red', 'white', 'blue'])
'same colors': (topic) -> assert.deepEqual topic.f.colors(), ['#ff0000', '#ffffff', '#0000ff']


.export(module)

0 comments on commit d77c2d4

Please sign in to comment.