-
Notifications
You must be signed in to change notification settings - Fork 14
/
color.html
354 lines (314 loc) · 12.4 KB
/
color.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
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
<!DOCTYPE html>
<html>
<title>giscloud jsapi color example</title>
<!--<link rel="stylesheet" type="text/css" href="reset.css"/>-->
<link rel="stylesheet" href="farbtastic.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="examples.css"/>
<link rel="stylesheet" type="text/css" href="colors.css"/>
<script type="text/javascript" src="https://api.giscloud.com/1/api.js"></script>
<script type="text/javascript" src="examples.js"></script>
<script type="text/javascript">
var c1, c2, cp1, cp2, nr, mode;
giscloud.ready(function() {
window.$ = window.jQuery = giscloud.exposeJQuery();
c1 = new giscloud.Color.fromHsl(0, 100, 50);
c2 = new giscloud.Color.fromHsl(120, 100, 50);
nr = 5;
// create colorpickers
giscloud.includeJs("farbtastic.js").done(function () {
$("#colInput1").html(c1.hex());
cp1 = $.farbtastic("#colorpicker1", function (colString) {
c1.hex(colString);
$("#colInput1").html(colString);
refresh();
});
cp1.setColor(c1.hex());
$("#colInput2").html(c2.hex());
cp2 = $.farbtastic("#colorpicker2", function (colString) {
c2.hex(colString);
$("#colInput2").html(colString);
refresh();
});
cp2.setColor(c2.hex());
$("#colInput1").click(function (evt) {
$("#colorpicker1").toggle("fast");
$("#colorpicker2").hide();
evt.stopPropagation();
});
$("#colInput2").click(function (evt) {
$("#colorpicker2").toggle("fast");
$("#colorpicker1").hide();
evt.stopPropagation();
});
$(document).click(function () {
$("#colorpicker1,#colorpicker2").hide();
});
$("#colorpicker1,#colorpicker2").click(function (evt) {
evt.stopPropagation();
});
$(document).keyup(function(e) {
if (e.keyCode === 27) {
$("#colorpicker1,#colorpicker2").hide();
}
});
});
mode = $("#rangeMode").val();
refresh();
$("input").change(onchange);
$("#rangeMode").change(onchange);
});
// refreshes all the values
function refresh() {
var rgb1, rgb2, hsv1, hsv2, hsl1, hsl2;
rgb1 = c1.rgb();
rgb2 = c2.rgb();
hsv1 = c1.hsv(true);
hsv2 = c2.hsv(true);
hsl1 = c1.hsl(true);
hsl2 = c2.hsl(true);
$("input").each(function () {
switch (this.id) {
// RGB
case "r1":
this.value = rgb1[0];
break;
case "g1":
this.value = rgb1[1];
break;
case "b1":
this.value = rgb1[2];
break;
case "r2":
this.value = rgb2[0];
break;
case "g2":
this.value = rgb2[1];
break;
case "b2":
this.value = rgb2[2];
break;
// HSV
case "hv1":
this.value = hsv1[0];
break;
case "sv1":
this.value = hsv1[1];
break;
case "v1":
this.value = hsv1[2];
break;
case "hv2":
this.value = hsv2[0];
break;
case "sv2":
this.value = hsv2[1];
break;
case "v2":
this.value = hsv2[2];
break;
// HSL
case "hl1":
this.value = hsl1[0];
break;
case "sl1":
this.value = hsl1[1];
break;
case "l1":
this.value = hsl1[2];
break;
case "hl2":
this.value = hsl2[0];
break;
case "sl2":
this.value = hsl2[1];
break;
case "l2":
this.value = hsl2[2];
break;
// NR
case "nrOfSwatches":
this.value = nr;
break;
}
});
swatches();
}
// reacts to changes in color values
function onchange() {
var id = this.id, v = parseInt(this.value, 10), o, r, m, c;
if (id === undefined) {
return;
} else if (id === "rangeMode") {
mode = this.value;
refresh();
return;
}
// check for bad input
if (v === null || isNaN(v) || v < 0) {
o = $(this);
if (!o.hasClass("err")) {
o.addClass("err");
}
return;
} else {
o = $(this);
if (o.hasClass("err")) {
o.removeClass("err");
}
}
// find out what changed
r = /((((r)|(g)|(b))|((hl)|(sl)|(l))|((hv)|(sv)|(v)))([12]))|(nrOfSwatches)/;
m = id.match(r);
if (m) {
if (m[16]) { // nr
nr = v;
} else {
// find out color1 or color2
if (m[15] == "1") {
c = c1;
} else {
c = c2;
}
// find out color mode and component and change color
if (m[3] && m[4]) { // r
c.rgb(v, null, null);
} else if (m[3] && m[5]) { // g
c.rgb(null, v, null);
} else if (m[3] && m[6]) { // b
c.rgb(null, null, v);
} else if (m[7] && m[8]) { // h hsl
c.hsl(v, null, null);
} else if (m[7] && m[9]) { // s hsl
c.hsl(null, v, null);
} else if (m[7] && m[10]) { // l hsl
c.hsl(null, null, v);
} else if (m[11] && m[12]) { // h hsv
c.hsv(v, null, null);
} else if (m[11] && m[13]) { // s hsv
c.hsv(null, v, null);
} else if (m[11] && m[14]) { // v hsv
c.hsv(null, null, v);
}
}
// set colorpicker value
if (m[15] == "1") {
cp1.setColor(c.hex());
} else if (m[15] == "2") {
cp2.setColor(c.hex());
}
// refresh all the values
refresh();
}
}
// creates the color swatches on the colorbar
function swatches() {
var r, cbar, w, i, hex, d;
cbar = $("#colorbar");
if (!cbar || cbar.length === 0) {
return;
}
// create the range of colors
r = giscloud.Color.range(c1, c2, nr, mode);
// calc the width of each swatch (except the last one)
w = Math.round(parseInt(cbar.width(), 10) / nr);
// clear colorbar
cbar.html("");
// generate swatches
w = (100 / nr) + "%";
for (i = 0; i < nr; i++) {
hex = r[i].hex();
d = $("<div/>", {
id: "swatch" + hex.replace(/#/, "_"),
title: hex,
width: w,
css: { "background-color": hex, "border-color": r[i].darker().hex()}
});
d.append(" ");
cbar.append(d);
}
}
</script>
<body>
<h1>Colors</h1>
<div id="example">
<form id="colorForm" onsubmit="return false;">
<div id="col1">
<table>
<thead>
<tr>
<th colspan="2">Color 1</th>
<th colspan="4"><span id="colInput1"></span></th>
</tr>
</thead>
<tbody>
<tr>
<td>Red:</td><td><input id="r1" type="text" maxlength="3"/></td>
<td>Hue:</td><td><input id="hl1" type="text" maxlength="3"/></td>
<td>Hue:</td><td><input id="hv1" type="text" maxlength="3"/></td>
</tr>
<tr>
<td>Green:</td><td><input id="g1" type="text" maxlength="3"/></td>
<td>Sat:</td><td><input id="sl1" type="text" maxlength="3"/></td>
<td>Sat:</td><td><input id="sv1" type="text" maxlength="3"/></td>
</tr>
<tr>
<td>Blue:</td><td><input id="b1" type="text" maxlength="3"/></td>
<td>Ligh:</td><td><input id="l1" type="text" maxlength="3"/></td>
<td>Val:</td><td><input id="v1" type="text" maxlength="3"/></td>
</tr>
</tbody>
</table>
</div>
<div id="col2">
<table>
<thead>
<tr>
<th colspan="2">Color 2</th>
<th colspan="4"><span id="colInput2"></span></th>
</tr>
</thead>
<tbody>
<tr>
<td>Red:</td><td><input id="r2" type="text" maxlength="3"/></td>
<td>Hue:</td><td><input id="hl2" type="text" maxlength="3"/></td>
<td>Hue:</td><td><input id="hv2" type="text" maxlength="3"/></td>
</tr>
<tr>
<td>Green:</td><td><input id="g2" type="text" maxlength="3"/></td>
<td>Sat:</td><td><input id="sl2" type="text" maxlength="3"/></td>
<td>Sat:</td><td><input id="sv2" type="text" maxlength="3"/></td>
</tr>
<tr>
<td>Blue:</td><td><input id="b2" type="text" maxlength="3"/></td>
<td>Ligh:</td><td><input id="l2" type="text" maxlength="3"/></td>
<td>Val:</td><td><input id="v2" type="text" maxlength="3"/></td>
</tr>
</tbody>
</table>
</div>
<div id="controls">
Nr of colors: <input id="nrOfSwatches" type="text" maxlength="2"/>
Range mode: <select id="rangeMode">
<option value="hsl cw" selected="true">HSL CW</option>
<option value="hsl ccw">HSL CCW</option>
<option value="rgb">RGB</option>
<option value="hsv cw">HSV CW</option>
<option value="hsv ccw">HSV CCW</option>
</select>
</div>
<!-- colorpickers -->
<div id="colorpicker1" class="hide"></div>
<div id="colorpicker2" class="hide"></div>
</form>
<div id="colorbar"></div>
</div>
<div id="ft">
<p>
This example uses the
<a href="http://acko.net/dev/farbtastic" target="_blank" title="Farbtastic by Steven Wittens - acko.net">
Farbtastic jQuery colorpicker plugin
</a> made by Steven Wittens.
</p>
</div>
</body>
</html>