-
Notifications
You must be signed in to change notification settings - Fork 1
/
colorpicker.js
261 lines (220 loc) · 6.95 KB
/
colorpicker.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
// Original author: John Zila
// Modified by: Andrey Petrov
function ctx_xy_to_rgb(ctx, xy) {
var img=ctx.getImageData(xy[0],xy[1],1,1);
return [img.data[0], img.data[1], img.data[2]];
}
//
// Accepts a "base" color and builds a square gradient with that color, black,
// and white.
//
function generatePickerSquare(ctx, rgb) {
var w=ctx.canvas.width, h=ctx.canvas.height;
var gradients = [
["#ffffff","#ffffff"],
[rgb_a_to_css(rgb, 0),rgb_a_to_css(rgb, 0xff)],
[rgb_a_to_css([0,0,0], 1),rgb_a_to_css([0,0,0], 0)]
];
var locs = [
[0,0,w,0],
[0,0,0,h],
[0,0,w,0]
];
for (i=0; i<3; i++) {
var gradient = ctx.createLinearGradient(locs[i][0],locs[i][1],locs[i][2],locs[i][3]);
gradient.addColorStop(0,gradients[i][0]);
gradient.addColorStop(1,gradients[i][1]);
ctx.fillStyle = gradient;
ctx.fillRect(0,0,w,h);
}
}
//
// Given a canvas context ctx and an active location x in the
// range [0,1), draw the spectrum bar on the canvas and put a black line at
// location x.
//
function drawSpectrumBar(ctx,x) {
var w=ctx.canvas.width, h=ctx.canvas.height, xw=Math.floor(x*w);
var gradient=ctx.createLinearGradient(0,0,w,0),sw=1.0/6;
var colors=[
"#FF0000",
"#FFFF00",
"#00FF00",
"#00FFFF",
"#0000FF",
"#FF00FF"
];
var locs=[0,sw,2*sw,3*sw,4*sw,5*sw,6*sw];
for (i=0; i<7; i++) {
gradient.addColorStop(locs[i], colors[i%6]);
}
ctx.fillStyle = gradient;
ctx.fillRect(0,0,w,h);
// Draw the line.
ctx.beginPath();
ctx.moveTo(xw,0);
ctx.lineTo(xw,h);
ctx.closePath();
ctx.strokeStyle = rgb_to_css(invert_rgb(ctx_xy_to_rgb(ctx, [xw,0])));
ctx.lineWidth = 4;
ctx.stroke();
}
//
// Draw a circle on the provided context with radius r, centered at (x,y), and
// styled with the color string represented by color.
//
function drawFocusCircle(ctx,r,x,y,color) {
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2,true);
ctx.closePath();
ctx.strokeStyle = color;
ctx.lineWidth = 1.5;
ctx.stroke();
}
function fillFocusCircle(ctx,r,x,y,color) {
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2,true);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
function clip(x,y,w,h) {
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= w) x = w-1;
if (y >= h) y = h-1;
return [x,y];
}
/***/
var active_picker = false;
function attach_colorpicker(t, preview, callback) {
var original_rgb = css_to_rgb($(preview).css('background-color'));
var picker = $('<canvas class="picker" width="208px" height="208px"></canvas>');
var spectrum = $('<canvas class="spectrum" width="208px" height="25px"></canvas>');
var box = $('<div class="color-picker"></div').append(picker).append(spectrum);
$(t).after(box);
var controller = render_colorpicker(spectrum, picker, callback);
controller(original_rgb);
var focusfn = function(e) {
e.stopPropagation();
$(t).select();
if(active_picker) $(active_picker).hide();
active_picker = $(box).show();
console.log(preview);
controller(css_to_rgb($(preview).css('background-color')));
};
$(t).click(focusfn).focus(focusfn).keydown(function(e) {
if(e.which == 13) {
$(active_picker).hide();
active_picker = false;
}
});
return controller;
}
$(document).ready(function() {
$("body").click(function() {
if(!active_picker) return;
$(active_picker).hide();
active_picker = false;
});
});
function ColorPicker(target) {
this.picker = $('<canvas class="picker" width="208px" height="208px"></canvas>');
this.spectrum = $('<canvas class="spectrum" width="208px" height="25px"></canvas>');
var = spectrumbar[0].getContext("2d");
var box = $('<div class="color-picker"></div').append(picker).append(spectrum);
$(target).after(box);
var controller = render_colorpicker(spectrum, picker, callback);
controller(original_rgb);
var focusfn = function(e) {
e.stopPropagation();
$(target).select();
if(active_picker) $(active_picker).hide();
active_picker = $(box).show();
console.log(preview);
};
$(target).click(focusfn).focus(focusfn).keydown(function(e) {
if(e.which == 13) {
$(active_picker).hide();
active_picker = false;
}
});
return controller;
}
ColorPicker.prototype = {
set_color: function(rgb) {
},
get_color: function() {
},
_draw_spectrum: function() {
},
_draw_picker: function() {
},
}
function render_colorpicker(spectrumbar, pickersquare, callback) {
var e,s,loc=[0,0],col=0,os;
// Generate the spectrum bar.
var ctxb = spectrumbar[0].getContext("2d");
var w=spectrumbar.width(), h=spectrumbar.height();
drawSpectrumBar(ctxb, col);
// Generate the picker square.
var ctx = pickersquare[0].getContext("2d");
var drawPickerSquare = function(coeff) {
var xy=[parseInt(coeff*(ctxb.canvas.width-1)), 0];
var rgb=invert_rgb(ctx_xy_to_rgb(ctxb, xy));
generatePickerSquare(ctx,rgb);
}
var drawPickerCircle = function(ctx, loc) {
var rgb = ctx_xy_to_rgb(ctx, loc);
callback(rgb);
fillFocusCircle(ctx, 7, loc[0], loc[1], "#" + rgb_to_hex(rgb));
drawFocusCircle(ctx, 7.5, loc[0], loc[1], "#000000");
drawFocusCircle(ctx, 6, loc[0], loc[1], "#ffffff");
};
var redrawPicker = function(ctx,loc,col) {
drawPickerSquare(col);
drawPickerCircle(ctx,loc);
};
var cfun = function(ev) {
ev.stopPropagation();
os = $(pickersquare).offset();
loc = clip(ev.pageX-os.left,ev.pageY-os.top,ctx.canvas.width,ctx.canvas.height);
drawPickerSquare(col);
drawPickerCircle(ctx,loc);
};
var calcSpectrumOffset = function(ev) {
ev.stopPropagation();
var w=ctxb.canvas.width, h=ctxb.canvas.height;
os=$(spectrumbar).offset();
var xy=clip(ev.pageX-os.left,ev.pageY-os.top,w,h);
col=(xy[0])/ctxb.canvas.width;
drawSpectrumBar(ctxb,col);
redrawPicker(ctx,loc,col);
};
redrawPicker(ctx,loc,col);
var sqDrag=pDrag=false;
$(pickersquare).click(cfun);
$(pickersquare).mousedown(function(e) {
sqDrag = true;
});
$(spectrumbar).click(calcSpectrumOffset);
$(spectrumbar).mousedown(function(e) {
pDrag = true;
});
$(document).mousemove(function(e) {
if (sqDrag) {cfun(e);}
if (pDrag) {calcSpectrumOffset(e);}
});
$(document).mouseup(function(e) {
sqDrag = false;
pDrag = false;
});
return function(rgb) {
var hsv=rgb_to_hsv(rgb);
col = (hsv[0]/0xff);
loc = [(hsv[2]/0xff)*ctx.canvas.width, (hsv[1]/0xff)*ctx.canvas.height];
drawSpectrumBar(ctxb,col);
drawPickerSquare(col);
drawPickerCircle(ctx,loc);
}
}