This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
546 lines (444 loc) · 10.7 KB
/
index.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
* Barebones color class.
*
* @module color
*/
module.exports = Color;
var parse = require('color-parse');
var stringify = require('color-stringify');
var manipulate = require('color-manipulate');
var measure = require('color-measure');
var spaces = require('color-space');
var slice = require('sliced');
var pad = require('left-pad');
var isString = require('mutype/is-string');
var isObject = require('mutype/is-object');
var isArray = require('mutype/is-array');
var isNumber = require('mutype/is-number');
var loop = require('mumath/loop');
var round = require('mumath/round');
var between = require('mumath/between');
/**
* Color class.
* @constructor
*/
function Color (arg, space) {
if (!(this instanceof Color)) {
return new Color(arg);
}
var self = this;
//init model
self._values = [0,0,0];
self._space = 'rgb';
self._alpha = 1;
// keep xyz values to preserve quality
self._xyz = [0,0,0];
//parse argument
self.parse(arg, space);
}
/** Static parser/stringifier */
Color.parse = Color.from = function (cstr) {
return new Color(cstr);
};
Color.stringify = function (color, space) {
return color.toString(space);
};
/** API */
var proto = Color.prototype;
/** Universal setter, detecting the type of argument */
proto.parse = proto.from = function (arg, space) {
var self = this;
if (!arg) {
return self;
}
//[0,0,0]
else if (isArray(arg)) {
self.fromArray(arg, space);
}
else if (isNumber(arg)) {
//12, 25, 47 [, space]
if (arguments.length > 2) {
var args = slice(arguments);
if (isString(args[args.length - 1])) {
space = args.pop();
}
self.parse(args, space);
}
//123445 [, space]
else {
self.fromNumber(arg, space);
}
}
//'rgb(0,0,0)'
else if (isString(arg)) {
self.fromString(arg, space);
}
//Color instance
else if (arg instanceof Color) {
self.fromArray(arg._values, arg._space);
}
//{r:0, g:0, b:0}
else if (isObject(arg)) {
self.fromJSON(arg, space);
}
return self;
};
/** String parser/stringifier */
proto.fromString = function (cstr) {
var res = parse(cstr);
this.setValues(res.values, res.space);
this.alpha(res.alpha);
return this;
};
proto.toString = function (type) {
type = type || this.getSpace();
var values = this.toArray(spaces[type] ? type : 'rgb');
values = round(values);
if (this._alpha < 1) {
values.push(this.alpha());
}
return stringify(values, type);
};
/** Array setter/getter */
proto.fromArray = function (values, spaceName) {
if (!spaceName || !spaces[spaceName]) {
spaceName = this._space;
}
this._space = spaceName;
var space = spaces[spaceName];
//get alpha
if (values.length > space.channel.length) {
this.alpha(values[space.channel.length]);
values = slice(values, 0, space.channel.length);
}
//walk by values list, cap them
this._values = values.map(function (value, i) {
return cap(value, space.name, i);
});
//update raw xyz cache
this._xyz = spaces[spaceName].xyz(this._values);
return this;
};
proto.toArray = function (space) {
var values;
if (!space) {
space = this._space;
}
//convert values to a target space
if (space !== this._space) {
//enhance calc precision, like hsl ←→ hsv ←→ hwb or lab ←→ lch ←→ luv
if (this._space[0] === space[0]) {
values = spaces[this._space][space](this._values);
}
else {
values = spaces.xyz[space](this._xyz);
}
} else {
values = this._values;
}
values = values.map(function (value, i) {
return round(value, spaces[space].precision[i]);
});
return values;
};
/** JSON setter/getter */
proto.fromJSON = function (obj, spaceName) {
var space;
if (spaceName) {
space = spaces[spaceName];
}
//find space by the most channel match
if (!space) {
var maxChannelsMatched = 0, channelsMatched = 0;
Object.keys(spaces).forEach(function (key) {
channelsMatched = spaces[key].channel.reduce(function (prev, curr) {
if (obj[curr] !== undefined || obj[ch(curr)] !== undefined) {
return prev+1;
}
else {
return prev;
}
}, 0);
if (channelsMatched > maxChannelsMatched) {
maxChannelsMatched = channelsMatched;
space = spaces[key];
}
if (channelsMatched >= 3) {
return;
}
});
}
//if no space for a JSON found
if (!space) {
throw Error('Cannot detect space.');
}
//for the space found set values
this.fromArray(space.channel.map(function (channel) {
return obj[channel] !== undefined ? obj[channel] : obj[ch(channel)];
}), space.name);
var alpha = obj.a !== undefined ? obj.a : obj.alpha;
if (alpha !== undefined) {
this.alpha(alpha);
}
return this;
};
proto.toJSON = function (spaceName) {
var space = spaces[spaceName || this._space];
var result = {};
var values = this.toArray(space.name);
//go by channels, create properties
space.channel.forEach(function (channel, i) {
result[ch(channel)] = values[i];
});
if (this._alpha < 1) {
result.a = this._alpha;
}
return result;
};
/** HEX number getter/setter */
proto.fromNumber = function (val, space) {
var values = pad(val.toString(16), 6, 0).split(/(..)/).filter(Boolean);
return this.fromArray(values, space);
};
proto.toNumber = function (space) {
var values = this.toArray(space);
return (values[0] << 16) | (values[1] << 8) | values[2];
};
/**
* Current space values
*/
proto.values = function () {
if (arguments.length) {
return this.setValues.apply(this, arguments);
}
return this.getValues.apply(this, arguments);
};
/**
* Return values array for a passed space
* Or for current space
*
* @param {string} space A space to calculate values for
*
* @return {Array} List of values
*/
proto.getValues = function (space) {
return this.toArray(space);
};
/**
* Set values for a space passed
*
* @param {Array} values List of values to set
* @param {string} spaceName Space indicator
*/
proto.setValues = proto.parse;
/**
* Current space
*/
proto.space = function () {
if (arguments.length) {
return this.setSpace.apply(this, arguments);
}
return this.getSpace.apply(this, arguments);
};
/** Return current space */
proto.getSpace = function () {
return this._space;
};
/** Switch to a new space with optional new values */
proto.setSpace = function (space, values) {
if (!space || !spaces[space]) {
throw Error('Cannot set space ' + space);
}
if (space === this._space) {
return this;
}
if (values) {
return this.setValues(values, space);
}
//just convert current values to a new space
this._values = spaces.xyz[space](this._xyz);
this._space = space;
return this;
};
/** Channel getter/setter */
proto.channel = function () {
if (arguments.length > 2) {
return this.setChannel.apply(this, arguments);
} else {
return this.getChannel.apply(this, arguments);
}
};
/** Get channel value */
proto.getChannel = function (space, idx) {
this.setSpace(space);
return this._values[idx];
};
/** Set current channel value */
proto.setChannel = function (space, idx, value) {
this.setSpace(space);
this._values[idx] = cap(value, space, idx);
this._xyz = spaces[space].xyz(this._values);
return this;
};
/** Define named set of methods for a space */
proto.defineSpace = function (name, space) {
//create precisions
space.precision = space.channel.map(function (ch, idx) {
return Math.abs(space.max[idx] - space.min[idx]) > 1 ? 1 : 0.01;
});
// .rgb()
proto[name] = function (values) {
if (arguments.length) {
if (arguments.length > 1) {
return this.setValues(slice(arguments), name);
}
return this.setValues(values, name);
} else {
return this.toJSON(name);
}
};
// .rgbString()
proto[name + 'String'] = function (cstr) {
if (cstr) {
return this.fromString(cstr);
}
return this.toString(name);
};
// .rgbArray()
proto[name + 'Array'] = function (values) {
if (arguments.length) {
return this.fromArray(values);
}
return this.toArray(name);
};
// .rgbaArray(), .hslaArray
if (name === 'rgb' || name === 'hsl') {
proto[name + 'aArray'] = function () {
var res = this[name + 'Array'].apply(this, arguments);
if (isArray(res)) {
res.push(this.alpha());
}
return res;
};
}
// .red(), .green(), .blue()
space.channel.forEach(function (cname, cidx) {
if (proto[cname]) {
return;
}
proto[cname] = function (value) {
if (arguments.length) {
return this.setChannel(name, cidx, value);
}
else {
return round(this.getChannel(name, cidx), space.precision[cidx]);
}
};
});
};
/**
* Create per-space API
*/
Object.keys(spaces).forEach(function (name) {
proto.defineSpace(name, spaces[name]);
});
/**
* Alpha getter / setter
*/
proto.alpha = function (value) {
if (arguments.length) {
this._alpha = between(value, 0, 1);
return this;
} else {
return this._alpha;
}
};
/** Hex string parser is the same as simple parser */
proto.hexString = function (str) {
if (arguments.length) {
return this.fromString(str, 'hex');
}
else {
return this.toString('hex').toUpperCase();
}
};
/** Percent string formatter */
proto.percentString = function (str) {
if (arguments.length) {
return this.fromString(str, 'percent');
}
else {
return this.toString('percent');
}
};
/** Keyword setter/getter */
proto.keyword = function (str) {
if (arguments.length) {
return this.fromString(str, 'keyword');
}
else {
return this.toString('keyword');
}
};
/** Clone instance */
proto.clone = function () {
return (new Color()).fromArray(this._values, this._space);
};
/**
* Create manipulation methods
*/
Object.keys(manipulate).forEach(function (name) {
proto[name] = function (a,b) {
return manipulate[name](this, a, b);
};
});
/** Some color compatibility bindings */
proto.rotate = proto.spin;
proto.negate = proto.invert;
proto.greyscale = proto.grayscale;
proto.clearer = proto.fadeout;
proto.opaquer = proto.fadein;
/**
* Create measure methods
*/
Object.keys(measure).forEach(function (name) {
proto[name] = function (a, b) {
// allow color.contrast() to take string args like 'red'
if (a && !(a instanceof Color)) a = Color(a)
return measure[name](this, a, b);
};
});
/** Some color compatibility bindings */
proto.luminosity = proto.luminance;
proto.dark = proto.isDark;
proto.light = proto.isLight;
//TODO:
//gray
//toFilter
//isValid
//getFormat
//random
//ie-hex-str($color)
//is(otherColor)
/** Get short channel name */
function ch (name) {
return name === 'black' ? 'k' : name[0];
}
/**
* Cap channel value.
* Note that cap should not round.
*
* @param {number} value A value to store
* @param {string} spaceName Space name take as a basis
* @param {int} idx Channel index
*
* @return {number} Capped value
*/
function cap(value, spaceName, idx) {
var space = spaces[spaceName];
if (space.channel[idx] === 'hue') {
return loop(value, space.min[idx], space.max[idx]);
} else {
return between(value, space.min[idx], space.max[idx]);
}
}