-
Notifications
You must be signed in to change notification settings - Fork 1
/
colorpicker.js
235 lines (199 loc) · 6.5 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
"use strict";
/*global module, require*/
var interpolate = require('fischer-color').interpolate,
d3 = require('d3'),
mousePos = require("./mouse-event-pos.js");
function ColorPicker() {
var w = 628,
h = 100,
canvW = 600,
canvH = 400;
var events = d3.dispatch('mousedown', 'mousemove', 'mouseup');
var ext = {min: 0, max: w, xs: [0]};
var pos = [0,0],
zm = 1;
var canv = document.createElement('canvas');
canv.width = w;
canv.height = h;
var colors = canv.getContext('2d');
var image = colors.getImageData(0, 0, w, h);
var imageMap = {};
for (var i = 0, j = -1, c; i < w * h; ++i) {
c = interpolate((i % w)/h, 1, Math.floor(i/w)/h);
image.data[++j] = c[0];
image.data[++j] = c[1];
image.data[++j] = c[2];
image.data[++j] = 255;
var thisC = [];
c.forEach(function(i){ thisC.push(Math.round(i)); });
if (!imageMap[thisC]) imageMap[thisC] = [j];
else imageMap[thisC].push(j);
}
colors.putImageData(image, 0, 0);
function findColor(rgb) {
var l = imageMap[rgb];
if (!l) return null;
var loc = l.reduce(function(a,b){return a+b;}) / l.length;
var x, y;
var pxW = w*4;
y = Math.ceil(loc/pxW);
x = Math.floor((loc % pxW) / 4);
return [x,y];
// TODO this is just a bit off (rounding errors or something)
// TODO this breaks for non-represented RGBs -- figure out a way to do reverse fischer-color lookups.
}
var center;
function isArray(val) {
return Object.prototype.toString.call(val ) === '[object Array]';
};
function cp(selection) {
var canvasBox = selection.append('canvas')
.attr('width', w)
.attr('height', h)
.attr('style', 'width:' + canvW + 'px;height:' + canvH + 'px;');
var dzoom = d3.behavior.zoom()
.scaleExtent([1, 8])
.on('zoom', zoom);
var canvas = canvasBox
.call(dzoom)
.node().getContext("2d");
cp.draw(canvas);
function zoom(tr, sc) {
canvas.save();
canvas.clearRect(0, 0, w, h);
var t = (tr && isArray(tr) && tr.length === 2) ? tr : d3.event.translate;
if (-t[0] + w > ext.max) {
ext.xs.push(ext.max);
ext.max += w;
} else if (-t[0] < ext.min) {
ext.xs.push(ext.min - w);
ext.min -= 628;
}
var s = sc || d3.event.scale;
t[1] = Math.min(Math.max(t[1], -h*(s-1)), 0);
canvas.translate(t[0],t[1]);
canvas.scale(s,s);
cp.draw(canvas);
pos = t; zm = s;
canvas.restore();
}
function getPos() {
var rect = canvasBox[0][0].getBoundingClientRect(),
newPos = mousePos(),
x = ((newPos.x - rect.left) * (w / canvW) - pos[0]) / zm,
y = ((newPos.y - rect.top) * (h / canvH) - pos[1]) / zm;
return [
/*
We shouldn't get NaN's here, but if we do then we're stuck forever, so we'll guard against them.
*/
isNaN(x) ? 0 : x,
isNaN(y) ? 0 : y
];
}
function getColor(p) {
while (p[0] < 0) p[0] += w;
while (p[0] > 628) p[0] -= w;
var rgba = colors.getImageData(Math.round(p[0]), Math.round(p[1]), 1, 1).data;
if (rgba.length > 4) {
var r = [], g = [], b = [], a = [];
for (var i=0; i<rgba.length; i+=4) {
r.push(rgba[i]);
g.push(rgba[i+1]);
b.push(rgba[i+2]);
a.push(rgba[i+3]);
}
rgba = [];
[r,g,b,a].forEach(function(c){
rgba.push(Math.round(c.reduce(function(a,b){
return a+b;
}) / c.length));
});
}
return rgbToHex(rgba);
}
function goTo(loc, scale) {
loc = loc || getPos();
scale = scale || zm;
var tl = [ w/2 - loc[0]*scale, h/2 - loc[1]*scale ];
d3.transition().duration(600).tween('zoom', function() {
var ix = d3.interpolate(
isNaN(pos[0]) ? 0 : pos[0],
tl[0]
),
iy = d3.interpolate(
isNaN(pos[1]) ? 0 : pos[1],
tl[1]
),
is = d3.interpolate(zm, scale);
return function(t) {
zoom([ix(t), iy(t)], is(t));
};
});
dzoom.translate(tl);
dzoom.scale(scale);
}
if (center) {
var color;
if (typeof center.color === 'string') color = hextoRGB(center.color);
else if (Array.isArray(center.color)) color = center.color;
else return;
var loc = findColor(color);
if (loc) goTo(loc, center.z);
}
var clicking = false, dragging = false;
selection.on('mousedown', function() {
clicking = true;
});
selection.on('mousemove', function() {
if (clicking) dragging = true;
});
selection.on('mouseup', function(d) {
if (!dragging) {
events.mouseup(getColor(getPos()), goTo());
}
// TODO not on doubleclick
clicking = false; dragging = false;
});
}
cp.draw = function(c) {
ext.xs.forEach(function(atX) {
c.drawImage(canv, atX, 0);
});
};
cp.width = function(_) {
if (!arguments.length) return canvW;
canvW = _;
return cp;
};
cp.height = function(_) {
if (!arguments.length) return canvH;
canvH = _;
return cp;
};
cp.center = function(color, z) {
if (color && z) center = {color: color, z: z};
return cp;
};
function hextoRGB(hex) {
var shr = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shr, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var res = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return res ? [
parseInt(res[1], 16),
parseInt(res[2], 16),
parseInt(res[3], 16)
] : null;
}
function rgbToHex (rgb) {
var hexa = '#';
[rgb[0],rgb[1],rgb[2]].forEach(function(c) {
var h = c.toString(16);
hexa += h.length == 1 ? '0' + h : h;
});
return hexa;
}
return d3.rebind(cp, events, 'on', 'off');
}
module.exports = ColorPicker;