/
colorjoe.js
279 lines (223 loc) · 5.58 KB
/
colorjoe.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
(function(root, factory) {
if(typeof define === 'function' && define.amd)
define(['./onecolor', './drag', './elemutils', './extras'], factory);
else root.colorjoe = factory(root.ONECOLOR, root.drag, root.elemutils,
root.colorjoeextras);
}(this, function(onecolor, drag, utils, extras) {
var picker = function(cbs) {
if(!all(isFunction, [cbs.init, cbs.xy, cbs.z]))
return console.warn('colorjoe: missing cb');
return function(element, initialColor, extras) {
return setup({
e: element,
color: initialColor,
cbs: cbs,
extras: extras
});
};
};
/* pickers */
picker.rgb = picker({
init: function(col, xy, z) {
var ret = onecolor(col).hsl();
this.xy(ret, {x: ret.saturation(), y: 1 - ret.value()}, xy, z);
this.z(ret, ret.hue(), xy, z);
return ret;
},
xy: function(col, p, xy, z) {
utils.X(xy.pointer, p.x);
utils.Y(xy.pointer, p.y);
return col.saturation(p.x).value(1 - p.y);
},
z: function(col, v, xy, z) {
utils.Y(z.pointer, v);
RGB_BG(xy.background, v);
return col.hue(v);
}
});
picker.hsl = picker({
init: function(col, xy, z) {
var ret = onecolor(col).hsl();
this.xy(ret, {x: ret.hue(), y: 1 - ret.saturation()}, xy, z);
this.z(ret, 1 - ret.lightness(), xy, z);
return ret;
},
xy: function(col, p, xy, z) {
utils.X(xy.pointer, p.x);
utils.Y(xy.pointer, p.y);
RGB_BG(z.background, p.x);
return col.hue(p.x).saturation(1 - p.y);
},
z: function(col, v, xy, z) {
utils.Y(z.pointer, v);
return col.lightness(1 - v);
}
});
picker._extras = {};
picker.registerExtra = function(name, fn) {
if(name in picker._extras)
console.warn('Extra "' + name + '"has been registered already!');
picker._extras[name] = fn;
};
for(var k in extras) {
picker.registerExtra(k, extras[k]);
}
return picker;
function RGB_BG(e, h) {
utils.BG(e, new onecolor.HSV(h, 1, 1).cssa());
}
function setup(o) {
if(!o.e) return console.warn('colorjoe: missing element');
var e = isString(o.e)? document.getElementById(o.e): o.e;
e.className = 'colorPicker';
var cbs = o.cbs;
var xy = drag.xyslider({
parent: e,
'class': 'twod',
cbs: {
begin: changeXY,
change: changeXY,
end: done
}
});
function changeXY(p) {
col = cbs.xy(col, {
x: utils.clamp(p.x, 0, 1),
y: utils.clamp(p.y, 0, 1)
}, xy, z);
changed();
}
var z = drag.slider({
parent: e,
'class': 'oned',
cbs: {
begin: changeZ,
change: changeZ,
end: done
}
});
function changeZ(p) {
col = cbs.z(col, utils.clamp(p.y, 0, 1), xy, z);
changed();
}
// Initial color
var previous = getColor(o.color);
var col = cbs.init(previous, xy, z);
var listeners = {change: [], done: []};
function changed(skip) {
skip = isArray(skip)? skip: [];
var li = listeners.change;
var v;
for(var i = 0, len = li.length; i < len; i++) {
v = li[i];
if(skip.indexOf(v.name) == -1) v.fn(col);
}
}
function done() {
// Do not call done callback if the color did not change
if (previous.equals(col)) return;
for(var i = 0, len = listeners.done.length; i < len; i++) {
listeners.done[i](col);
}
previous = col;
}
var ob = {
e: e,
update: function(skip) {
changed(skip);
return this;
},
get: function() {
return col;
},
set: function(c) {
var oldCol = this.get();
col = cbs.init(getColor(c), xy, z);
if(!oldCol.equals(col)) this.update();
return this;
},
getAlpha: function() {
return col.alpha();
},
setAlpha: function(v) {
col = col.alpha(v);
this.update();
return this;
},
on: function(evt, cb, name) {
if(evt == 'change' || evt == 'done') {
listeners[evt].push({name: name, fn: cb});
}
else console.warn('Passed invalid evt name "' + evt + '" to colorjoe.on');
return this;
},
removeAllListeners: function(evt) {
if (evt) {
delete listeners[evt];
}
else {
for(var key in listeners) {
delete listeners[key];
}
}
return this;
}
};
setupExtras(e, ob, o.extras);
changed();
return ob;
}
function getColor(c) {
if(!isDefined(c)) return onecolor('black');
if(c.isColor) return c;
var ret = onecolor(c);
if(ret) return ret;
if(isDefined(c)) console.warn('Passed invalid color to colorjoe, using black instead');
return onecolor('black');
}
function setupExtras(p, joe, extras) {
if(!extras) return;
var c = utils.div('extras', p);
var cbs;
var name;
var params;
extras.forEach(function(e, i) {
if(isArray(e)) {
name = e[0];
params = e.length > 1? e[1]: {};
}
else {
name = e;
params = {};
}
extra = name in picker._extras? picker._extras[name]: null;
if(extra) {
cbs = extra(c, extraProxy(joe, name + i), params);
for(var k in cbs) joe.on(k, cbs[k], name);
}
});
}
function extraProxy(joe, name) {
var ret = copy(joe);
ret.update = function() {
joe.update([name]);
};
return ret;
}
function copy(o) {
// returns a shallow copy
var ret = {};
for(var k in o) {
ret[k] = o[k];
}
return ret;
}
function all(cb, a) {return a.map(cb).filter(id).length == a.length;}
function isArray(o) {
return Object.prototype.toString.call(o) === "[object Array]";
}
function isString(o) {return typeof(o) === 'string';}
function isDefined(input) {return typeof input !== "undefined";}
function isFunction(input) {return typeof input === "function";}
function id(a) {return a;}
}));