-
Notifications
You must be signed in to change notification settings - Fork 2
/
tinycolor.js
462 lines (406 loc) · 12.3 KB
/
tinycolor.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
/* eslint no-constructor-return:0 */
/*
* ES-TinyColor : Core Class
* ────────────────────────────────────────────────────────────────────────────
* © 2016 Mark Griffiths @ The Bespoke Pixel (MIT licensed)
* Based on TinyColor © Brian Grinstead
*/
import {mathRound, boundAlpha, roundAlpha, roundIf01} from '../utilities.js'
import {rawToDeepRgba, rawToRgba, rgbaToHex, rgbToHex, rgbaToArray, rgbaToString, convertToPercentage, rgbaToPercentageRgba} from '../converters.js'
import {calcMix, calcBrightness, calcLuminance} from '../calculations.js'
import {readability as readabilityWCAG, isReadable as isWCAGReadable, mostReadable as mostWCAGReadable} from '../readability.js'
import {combine} from '../combiners.js'
import {modify} from '../modifiers.js'
import TinyColorExtensionAPI from './tinycolor-extension-api.js'
let tinyCounter = 0
const extensionApi = new TinyColorExtensionAPI()
export default class TinyColor {
/**
* Create a new TinyColor instance
* @param {string|array|object} color Notation describing a color
* @param {object} options Options object (see below)
* @return {TinyColor} An instance representing the color
*/
constructor(color, options = {}) {
color = color || ''
// If input is already a tinycolor, return itself
if (color instanceof TinyColor) {
return color
}
const rgba = extensionApi.findColor(color)
this._originalInput = color
this._r = roundIf01(rgba.r)
this._g = roundIf01(rgba.g)
this._b = roundIf01(rgba.b)
this._a = rgba.a
this._roundA = roundAlpha(this._a)
this._format = options.format || rgba.format
this._gradientType = options.gradientType
this._ok = rgba.ok
this._tc_id = TinyColor.newId()
extensionApi.set(options)
}
/**
* Create a new ID
*
* @return {number} Incremented ID counter
*/
static newId() {
return tinyCounter++
}
/**
* Register a TinyColor extension
* @param {string} id The plugin identifier
* @param {object} [options={}] Plugin options
* @param {string} options.alphaFormat rgb|hex
* @param {boolean} options.shortHex Short hex codes #ABC, if possible
* @param {boolean} options.upperCaseHex User UPPER case hex
* @return {TinyColorExtension} The TinyColor extension
*/
static registerFormat(id, options = {}) {
return extensionApi.add(id, options)
}
/**
* Are two TinyColor colours equivalent?
*
* @param {TinyColor} color1 The first color
* @param {TinyColor} color2 The second color
* @return {boolean} Equivalent or not?
*/
static equals(color1, color2) {
if (!color1 || !color2) {
return false
}
return new TinyColor(color1).toRgbString() === new TinyColor(color2).toRgbString()
}
/**
* Create a new TinyColor from values from 0..1
*
* @param {object} color The color
* @param {object} options The options
* @return {TinyColor} The tiny color.
*/
static fromRatio(color, options) {
if (typeof color === 'object') {
const newColor = {}
for (const i in color) {
if (Object.prototype.hasOwnProperty.call(color, i)) {
if (i === 'a') {
newColor[i] = color[i]
} else {
newColor[i] = convertToPercentage(color[i])
}
}
}
color = newColor
}
return new TinyColor(color, options)
}
/**
* Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)
*
* @param {TinyColor} color1 The first color
* @param {TinyColor} color2 The second color
* @return {number} The color contrast defined by (WCAG Version 2)
*/
static readability(color1, color2) {
return readabilityWCAG(color1, color2)
}
/**
* Ensure that foreground and background color combinations meet WCAG2 guidelines.
*
* @param {TinyColor} color1 The first color
* @param {TinyColor} color2 The second color
* @param {object} wcag2 The WCAG2 properties to test
* @param {object} wcag2.level The level to test "AA" or "AAA" (default "AA")
* @param {object} wcag2.size The content size to test "large" or "small" (default "small")
* @example Tinycolor.isReadable("#000", "#111") → false
* @example Tinycolor.isReadable("#000", "#111", {level:"AA",size:"large"}) → false
* @return {(boolean|number)} True if readable, False otherwise.
*/
static isReadable(color1, color2, wcag2) {
return isWCAGReadable(color1, color2, wcag2)
}
/**
* Given a base color and a list of possible foreground or background colors for that
* base, returns the most readable color.
*
* Optionally returns Black or White if the most readable color is unreadable.
*
* @param {TinyColor} baseColor The base color
* @param {[TinyColor]} colorList An array of TinyColors
* @param {object} [args={}] The arguments
* @param {boolean} args.includeFallbackColors Include fallback colors?
* @param {object} args.level The level to test "AA" or "AAA" (default "AA")
* @param {object} args.size The content size to test "large" or "small" (default "small")
* @example Tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"], {includeFallbackColors:false}).toHexString(); // "#112255"
* @example Tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"], {includeFallbackColors:true}).toHexString(); // "#ffffff"
* @example Tinycolor.mostReadable("#a8015a", ["#faf3f3"], {includeFallbackColors:true, level:"AAA", size:"large"}).toHexString(); // "#faf3f3"
* @example Tinycolor.mostReadable("#a8015a", ["#faf3f3"], {includeFallbackColors:true, level:"AAA", size:"small"}).toHexString(); // "#ffffff"
* @return {TinyColor} A TinyColor instance of the msot readable color.
*/
static mostReadable(baseColor, colorList, args) {
return mostWCAGReadable(baseColor, colorList, args)
}
/**
* Mix a second colour into the first
*
* @param {TinyColor} color1 The first color
* @param {TinyColor} color2 The second color
* @param {number} amount The mix amount of the second color
* @return {TinyColor} A new, mixed TinyColor instance
*/
static mix(color1, color2, amount) {
return calcMix(color1, color2, amount)
}
/**
* Determines if dark.
*
* @return {boolean} True if dark, False otherwise.
*/
isDark() {
return this.getBrightness() < 128
}
/**
* Determines if light.
*
* @return {boolean} True if light, False otherwise.
*/
isLight() {
return !this.isDark()
}
/**
* Determines if valid.
*
* @return {boolean} True if valid, False otherwise.
*/
isValid() {
return this._ok
}
/**
* Gets the original input.
*
* @return {string|object} The original input.
*/
getOriginalInput() {
return this._originalInput
}
/**
* Gets the format.
*
* @return {string} The format.
*/
getFormat() {
return this._format
}
/**
* Gets the alpha.
*
* @return {number} The alpha.
*/
getAlpha() {
return this._a
}
/**
* Gets the brightness.
*
* @return {number} The brightness.
*/
getBrightness() {
return calcBrightness(this.toRgb())
}
/**
* Gets the luminance.
*
* @return {number} The luminance.
*/
getLuminance() {
return calcLuminance(rawToDeepRgba(this))
}
/**
* Return the current color as a string.
*
* @param {string} format The color format
* @return {string} The current color, as a string.
*/
toString(format) {
return extensionApi.print(rawToRgba(this), this._format, format)
}
/**
* Returns a name representation of the object.
*
* @return {string} The name of the colour.
*/
toName() {
return extensionApi.print(rawToRgba(this), 'name', 'toName')
}
/**
* Returns a rgb representation of the object.
*
* @return {object} Rgb representation of the object.
*/
toRgb() {
return rawToDeepRgba(this)
}
/**
* Returns a rgb string representation of the object.
*
* @return {string} Rgb string representation of the object.
*/
toRgbString() {
return rgbaToString(rawToRgba(this))
}
/**
* Returns a rgb array representation of the object.
*
* @return {[number]} Rgb array representation of the object.
*/
toRgbArray() {
return rgbaToArray(rawToRgba(this))
}
/**
* Returns a percentage rgb representation of the object.
*
* @return {object} Percentage rgb representation of the object.
*/
toPercentageRgb() {
return rgbaToPercentageRgba(rawToDeepRgba(this))
}
/**
* Returns a percentage rgb string representation of the object.
*
* @return {string} Percentage rgb string representation of the object.
*/
toPercentageRgbString() {
return rgbaToString(rgbaToPercentageRgba(rawToRgba(this)))
}
/**
* Return the hex string of a color, as pure hexadecimal.
*
* @param {boolean} allow3Char Allow 3 digit RGB strings
* @return {string} The Hex string of the color.
*/
toHex(allow3Char) {
return rgbToHex(rawToRgba(this), allow3Char)
}
/**
* Return the hex string of a color, with a leading #
*
* @param {boolean} allow3Char Allow 3 digit RGB strings
* @return {string} The Hex string of the color.
*/
toHexString(allow3Char) {
return `#${this.toHex(allow3Char)}`
}
/**
* Return the hex string of a color with aplha, as pure hexadecimal.
*
* @param {boolean} allow4Char Allow 4 digit RGBA strings
* @return {string} The Hex string of the color.
*/
toHex8(allow4Char) {
return rgbaToHex(rawToRgba(this), allow4Char)
}
/**
* Return the hex string of a color with aplha, with a leading #
*
* @param {boolean} allow3Char Allow 4 digit RGBA strings
* @return {string} The Hex string of the color.
*/
toHex8String(allow4Char) {
return `#${this.toHex8(allow4Char)}`
}
/**
* Returns a HSV object representation of the object.
*
* @return {object} HSV(A) representation of the color.
*/
toHsv() {
return extensionApi.raw(rawToDeepRgba(this), 'hsv')
}
/**
* Returns a HSV string representation of the object.
*
* @return {string} hsv(h, s, v[, a]) representation of the color.
*/
toHsvString() {
return extensionApi.print(rawToDeepRgba(this), this._format, 'hsv')
}
/**
* Returns a HSL object representation of the object.
*
* @return {object} HSL(A) representation of the color.
*/
toHsl() {
return extensionApi.raw(rawToDeepRgba(this), 'hsl')
}
/**
* Returns a HSL string representation of the object.
*
* @return {string} hsl(h, s, l[, a]) representation of the color.
*/
toHslString() {
return extensionApi.print(rawToDeepRgba(this), this._format, 'hsl')
}
/**
* Sets the alpha.
*
* @param {number} value The alpha value (0 - 1.0)
* @return {TinyColor} The current colour with the set alpha.
*/
setAlpha(value) {
this._a = boundAlpha(value)
this._roundA = mathRound(100 * this._a) / 100
return this
}
/**
* Creates a new instance of the object with same properties than original.
*
* @return {TinyColor} Copy of this object.
*/
clone() {
return new TinyColor(this.toString())
}
lighten(...args) {
return modify('lighten', [this, ...args])
}
brighten(...args) {
return modify('brighten', [this, ...args])
}
darken(...args) {
return modify('darken', [this, ...args])
}
desaturate(...args) {
return modify('desaturate', [this, ...args])
}
saturate(...args) {
return modify('saturate', [this, ...args])
}
greyscale(...args) {
return modify('greyscale', [this, ...args])
}
invert(...args) {
return modify('invert', [this, ...args])
}
spin(...args) {
return modify('spin', [this, ...args])
}
analogous(...args) {
return combine('analogous', [this, ...args])
}
complement(...args) {
return combine('complement', [this, ...args])
}
monochromatic(...args) {
return combine('monochromatic', [this, ...args])
}
splitcomplement(...args) {
return combine('splitcomplement', [this, ...args])
}
triad(...args) {
return combine('triad', [this, ...args])
}
tetrad(...args) {
return combine('tetrad', [this, ...args])
}
}