Skip to content

Instantly share code, notes, and snippets.

@nitaku
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitaku/34eaba160faac9c8c6c2 to your computer and use it in GitHub Desktop.
Save nitaku/34eaba160faac9c8c6c2 to your computer and use it in GitHub Desktop.
2 multihue diverging color scales
height = 468
N = 2
palettes = [
d3.range(128).map( (i) ->
return d3.hcl(-90+120*i/128, 25-Math.pow(64-i,2)/200, 10+90*i/128)
).concat( d3.range(128).map( (i) ->
return d3.hcl(90+120*(128-i)/128, 25-Math.pow(64-i,2)/200, 10+90*i/128)
).reverse() ),
d3.range(128).map( (i) ->
return d3.hcl(90-90+120*i/128, 25-Math.pow(64-i,2)/200, 10+90*i/128)
).concat( d3.range(128).map( (i) ->
return d3.hcl(90+90+120*(128-i)/128, 25-Math.pow(64-i,2)/200, 10+90*i/128)
).reverse() )
]
svg = d3.select('svg')
bands = svg.selectAll('.palette')
.data(palettes)
.enter().append('g')
.attr
class: 'palette'
transform: (d,n) -> "translate(0,#{height/N*n})"
bands.selectAll('.band')
.data((palette) -> palette)
.enter().append('rect')
.attr
class: 'band'
x: (d,i) -> 32+3.5*i
y: 21
width: 3.5
height: height/N-10
fill: (d) -> d
body {
background: #272822;
margin: 0;
padding: 0;
}
svg {
background: white;
}
.label {
text-anchor: middle;
font-size: 24px;
}
rect {
shape-rendering: crispEdges;
}
path {
fill: none;
}
path.hue {
stroke: white;
stroke-width: 1;
}
path.chroma {
stroke: #FF3D00;
stroke-width: 1;
}
path.luminance {
stroke: black;
stroke-width: 0.6;
}
.axis .domain, .axis .tick line {
stroke: black;
shape-rendering: crispEdges;
}
.axis {
font-size: 8px;
font-family: sans-serif;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="2 multihue diverging color scales" />
<title>2 multihue diverging color scales</title>
<link rel="stylesheet" href="index.css">
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<svg height="500" width="960"></svg>
<script src="index.js"></script>
</body>
</html>
(function() {
var N, bands, height, palettes, svg;
height = 468;
N = 2;
palettes = [
d3.range(128).map(function(i) {
return d3.hcl(-90 + 120 * i / 128, 25 - Math.pow(64 - i, 2) / 200, 10 + 90 * i / 128);
}).concat(d3.range(128).map(function(i) {
return d3.hcl(90 + 120 * (128 - i) / 128, 25 - Math.pow(64 - i, 2) / 200, 10 + 90 * i / 128);
}).reverse()), d3.range(128).map(function(i) {
return d3.hcl(90 - 90 + 120 * i / 128, 25 - Math.pow(64 - i, 2) / 200, 10 + 90 * i / 128);
}).concat(d3.range(128).map(function(i) {
return d3.hcl(90 + 90 + 120 * (128 - i) / 128, 25 - Math.pow(64 - i, 2) / 200, 10 + 90 * i / 128);
}).reverse())
];
svg = d3.select('svg');
bands = svg.selectAll('.palette').data(palettes).enter().append('g').attr({
"class": 'palette',
transform: function(d, n) {
return "translate(0," + (height / N * n) + ")";
}
});
bands.selectAll('.band').data(function(palette) {
return palette;
}).enter().append('rect').attr({
"class": 'band',
x: function(d, i) {
return 32 + 3.5 * i;
},
y: 21,
width: 3.5,
height: height / N - 10,
fill: function(d) {
return d;
}
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment