-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathpalettes.php
381 lines (354 loc) · 10.5 KB
/
palettes.php
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!doctype html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>iWantHue - Palettes Generator</title>
<?php include("includes/codetop.php"); ?>
<style>
#hs_div, #sl_div, #hl_div, #sv_div, #hv_div, #rg_div, #gb_div, #rb_div{
max-width: 300px;
}
.generator button{
width: 100%;
height: 30px;
}
#palette{
line-height: 60px;
text-shadow: -1px -1px #FFF, 1px 1px #333;
}
#colorspace{
color:#FFF;
font-size: 9px;
word-wrap: break-word;
}
#colorspace span{
margin-right: 1px;
}
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
</head>
<body>
<?php include("includes/header.php"); ?>
<div class="container">
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="splash-unit row">
<div class="span7">
<div class="image">
<a href="index.php"><img src="res/header.png"/></a>
</div>
<div class="title">
<small class="muted">(old version)</small>
</div>
</div>
<div class="span5">
<br/>
<br/>
<div class="alert alert-error">
<strong>Outdated version!</strong> Check out our fresh new interface
</div>
<a class="btn btn-primary" href="index.php"><i class="icon-white icon-arrow-right"></i> Current version</a>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="span12">
<h2>Palettes Generator</h2>
<p>
Choose how much colors you want, choose a preset or use custom settings, and generate your palette. The palette is generated by clustering a color space and ordering the resulting colors.
</p>
</div>
</div>
<div class="row">
<div class="span4">
<h3>Presets</h3>
<p>
<select id="presets" onchange="updateSettings()">
</select>
<button onclick="generate();">Generate Palette</button>
</p>
<h3>Palette Size</h3>
<p>
<input id="nodescount" type="text" value="10" style="width:35px;"/> Colors
</p>
<h3>Settings</h3>
<p>
<i>These settings apply on the HCL color space, as described in the Chroma.js library. Note that the CIE L*a*b* color space is nevertheless used for the computations.</i>
</p>
<p>
<b>Hue</b> (range: 0 to 360)
<br/>
<input id="hmin" type="text" value="0" style="width:35px;"/> Minimum
<br/>
<input id="hmax" type="text" value="360" style="width:35px;"/> Maximum
</p>
<p>
<b>Chroma</b> (range: 0 to 10, most of the values under 1)
<br/>
<input id="cmin" type="text" value="0" style="width:35px;"/> Minimum
<br/>
<input id="cmax" type="text" value="10" style="width:35px;"/> Maximum
</p>
<p>
<b>Lightness</b> (range: 0 to 10, most of the values under 1)
<br/>
<input id="lmin" type="text" value="0" style="width:35px;"/> Minimum
<br/>
<input id="lmax" type="text" value="10" style="width:35px;"/> Maximum
</p>
<p>
<b>Quality</b> (more is slower, minimum 1, default 50)
<br/>
<input id="q" type="text" value="50" style="width:35px;"/>
</p>
<p>
<b>Algorithm</b>
<br/>
<select id="algo">
<option value="kmeans" selected=true>k-Means</option>
<option value="forcevector" >Force Vector</option>
</select>
</p>
<button onclick="generate();">Generate Palette</button>
<h3 id="colorspace_title">Sub-space</h3>
<div id="colorspace"></div>
<h3>How it works</h3>
<p>
The color above are obtained by a regular sampling of the CIE L*a*b* color space. Not filtered, the 1782 colors represent the full range of colors that the human eye is able to see.
</p>
<p>
The settings allow you to restrain the color space. Each of these 1782 are tested to know if they fit to some criteria. This criteria is expressed in the HCL color space popularized by Chroma.js.
<br/>
The HCL color space fits better the human vision than HSV or HSL, but is easy to understand, contrary to L*a*b* (since Hue is a separate parameter).
</p>
<p>
We use an algorithm (k-Means or Force Vector) to place colors, in this restrained color space, in a way that they are the most distant possible - "distant" in the sense of the distance in the L*a*b* color space.
</p>
<p>
More about this technique <a href="experiment.php">there</a>.
</p>
</div>
<div class="span6">
<h2>Palette</h2>
<button onclick="generate();">Generate Palette</button>
<div id="palette"></div>
<h3>Hex colors</h3>
<textarea id="output_hexjson" style="width:100%;height:100px;">
</textarea>
<h3>Generation Code (javascript)</h3>
<textarea id="output_code" style="width:100%;height:100px;">
</textarea>
<br/>
<i>NB: requires <a href="https://github.com/gka/chroma.js">Chroma.js</a> and our <a href="js/libs/chroma.palette-gen.js" target="_blank">chroma.palette-gen.js</a></i>
</div>
</div>
</div>
<?php include("includes/footer.php"); ?>
</div> <!--! end of #container -->
<?php include("includes/codebottom.php"); ?>
<script>
// Generators data
var generators = [
{
name: "Full Range - Soft",
hclrange:[[0,360],[0,10],[0,10]],
q:50,
algo:'kmeans'
},
{
name: "Full Range - Radical",
hclrange:[[0,360],[0,10],[0,10]],
q:50,
algo:'forcevector'
},
{
name: "Pastel",
hclrange:[[0,360],[0,0.9],[1,10]],
q:50,
algo:'kmeans'
},
{
name: "Fancy",
hclrange:[[0,360],[0.6,1.2],[0.8,10]],
q:50,
algo:'kmeans'
},
{
name: "Pimp",
hclrange:[[0,360],[0.9,10],[0.4,10]],
q:50,
algo:'kmeans'
},
{
name: "Tarnish",
hclrange:[[0,360],[0,0.6],[0.4,1.1]],
q:50,
algo:'kmeans'
},
{
name: "Intense",
hclrange:[[0,360],[0.6,10],[0.2,1.1]],
q:50,
algo:'kmeans'
},
{
name: "Shades",
hclrange:[[0,360],[0,0.4],[0,10]],
q:50,
algo:'kmeans'
},
{
name: "Red Berries",
hclrange:[[330,20],[0.3,10],[0.5,10]],
q:50,
algo:'forcevector'
},
{
name: "Ochre",
hclrange:[[20,60],[0.3,10],[0.5,10]],
q:50,
algo:'forcevector'
},
{
name: "Lime",
hclrange:[[60,90],[0.3,10],[0.5,10]],
q:50,
algo:'forcevector'
},
{
name: "Mint",
hclrange:[[90,150],[0.3,10],[0.5,10]],
q:50,
algo:'forcevector'
},
{
name: "Ice Cube",
hclrange:[[150,200],[0.3,10],[0.5,10]],
q:50,
algo:'forcevector'
},
{
name: "Ocean",
hclrange:[[220,260],[0.3,10],[0.5,10]],
q:50,
algo:'forcevector'
},
{
name: "Indigo",
hclrange:[[260,290],[0.3,10],[0.5,10]],
q:50,
algo:'forcevector'
},
{
name: "Pink Wine",
hclrange:[[290,330],[0.3,10],[0.5,10]],
q:50,
algo:'forcevector'
}
];
// Init generators
$(document).ready(function(){
generators.forEach(function(generator, i){
$('#presets').append('<option value="'+i+'">'+generator.name+'</option>');
});
})
// Update Settings
function updateSettings(){
var generator = generators[$('#presets').val()];
$('#hmin').val(generator.hclrange[0][0]);
$('#hmax').val(generator.hclrange[0][1]);
$('#cmin').val(generator.hclrange[1][0]);
$('#cmax').val(generator.hclrange[1][1]);
$('#lmin').val(generator.hclrange[2][0]);
$('#lmax').val(generator.hclrange[2][1]);
$('#q').val(generator.q);
$('#algo').val(generator.algo);
}
// Generate colors
function generate(){
// Variables
var hmin = +$('#hmin').val();
var hmax = +$('#hmax').val();
var cmin = +$('#cmin').val();
var cmax = +$('#cmax').val();
var lmin = +$('#lmin').val();
var lmax = +$('#lmax').val();
var q = +$('#q').val();
var useFV = ($('#algo').val()=="forcevector");
// Conditions restraining the color space
var hcondition;
var hcondition_txt;
if(hmin<hmax){
hcondition = function(hcl){return hcl[0]>=hmin && hcl[0]<=hmax};
hcondition_txt = "hcl[0]>="+hmin+" && hcl[0]<="+hmax;
} else {
hcondition = function(hcl){return hcl[0]>=hmin || hcl[0]<=hmax};
hcondition_txt = "(hcl[0]>="+hmin+" || hcl[0]<="+hmax+")";
}
var ccondition = function(hcl){return hcl[1]>=cmin && hcl[1]<=cmax};
var ccondition_txt = "hcl[1]>="+cmin+" && hcl[1]<="+cmax;
var lcondition = function(hcl){return hcl[2]>=lmin && hcl[2]<=lmax};
var lcondition_txt = "hcl[2]>="+lmin+" && hcl[2]<="+lmax;
// General condition for selecting the color space
var colorspaceSelector = function(color){
var hcl = color.hcl();
return hcondition(hcl) && ccondition(hcl) && lcondition(hcl);
}
// Sample the color space (for monitoring)
var subspaceSamples = [];
for(l=0; l<=1; l+=0.05){
for(a=-1; a<=1; a+=0.1){
for(b=-1; b<=1; b+=0.1){
var color = chroma.lab(l, a, b);
// Test if the color exists in the RGB color space (there are holes in the CIE Labs space)
if(!isNaN(color.rgb[0]) && color.rgb[0]>=0 && color.rgb[1]>=0 && color.rgb[2]>=0 && color.rgb[0]<256 && color.rgb[1]<256 && color.rgb[2]<256){
// Test if the color is in the specified subspace
if(colorspaceSelector(color)){
subspaceSamples.push(color);
}
}
}
}
}
$('#colorspace_title').text("Sub-space ("+subspaceSamples.length+" color samples)");
$('#colorspace').html(subspaceSamples.map(function(color){
return "<span style='width: 8px;background-color:"+color.hex()+";'>_</span>";
}).join(""));
// Generate colors
var colors = paletteGenerator.generate(parseInt($('#nodescount').val()), colorspaceSelector, useFV, q);
colors = paletteGenerator.diffSort(colors);
updateViz(colors);
// Code block
code = "// Generate colors (as Chroma.js objects)"
+"\nvar colors = paletteGenerator.generate("+parseInt($('#nodescount').val())+", function(color){"
+"\n\tvar hcl = color.hcl();"
+"\n\treturn "+hcondition_txt+" && "+ccondition_txt+" && "+lcondition_txt+";"
+"\n},\n"+(useFV.toString())+", // Using Force Vector instead of k-Means\n"+q+" // Steps (quality)\n);"
+"\n// Sort colors by differenciation first"
+"\ncolors = paletteGenerator.diffSort(colors);";
$('#output_code').val(code);
}
// Update the visualization with a given palette
function updateViz(colors){
$('#palette').empty();
colors.forEach(function(color){
$('#palette').append('<span style="border: 20px solid '+color.hex()+'; color:'+color.hex()+'; background-color:'+color.hex()+';">'+color.hex()+'</span> ');
});
$('#output_hexjson').html(
"[\n"+
colors.map(function(color){
return "\t\""+color.hex()+"\"";
}).join(",\n")
+"\n]"
);
}
</script>
</body>
</html>