-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Color.ts
320 lines (290 loc) · 8.56 KB
/
Color.ts
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
import { ColorNameMap } from './color_map';
import { reHSLa, reHex, reRGBa } from './constants';
import type { TRGBAColorSource, TColorArg } from './typedefs';
import {
hue2rgb,
hexify,
rgb2Hsl,
fromAlphaToFloat,
greyAverage,
} from './util';
/**
* @class Color common color operations
* @tutorial {@link http://fabricjs.com/fabric-intro-part-2/#colors colors}
*/
export class Color {
private declare _source: TRGBAColorSource;
/**
*
* @param {string} [color] optional in hex or rgb(a) or hsl format or from known color list
*/
constructor(color?: TColorArg) {
if (!color) {
// we default to black as canvas does
this.setSource([0, 0, 0, 1]);
} else if (color instanceof Color) {
this.setSource([...color._source]);
} else if (Array.isArray(color)) {
const [r, g, b, a = 1] = color;
this.setSource([r, g, b, a]);
} else {
this.setSource(this._tryParsingColor(color));
}
}
/**
* @private
* @param {string} [color] Color value to parse
* @returns {TRGBAColorSource}
*/
protected _tryParsingColor(color: string) {
if (color in ColorNameMap) {
color = ColorNameMap[color as keyof typeof ColorNameMap];
}
return color === 'transparent'
? ([255, 255, 255, 0] as TRGBAColorSource)
: Color.sourceFromHex(color) ||
Color.sourceFromRgb(color) ||
Color.sourceFromHsl(color) ||
// color is not recognized
// we default to black as canvas does
([0, 0, 0, 1] as TRGBAColorSource);
}
/**
* Returns source of this color (where source is an array representation; ex: [200, 200, 100, 1])
* @return {TRGBAColorSource}
*/
getSource() {
return this._source;
}
/**
* Sets source of this color (where source is an array representation; ex: [200, 200, 100, 1])
* @param {TRGBAColorSource} source
*/
setSource(source: TRGBAColorSource) {
this._source = source;
}
/**
* Returns color representation in RGB format
* @return {String} ex: rgb(0-255,0-255,0-255)
*/
toRgb() {
const [r, g, b] = this.getSource();
return `rgb(${r},${g},${b})`;
}
/**
* Returns color representation in RGBA format
* @return {String} ex: rgba(0-255,0-255,0-255,0-1)
*/
toRgba() {
return `rgba(${this.getSource().join(',')})`;
}
/**
* Returns color representation in HSL format
* @return {String} ex: hsl(0-360,0%-100%,0%-100%)
*/
toHsl() {
const [h, s, l] = rgb2Hsl(...this.getSource());
return `hsl(${h},${s}%,${l}%)`;
}
/**
* Returns color representation in HSLA format
* @return {String} ex: hsla(0-360,0%-100%,0%-100%,0-1)
*/
toHsla() {
const [h, s, l, a] = rgb2Hsl(...this.getSource());
return `hsla(${h},${s}%,${l}%,${a})`;
}
/**
* Returns color representation in HEX format
* @return {String} ex: FF5555
*/
toHex() {
const fullHex = this.toHexa();
return fullHex.slice(0, 6);
}
/**
* Returns color representation in HEXA format
* @return {String} ex: FF5555CC
*/
toHexa() {
const [r, g, b, a] = this.getSource();
return `${hexify(r)}${hexify(g)}${hexify(b)}${hexify(Math.round(a * 255))}`;
}
/**
* Gets value of alpha channel for this color
* @return {Number} 0-1
*/
getAlpha() {
return this.getSource()[3];
}
/**
* Sets value of alpha channel for this color
* @param {Number} alpha Alpha value 0-1
* @return {Color} thisArg
*/
setAlpha(alpha: number) {
this._source[3] = alpha;
return this;
}
/**
* Transforms color to its grayscale representation
* @return {Color} thisArg
*/
toGrayscale() {
this.setSource(greyAverage(this.getSource()));
return this;
}
/**
* Transforms color to its black and white representation
* @param {Number} threshold
* @return {Color} thisArg
*/
toBlackWhite(threshold: number) {
const [average, , , a] = greyAverage(this.getSource()),
bOrW = average < (threshold || 127) ? 0 : 255;
this.setSource([bOrW, bOrW, bOrW, a]);
return this;
}
/**
* Overlays color with another color
* @param {String|Color} otherColor
* @return {Color} thisArg
*/
overlayWith(otherColor: string | Color) {
if (!(otherColor instanceof Color)) {
otherColor = new Color(otherColor);
}
const source = this.getSource(),
otherAlpha = 0.5,
otherSource = otherColor.getSource(),
[R, G, B] = source.map((value, index) =>
Math.round(value * (1 - otherAlpha) + otherSource[index] * otherAlpha)
);
this.setSource([R, G, B, source[3]]);
return this;
}
/**
* Returns new color object, when given a color in RGB format
* @memberOf Color
* @param {String} color Color value ex: rgb(0-255,0-255,0-255)
* @return {Color}
*/
static fromRgb(color: string): Color {
return Color.fromRgba(color);
}
/**
* Returns new color object, when given a color in RGBA format
* @static
* @function
* @memberOf Color
* @param {String} color
* @return {Color}
*/
static fromRgba(color: string): Color {
return new Color(Color.sourceFromRgb(color));
}
/**
* Returns array representation (ex: [100, 100, 200, 1]) of a color that's in RGB or RGBA format
* @memberOf Color
* @param {String} color Color value ex: rgb(0-255,0-255,0-255), rgb(0%-100%,0%-100%,0%-100%)
* @return {TRGBAColorSource | undefined} source
*/
static sourceFromRgb(color: string): TRGBAColorSource | undefined {
const match = color.match(reRGBa());
if (match) {
const [r, g, b] = match.slice(1, 4).map((value) => {
const parsedValue = parseFloat(value);
return value.endsWith('%')
? Math.round(parsedValue * 2.55)
: parsedValue;
});
return [r, g, b, fromAlphaToFloat(match[4])];
}
}
/**
* Returns new color object, when given a color in HSL format
* @param {String} color Color value ex: hsl(0-260,0%-100%,0%-100%)
* @memberOf Color
* @return {Color}
*/
static fromHsl(color: string): Color {
return Color.fromHsla(color);
}
/**
* Returns new color object, when given a color in HSLA format
* @static
* @function
* @memberOf Color
* @param {String} color
* @return {Color}
*/
static fromHsla(color: string): Color {
return new Color(Color.sourceFromHsl(color));
}
/**
* Returns array representation (ex: [100, 100, 200, 1]) of a color that's in HSL or HSLA format.
* Adapted from <a href="https://rawgithub.com/mjijackson/mjijackson.github.com/master/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript.html">https://github.com/mjijackson</a>
* @memberOf Color
* @param {String} color Color value ex: hsl(0-360,0%-100%,0%-100%) or hsla(0-360,0%-100%,0%-100%, 0-1)
* @return {TRGBAColorSource | undefined} source
* @see http://http://www.w3.org/TR/css3-color/#hsl-color
*/
static sourceFromHsl(color: string): TRGBAColorSource | undefined {
const match = color.match(reHSLa());
if (!match) {
return;
}
const h = (((parseFloat(match[1]) % 360) + 360) % 360) / 360,
s = parseFloat(match[2]) / 100,
l = parseFloat(match[3]) / 100;
let r: number, g: number, b: number;
if (s === 0) {
r = g = b = l;
} else {
const q = l <= 0.5 ? l * (s + 1) : l + s - l * s,
p = l * 2 - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return [
Math.round(r * 255),
Math.round(g * 255),
Math.round(b * 255),
fromAlphaToFloat(match[4]),
];
}
/**
* Returns new color object, when given a color in HEX format
* @static
* @memberOf Color
* @param {String} color Color value ex: FF5555
* @return {Color}
*/
static fromHex(color: string): Color {
return new Color(Color.sourceFromHex(color));
}
/**
* Returns array representation (ex: [100, 100, 200, 1]) of a color that's in HEX format
* @static
* @memberOf Color
* @param {String} color ex: FF5555 or FF5544CC (RGBa)
* @return {TRGBAColorSource | undefined} source
*/
static sourceFromHex(color: string): TRGBAColorSource | undefined {
if (color.match(reHex())) {
const value = color.slice(color.indexOf('#') + 1),
isShortNotation = value.length <= 4;
let expandedValue: string[];
if (isShortNotation) {
expandedValue = value.split('').map((hex) => hex + hex);
} else {
expandedValue = value.match(/.{2}/g)!;
}
const [r, g, b, a = 255] = expandedValue.map((hexCouple) =>
parseInt(hexCouple, 16)
);
return [r, g, b, a / 255];
}
}
}