-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.js
More file actions
442 lines (428 loc) · 17.6 KB
/
Copy pathColor.js
File metadata and controls
442 lines (428 loc) · 17.6 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
'use strict';
window.Color = (function () {
var Color;
function merge_objs () {
var merged = {}, i, name, addon_obj;
for (i = 0; i < arguments.length && arguments[i]; i++) {
addon_obj = arguments[i];
for (name in addon_obj) {
if (addon_obj.hasOwnProperty(name)) {
merged[name] = addon_obj[name];
}
}
}
return merged;
}
function get_args (base_args, args, merge) {
var arg,
final_args = merge_objs({}, base_args);
for (arg in args) {
if (args.hasOwnProperty(arg) && args[arg] !== undefined) {
if (arg in base_args || merge) {
final_args[arg] = args[arg];
}
}
}
return final_args;
}
function is_element (item) {
return typeof(item) === 'object' && item.tagName !== undefined;
}
Color = {
get: function (color, args) {
var hue, result, i, rgb_clr, min, max, delta, tmp;
args = get_args({
format: 'hex', // e.g. hex, rgb, rgba, rgb_obj, hsv_obj
surface: (is_element(color) ? 'color' : undefined)
}, args);
if (color === undefined) {
throw new Error("Invalid color to get value of.");
}
if (args.surface) {
color = window.getComputedStyle(color, null).style[args.surface];
}
if (typeof color === 'string') {
if (color.match(/^#?[a-f0-9]{6}$/i)) {
// e.g. 0099FF, #996600
// add the # if it isn't there
if (color.substr(0, 1) !== '#') {
color = '#' + color;
}
// no conversion needed
if (args.format === 'hex') {
return color;
}
} else if (color.substr(0, 3) === 'rgb') {
if (color.substr(0, 4) === 'rgba') {
// kill the alpha part if present
color = color.replace(/, \d+\)/, ')');
}
// e.g. rgb(255, 41, 6)
// no conversion needed
if (args.format === 'rgb') {
return color;
}
// get everything between "rgb(" and the ending ")"
color = color.substr(4, color.length - 5).split(',');
for (i = 0; i < color.length; i++) {
color[i] = parseInt(color[i]).toString(16);
if (color[i].length === 1) {
color[i] = '0' + color[i];
} else if (color[i].length > 2) {
throw new Error("Color component '" + color[i] + "' is out of bounds.");
}
}
color = '#' + color.join('');
} else {
throw new Error("Invalid color string: '" + color + "'.");
}
} else if (typeof color === 'object') {
result = '#';
if (color.r !== undefined && color.g !== undefined && color.b !== undefined) {
// no conversion needed
if (args.format === 'rgb_obj') {
return color;
}
// make sure our colors are in order
color = {
r: color.r,
g: color.g,
b: color.b
};
for (hue in color) {
if (color.hasOwnProperty(hue)) {
color[hue] = color[hue].toString(16);
if (color[hue].length === 1) {
result += '0' + color[hue];
} else {
result += color[hue];
}
}
}
} else if (color.h !== undefined && color.s !== undefined && color.v !== undefined) {
// no conversion needed
if (args.format === 'hsv_obj') {
return color;
}
if (color.s === 0) {
color = parseInt(color.v * 255).toString(16);
if (color.length === 1) {
color = '0' + color;
}
result = '#' + color + color + color;
} else {
color.h = (color.h % 360) / 60;
tmp = {
i: Math.floor(color.h)
};
tmp.f = color.h - tmp.i;
tmp.p = color.v * (1 - color.s);
tmp.q = color.v * (1 - color.s * tmp.f);
tmp.t = color.v * (1 - color.s * (1 - tmp.f));
if (tmp.i === 0) {
rgb_clr = {
r: color.v,
g: tmp.t,
b: tmp.p
};
} else if (tmp.i === 1) {
rgb_clr = {
r: tmp.q,
g: color.v,
b: tmp.p
};
} else if (tmp.i === 2) {
rgb_clr = {
r: tmp.p,
g: color.v,
b: tmp.t
};
} else if (tmp.i === 3) {
rgb_clr = {
r: tmp.p,
g: tmp.q,
b: color.v
};
} else if (tmp.i === 4) {
rgb_clr = {
r: tmp.t,
g: tmp.p,
b: color.v
};
} else if (tmp.i === 5) {
rgb_clr = {
r: color.v,
g: tmp.p,
b: tmp.q
};
} else {
throw new Error("Invalid color hue: '" + (color.h * 60) + "'.");
}
// now convert back to hex for further conversion
result = '#';
for (hue in rgb_clr) {
if (rgb_clr.hasOwnProperty(hue)) {
rgb_clr[hue] = parseInt(rgb_clr[hue] * 255, 10).toString(16);
if (rgb_clr[hue].length === 1) {
result += '0' + rgb_clr[hue];
} else {
result += rgb_clr[hue];
}
}
}
}
} else {
throw new Error("Unrecognized color object.");
}
color = result;
} else {
throw new Error("Unrecognized color type: " + color + '.');
}
// and return as the requested format - we're in hex by default
// still nothin' to do
if (args.format === 'rgb') {
color = 'rgb(' + parseInt(color.substr(1, 2), 16) + ', ' +
parseInt(color.substr(3, 2), 16) + ', ' +
parseInt(color.substr(5, 2), 16) + ')';
} else if (args.format === 'hsv_obj') {
// convert to RBG [0,1] first
rgb_clr = {
'r': parseInt(color.substr(1, 2), 16) / 255,
'g': parseInt(color.substr(3, 2), 16) / 255,
'b': parseInt(color.substr(5, 2), 16) / 255
};
max = Math.max(rgb_clr.r, rgb_clr.g, rgb_clr.b);
min = Math.min(rgb_clr.r, rgb_clr.g, rgb_clr.b);
delta = max - min;
color = {
h: 0,
s: 0,
v: max
};
if (max !== 0) {
color.s = delta / max;
} else {
return {s: 0, h: 360, v: color.v};
}
if (rgb_clr.r === max) {
color.h = (rgb_clr.g - rgb_clr.b) / delta; // yellow/magenta range
} else if (rgb_clr.g === max) {
color.h = 2 + (rgb_clr.b - rgb_clr.r) / delta; // cyan/yellow range
} else {
color.h = 4 + (rgb_clr.r - rgb_clr.g) / delta; // yellow/magenta range
}
color.h *= 60;
if (color.h < 0) {
color.h += 360;
}
color.h = parseInt(color.h, 10);
} else if (args.format === 'rgb_obj') {
color = {
'r': parseInt(color.substr(1, 2), 16),
'g': parseInt(color.substr(3, 2), 16),
'b': parseInt(color.substr(5, 2), 16)
};
} else if (args.format !== 'hex') {
// we're in hex by default
throw new Error("Invalid color format: '" + args.format + "'.");
}
return color;
},
blend: function (source, target, args) {
var color, part;
// if there are no steps or the offset is zero take the easy way out
args = get_args({
format: 'hex',
ratio: 0.5,
surface: undefined
}, args);
source = Color.get(source, {format: 'rgb_obj', surface: args.surface});
target = Color.get(target, {format: 'rgb_obj', surface: args.surface});
// easy cases
if (args.ratio === 0) {
color = source;
} else if (args.ratio === 1) {
color = target;
} else {
// and blend each part
color = {
'r': parseInt((source.r * (1 - args.ratio)) + (target.r * args.ratio), 10),
'g': parseInt((source.g * (1 - args.ratio)) + (target.g * args.ratio), 10),
'b': parseInt((source.b * (1 - args.ratio)) + (target.b * args.ratio), 10)
};
// limit values to 0-255, in case the ratio is > 1 or < 0
for (part in color) {
if (color.hasOwnProperty(part)) {
if (parseInt(color[part], 10) > 255) {
color[part] = 255;
} else if (parseInt(color[part], 10) < 0) {
color[part] = 0;
}
}
}
}
return Color.get(color, {format: args.format});
},
mix: function (color, args) {
/* arguments:
args = {
format: 'hex', // or any other format supported by "Color.get"
hue: 0 - 360,
hue_mult: 0.0 - 1.0,
hue_shift: 0 - 360,
saturation: 0.0 - 1.0,
saturation_mult: 0.0 - 1.0,
saturation_shift: 0 - 360,
value: 0.0 - 1.0,
value_mult: 0.0 - 1.0,
value_shift: 0 - 360
};
*/
// can't use get_args as args effects are cumulative
if (args === undefined) {
args = {};
}
color = Color.get(color, {format: 'hsv_obj'});
if (args.hue !== undefined) {
color.h = args.hue;
}
if (args.hue_mult !== undefined) {
color.h *= args.hue_mult;
}
if (args.hue_shift !== undefined) {
color.h += args.hue_shift;
}
if (args.saturation !== undefined) {
color.s = args.saturation;
}
if (args.saturation_mult !== undefined) {
color.s *= args.saturation_mult;
}
if (args.saturation_shift !== undefined) {
color.s += args.saturation_shift;
}
if (args.value !== undefined) {
color.v = args.value;
}
if (args.value_mult !== undefined) {
color.v *= args.value_mult;
}
if (args.value_shift !== undefined) {
color.v += args.value_shift;
}
if (color.h < 0) {
color.h = 360 + (color.h % 360);
}
if (color.h > 360) {
color.h = color.h % 360;
}
if (color.s > 1) {
color.s = 1;
}
if (color.s < 0) {
color.s = 0;
}
if (color.v > 1) {
color.v = 1;
}
if (color.v < 0) {
color.v = 0;
}
return Color.get(color, args.format);
},
fade: function (colors, args) {
var fade;
args = get_args({
steps: 1, // how many steps between colors
allow_oob: true // whether or not to allow out-of-bounds colors
}, args);
// validate our colors object
fade = {
_args: args,
colors: []
};
fade.set_steps = function (steps) {
if (typeof(steps) === 'number' && steps >= 0) {
fade.steps = steps;
fade.size = ((fade.colors.length - 1) * fade.steps) + fade.colors.length;
} else {
throw new Error("The number of steps must be 0 or greater.");
}
};
fade.set_size = function (count) {
// make sure this size can be used with this many colors
if (count === fade.colors.length) {
fade.set_steps(0);
} else {
// figure out how many steps we need to have the number of colors requested
fade.set_steps(
(count - fade.colors.length) / (fade.colors.length - 1)
);
}
};
fade.get_colors = function (args) {
var colors = [],
i;
for (i = 0; i < fade.size; i += 1) {
colors.push(fade.get_color(i), args);
}
return colors;
};
fade.set_colors = function (colors, args) {
var i;
args = get_args({
surface: (is_element(colors) ? 'color' : undefined)
}, args);
if (typeof(colors) !== 'object' || colors.length === undefined) {
throw new Error("Invalid colors object; array of colors or elements expected.");
}
fade.colors = [];
for (i = 0; i < colors.length; i += 1) {
fade.colors[i] = Color.get(colors[i], args);
}
};
/* Get a color at the specified index. */
fade.get_color = function (i, args) {
var start_color_num, end_color_num, offset, blend_ratio,
depth = 0;
args = get_args({
format: 'hex'
}, args);
if (i < 0 || i >= fade.size) {
if (! fade._args.allow_oob) {
throw new Error("Please enter a color number between 0 and " + fade.size - 1 + ".");
} else {
// otherwise, translate the step to exist in the range we've got
if (i < 0) {
i = i * -1;
}
if (i >= fade.size) {
depth = parseInt(i / fade.size, 10);
// if we're an odd number in depth then flip the fade around for smooth gradients (e.g. 0 1 2 3 2 1 0 1 2 ...)
if (depth % 2 === 1) {
i = fade.size - (i % fade.size) - 1;
} else {
i = i % fade.size;
}
}
}
}
// now get the color for the step we are on
// figure out what color it is based on, what it is fading to, and which fade step it is on
start_color_num = parseInt((i / fade.size), 10);
end_color_num = start_color_num + 1;
offset = i % (fade.steps + 2);
blend_ratio = offset / (fade.steps + 2);
return Color.blend(
fade.colors[start_color_num],
fade.colors[end_color_num],
{ratio: blend_ratio, format: args.format}
);
};
fade.set_colors(colors);
fade.set_steps(args.steps);
return fade;
}
};
return Color;
}());