-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtinycolor.js
258 lines (202 loc) · 5.51 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
/* 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)
}
static newId() {
return tinyCounter++
}
static registerFormat(id, options = {}) {
return extensionApi.add(id, options)
}
static equals(color1, color2) {
if (!color1 || !color2) {
return false
}
return new TinyColor(color1).toRgbString() === new TinyColor(color2).toRgbString()
}
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)
}
static readability(color1, color2) {
return readabilityWCAG(color1, color2)
}
static isReadable(color1, color2, wcag2) {
return isWCAGReadable(color1, color2, wcag2)
}
static mostReadable(baseColor, colorList, args) {
return mostWCAGReadable(baseColor, colorList, args)
}
static mix(color1, color2, amount) {
return calcMix(color1, color2, amount)
}
isDark() {
return this.getBrightness() < 128
}
isLight() {
return !this.isDark()
}
isValid() {
return this._ok
}
getOriginalInput() {
return this._originalInput
}
getFormat() {
return this._format
}
getAlpha() {
return this._a
}
getBrightness() {
return calcBrightness(this.toRgb())
}
getLuminance() {
return calcLuminance(this.toRgb(), rawToDeepRgba(this))
}
toString(format) {
return extensionApi.print(rawToRgba(this), this._format, format)
}
toName() {
return extensionApi.print(rawToRgba(this), 'name', 'toName')
}
toRgb() {
return rawToDeepRgba(this)
}
toRgbString() {
return rgbaToString(rawToRgba(this))
}
toRgbArray() {
return rgbaToArray(rawToRgba(this))
}
toPercentageRgb() {
return rgbaToPercentageRgba(rawToDeepRgba(this))
}
toPercentageRgbString() {
return rgbaToString(rgbaToPercentageRgba(rawToRgba(this)))
}
toHex(allow3Char) {
return rgbToHex(rawToRgba(this), allow3Char)
}
toHexString(allow3Char) {
return `#${this.toHex(allow3Char)}`
}
toHex8(allow4Char) {
return rgbaToHex(rawToRgba(this), allow4Char)
}
toHex8String(allow4Char) {
return `#${this.toHex8(allow4Char)}`
}
toHsv() {
return extensionApi.raw(rawToDeepRgba(this), 'hsv')
}
toHsvString() {
return extensionApi.print(rawToDeepRgba(this), this._format, 'hsv')
}
toHsl() {
return extensionApi.raw(rawToDeepRgba(this), 'hsl')
}
toHslString() {
return extensionApi.print(rawToDeepRgba(this), this._format, 'hsl')
}
setAlpha(value) {
this._a = boundAlpha(value)
this._roundA = mathRound(100 * this._a) / 100
return this
}
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])
}
}