-
Notifications
You must be signed in to change notification settings - Fork 2
/
category-colors-2L-inplace.html
271 lines (234 loc) · 7.95 KB
/
category-colors-2L-inplace.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!DOCTYPE html>
<meta charset="utf-8">
<!--
License: Public Domain.
Original Author: Jonathan Newnham
-->
<style>
.axis {
stroke: #999;
stroke-opacity: .5;
fill-opacity: 0;
stroke-width: 1;
}
.mo {
font-family: Consolas, Inconsolata, "Courier New", Courier, monospace;
}
p { max-width: 40em; }
.invalid { color: red; }
</style>
<body>
<h1 style="display:inline">Category Color Generator</h1>
<div></div>
<div id="controls" style="float:right; padding:1em">
<form id="fc" onsubmit="force.start(); return false;" style="display:inline">
L1:<input id="lrange" type="range" min="0" max="100" value="70" step="1" oninput="initcolors();" />
<br />
L2:<input id="lrange2" type="range" min="0" max="100" value="45" step="1" oninput="initcolors();" />
<br />
Grid:<input id="gridstep" type="range" min="6" max="60" value="12" step="1" oninput="initcolors();" />
<br />
<input type="button" value="Reset" onclick="initcolors();"/>
<input type="button" value="Start" onclick="startsort();"/>
<input type="button" value="Pause" onclick="sorting=false;"/>
</form>
<div id="status" class="mo"></div>
</div>
<svg id="graph" style="display:inline"></svg>
<div id="sortedlist" class="mo"></div>
<script src="d3.v3.min.js"></script>
<script>
var width = 800;
var height = 800;
var svg = d3.select("svg#graph")
.attr("width", width)
.attr("height", height);
var layer0 = svg.append("g").attr("id", "layer0"); // for background stuff (link paths, text)
var layer1 = svg.append("g").attr("id", "layer1"); // for background stuff (link paths, text)
var x0 = width / 2;
var y0 = height / 2;
var sorting = false;
var scalefactor = 3; // distance on screen vs. change in color
var mindist = 0; // distance between adjacent color points
layer0.selectAll(".backrect").data([0]).enter().append("rect")
.classed("backgrect", true)
.attr("height", height)
.attr("width", width)
.style("fill", "white");
layer0.selectAll(".axis").data([
[[0, height/2],[width, height/2]],
[[width/2, 0],[width/2, height]],
])
.enter().append("path")
.attr("d", function(d) { return d3.svg.line().interpolate("monotone")(d); })
.style("stroke-width", "1")
.classed("axis", true);
var ds = []; // data array: list of (x,y) pairs and d3js internals
var d_sorteds = [];
function validcolor(lab) {
var rugub = lab.rgb();
return ((0 < rugub.r) && (rugub.r < 256)
&& (0 < rugub.g) && (rugub.g < 256)
&& (0 < rugub.b) && (rugub.b < 256));
}
function coord_to_labcolor(x, y, L) {
return d3.lab(L, (x-x0)/scalefactor, (y0-y)/scalefactor);
}
function initcolors() {
sorting = false;
var L1 = document.getElementById("lrange").value;
var L2 = document.getElementById("lrange2").value;
document.getElementById("sortedlist").innerHTML = "";
var gridStep = parseInt(document.getElementById("gridstep").value);
mindist = gridStep/scalefactor;
document.getElementById("status").innerHTML = "L1=" + L1 + " L2=" + L2 + "<br />Gridstep=" + gridStep;
ds = [];
d_sorteds = [];
for (x = 0; x < width; x+=gridStep) {
for (y = 0; y < height; y+=gridStep) {
var lab = coord_to_labcolor(x, y, L1);
if (validcolor(lab)) {
ds.push({
lab: lab,
x: x,
y: y,
r: 5
});
}
var lab = coord_to_labcolor(x, y, L2);
if (validcolor(lab)) {
ds.push({
lab: lab,
x: x,
y: y,
r: 3
});
}
}
}
layer1.selectAll(".posnode").remove();
var nodes = layer1.selectAll(".posnode")
.data(ds)
.enter().append("circle")
.classed("posnode", true)
.attr("cx", 0)
.attr("cy", 0)
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return d.lab; })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
function startsort() {
sorting = true;
if (0 === d_sorteds.length) {
layer1.selectAll(".posnode")
.transition()
.style("opacity", 0);
}
sortcolors();
}
// Order colours by greatest distance from all other selected colors.
function sortcolors() {
// faster ways of doing this include
// - a quadtree of d_sorteds for the nearest-neighbour search
var d_new = find_distant_node();
if (!sorting)
return;
if(d_new.nearest2 > 0)
{
d_new.sorted = true;
var i = d_sorteds.length;
d_sorteds.push(d_new);
move_sorted_node(d_new, i);
add_to_sorted_list(d_new.lab);
setTimeout(sortcolors, 500 * d3.ease("quad")(1-i/50));
}
}
function add_to_sorted_list(lab)
{
document.getElementById("sortedlist").innerHTML += "\""+lab.rgb()+"\", ";
document.getElementById("status").innerHTML = "Sorted: "+d_sorteds.length
+"<br />Distance: "+Math.floor(Math.sqrt(d_sorteds[d_sorteds.length-1].nearest2))
+"<br />(Minimum: "+Math.floor(mindist) + ")"
}
// find the node that is furthest away from all the currently selected (sorted) nodes.
function find_distant_node()
{
// should really be using an octree of d_sorted here, this is pretty slow
ds.forEach(function(d) {
if (d.sorted) {
d.nearest2 = 0;
return;
}
d.nearest2 = 1000000;
var sq = function(x) { return x * x; }
d_sorteds.forEach(function(d_sorted) {
dist = (sq(d.lab.a - d_sorted.lab.a)
+ sq(d.lab.b - d_sorted.lab.b)
+ sq(d.lab.l - d_sorted.lab.l));
if (dist < d.nearest2)
d.nearest2 = dist; // square root not necessary (sqrt(a) < sqrt(b) == a < b)
});
});
ds.sort(function(d1, d2) { return d2.nearest2 - d1.nearest2;});
return ds[0];
}
// pretty transition -- move to new sorted location.
function move_sorted_node(d_selected, i)
{
var element = d3.selectAll(".posnode")
.filter(function(d) { return d == d_selected; })
.transition()
.duration(1000)
.style("opacity", 1);
}
layer0.on("mousemove", function() {
var x = d3.mouse(this)[0];
var y = d3.mouse(this)[1];
var lab = coord_to_labcolor(x, y, document.getElementById("lrange").value);
var rgb = lab.rgb();
var status = "x=" + x + ", y=" + y;
status += "<br />";
status += "L=" + lab.l + ", a=" + Math.floor(lab.a) + ", b=" + Math.floor(lab.b);
status += "<br />";
status += "R="+ printColorValue(rgb.r) + " G=" + printColorValue(rgb.g) + " B=" + printColorValue(rgb.b);
document.getElementById("status").innerHTML = status;
});
function printColorValue(v) {
if (v < 0 || v > 255)
return "<span class=\"invalid\">" + v + "</span>";
else
return "" + v
}
initcolors();
</script>
<div id="about" style="display:inline">
<h3>About</h3>
2014-08-04
<p>
Generate a continuous sequence of distinct random colours for diagrams.
</p><p>
Sampling is done in the Lab color space so that perceptually different colours are generated for the given lightnesses.
</p><p>
Further work:
Instead of taking flat slices of lightness, take all samples to be the same distance (in Lab-space) from a given point (i.e. the background color). This should give a more consistent and pleasing amount of contrast.
</p><p>
Update 2015-09-24: advanced version implemented (<a href="category-colors-constrained.html">here</a>).
</p>
</p><p>
Bibliography:
<ol><li>
<a href="http://bl.ocks.org/mbostock/310c99e53880faec2434">bl.ocks.org/mbostock/310c99e53880faec2434</a><br />
For introducing the idea of perceptual color
</li><li>
<a href="http://tools.medialab.sciences-po.fr/iwanthue/theory.php">medialab: iwanthue</a><br />
Generating fixed-size palettes of optimal colours using the LAB colour space.
Spacing done with repulsive forces or kNN clustering.
</li><li>
<a href="http://www.colorcodehex.com/color-model.html">colorcodehex</a> and <a href="http://en.wikipedia.org/wiki/Lab_color_space">Lab color space</a> for the description of the LAB color space.
</li><li>
<a href="http://en.wikipedia.org/wiki/Low-discrepancy_sequence">Low-discrepancy sequence</a>
Methods for nice multidimensional sampling
</li>
</ol>
</p>
</div>