-
Notifications
You must be signed in to change notification settings - Fork 28
/
colorwheel.js
executable file
·342 lines (287 loc) · 9.01 KB
/
colorwheel.js
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
/*
* Colorwheel
* Copyright (c) 2010 John Weir (http://famedriver.com)
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*
* requires jQuery & Raphael
* http://jquery.com http://raphaeljs.com
*
* see http://jweir.github.com/colorwheel for Usage
*
*/
Raphael.colorwheel = function(target, color_wheel_size, no_segments){
var canvas,
current_color,
current_color_hsb,
size,
segments = no_segments || 60,
bs_square = {},
hue_ring = {},
tri_size,
cursor = {},
drag_target,
input_target,
center,
parent,
change_callback,
drag_callbacks = [function(){}, function(){}],
offset,
padding = 2,
sdim; // holds the dimensions for the saturation square
function point(x, y){ return {x:x, y:y};}
function radians(a){ return a * (Math.PI/180);}
function angle(x,y){
var q = x > 0 ? 0 : 180;
return q+Math.atan((0 - y)/(0 - x))*180/(Math.PI);
}
function create(target, color_wheel_size){
size = color_wheel_size;
tri_size = size/20;
center = size/2;
parent = $(target);
canvas = Raphael(parent[0],size, size);
canvas.safari();
create_bs_square();
create_hue_ring();
hue_ring.cursor = cursor_create(tri_size);
bs_square.cursor = cursor_create(tri_size*0.5);
events_setup();
parent.css({height:size+"px", width:size+"px"});
disable_select(parent);
return public_methods();
}
function disable_select(target){
$(target).css({"unselectable": "on","-moz-user-select": "none","-webkit-user-select": "none"});
}
function public_methods(){
return {
input: input,
onchange: onchange,
ondrag : ondrag,
color : public_set_color,
color_hsb : public_set_color_hsb
};
}
// Sets a textfield for user input of hex color values
// TODO don't clear the change callback
// TODO allow a null target to unbind the input
function input(target){
change_callback = null;
input_target = target;
$(target).keyup(function(){
if(this.value.match(/^#([0-9A-F]){3}$|^#([0-9A-F]){6}$/img)){
set_color(this.value);
update_color(true);
run_onchange_event();
}
});
set_color(target.value);
update_color(true);
return public_methods();
}
function onchange(callback){
change_callback = callback;
update_color(false);
return public_methods();
}
function ondrag(start_callback, end_callback){
drag_callbacks = [start_callback || function(){}, end_callback || function(){}];
return public_methods();
}
function drag(e){
var x, y, page;
e.preventDefault(); // prevents scrolling on touch
page = e.originalEvent.touches ? e.originalEvent.touches[0] : e;
x = page.pageX - (parent.offset().left + center);
y = page.pageY - (parent.offset().top + center);
if(drag_target == hue_ring){
set_hue_cursor(x,y);
update_color();
run_onchange_event();
return true;
}
if(drag_target == bs_square){
set_bs_cursor(x,y);
update_color();
run_onchange_event();
return true;
}
}
function start_drag(event, target){
event.preventDefault(); // prevents scrolling on touch
$(document).on('mouseup touchend',stop_drag);
$(document).on('mousemove touchmove',drag);
drag_target = target;
drag(event);
drag_callbacks[0](current_color);
}
function stop_drag(event){
event.preventDefault(); // prevents scrolling on touch
$(document).off("mouseup touchend",stop_drag);
$(document).off("mousemove touchmove",drag);
drag_callbacks[1](current_color);
run_onchange_event();
}
function events_setup(){
$([hue_ring.event.node,hue_ring.cursor[0].node]).on("mousedown touchstart",
function(e){start_drag(e,hue_ring);});
$([bs_square.b.node, bs_square.cursor[0].node]).on("mousedown touchstart",
function(e){start_drag(e,bs_square);});
}
function cursor_create(size){
var set = canvas.set().push(
canvas.circle(0, 0, size).attr({"stroke-width":4, stroke:"#333"}),
canvas.circle(0, 0, size+2).attr({"stroke-width":1, stroke:"#FFF", opacity:0.5})
);
set[0].node.style.cursor = "crosshair";
return set;
}
function set_bs_cursor(x,y){
x = x+center;
y = y+center;
if(x < sdim.x){x = sdim.x}
if(x > sdim.x+sdim.l){x = sdim.x+sdim.l}
if(y < sdim.y){y = sdim.y}
if(y > sdim.y+sdim.l){y = sdim.y + sdim.l}
bs_square.cursor.attr({cx:x, cy:y}).transform("t0,0");
}
function set_hue(color){
var hex = Raphael.getRGB(color).hex;
bs_square.h.attr("fill", hex);
}
function hue(){
return Raphael.rgb2hsb(bs_square.h.attr("fill")).h;
}
function public_set_color(value){
var ret = set_color(value, false);
update_color(false);
return ret;
}
function public_set_color_hsb(hsb){
var ret = set_color(hsb, true);
update_color(false);
return ret;
}
function set_color(value, is_hsb){
if(value === undefined){
if(is_hsb){
return current_color_hsb;
} else {
return current_color;
}
}
var hsb, hex;
if(is_hsb){
hsb = value;
// Allow v (value) instead of b (brightness), as v is sometimes
// used by Raphael.
if(hsb.b === undefined){ hsb.b = hsb.v; }
var rgb = canvas.raphael.hsb2rgb(hsb.h, hsb.s, hsb.b);
hex = rgb.hex;
} else {
hex = value;
hsb = canvas.raphael.rgb2hsb(hex);
}
var temp = canvas.rect(1,1,1,1).attr({fill:hex});
set_bs_cursor(
(0-sdim.l/2) + (sdim.l*hsb.s),
sdim.l/2 - (sdim.l*hsb.b));
set_hue_cursor((360*(hsb.h))-90);
temp.remove();
return public_methods();
}
// Could optimize this method
function update_color(dont_replace_input_value){
var x = bs_square.cursor.items[0].attr("cx"),
y = bs_square.cursor.items[0].attr("cy"),
hsb = {
b: 1-(y-sdim.y)/sdim.l,
s: (x-sdim.x)/sdim.l,
h: hue()
};
current_color_hsb = hsb;
current_color = Raphael.hsb2rgb(hsb.h, hsb.s,hsb.b);
if(input_target){
var c = current_color.hex;
if(dont_replace_input_value !== true) { input_target.value = c;}
if(hsb.b < 0.5){
$(input_target).css("color", "#FFF");
} else {
$(input_target).css("color", "#000");
}
input_target.style.background = c;
}
}
// accepts either x,y or d (degrees)
function set_hue_cursor(mixed_args){
var d;
if(arguments.length == 2){
d = angle(arguments[0],arguments[1]);
} else {
d = arguments[0];
}
var x = Math.cos(radians(d)) * (center-tri_size-padding);
var y = Math.sin(radians(d)) * (center-tri_size-padding);
hue_ring.cursor.attr({cx:x+center, cy:y+center}).transform("t0,0");
set_hue("hsb("+(d+90)/360+",1,1)");
}
function bs_square_dim(){
if(sdim){ return sdim;}
var s = size - (tri_size * 4);
sdim = {
x:(s/6)+tri_size*2+padding,
y:(s/6)+tri_size*2+padding,
l:(s * 2/3)-padding*2
};
return sdim;
}
function create_bs_square(){
bs_square_dim();
box = [sdim.x, sdim.y, sdim.l, sdim.l];
bs_square.h = canvas.rect.apply(canvas, box).attr({
stroke:"#EEE", gradient: "0-#FFF-#000", opacity:1});
bs_square.s = canvas.rect.apply(canvas, box).attr({
stroke:null, gradient: "0-#FFF-#FFF", opacity:0});
bs_square.b = canvas.rect.apply(canvas, box).attr({
stroke:null, gradient: "90-#000-#FFF", opacity:0});
bs_square.b.node.style.cursor = "crosshair";
}
function hue_segement_shape(){
var path = "M -@W 0 L @W 0 L @W @H L -@W @H z";
return path.replace(/@H/img, tri_size*2).replace(/@W/img,tri_size);
}
function copy_segment(r, d, k){
var n = r.clone();
var hue = d*(255/k);
var s = size/2,
t = tri_size,
p = padding;
n.transform("t"+s+","+(s-t)+"r"+(360/k)*d+"t0,-"+(s-t-p)+"");
n.attr({"stroke-width":0, fill:"hsb("+d*(1/k)+", 1, 0.85)"});
hue_ring.hues.push(n);
}
function create_hue_ring(){
var s = hue_segement_shape(),
tri = canvas.path(s).attr({stroke:"rgba(0,0,0,0)"}).transform("t"+(size/2)+","+padding),
k = segments; // # of segments to use to generate the hues
hue_ring.hues = canvas.set();
for(n=0; n<k; n++){ copy_segment(tri, n, k); }
// IE needs a slight opacity to assign events
hue_ring.event = canvas.circle(
center,
center,
center-tri_size-padding).attr({"stroke-width":tri_size*2, opacity:0.01});
hue_ring.outline = canvas.circle(
center,
center,
center-tri_size-padding).attr({"stroke":"#000", "stroke-width":(tri_size*2)+3, opacity:0.1});
hue_ring.outline.toBack();
hue_ring.event.node.style.cursor = "crosshair";
}
function run_onchange_event(){
if (({}).toString.call(change_callback).match(/function/i)){
change_callback(current_color);
}
}
return create(target, color_wheel_size);
};