-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcolor.js
304 lines (274 loc) · 7.52 KB
/
color.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
import { colord, extend } from "colord";
import namesPlugin from "colord/plugins/names";
import mixPlugin from "colord/plugins/mix";
import { truncate, constrain, degreeCircle } from "@pencil.js/math";
extend([namesPlugin, mixPlugin]);
/**
* @module Color
*/
const parsedKey = Symbol("_parsed");
/**
* Color class
* @class
*/
export default class Color {
/**
* @typedef {Color|String|Number|Array<Number>} ColorDefinition
*/
/**
* Color constructor
* @param {ColorDefinition} colorDefinition - Many types accepted (other Color instance, color name, hex string, hex number, red/green/blue/alpha value)
* @example
* new Color("indigo"); // Any valid CSS color name
* new Color("#123456"); // Hex string definition
* new Color("#123"); // Hex shorthand string definition, #123 <=> #112233
* new Color(0x123456); // Hex number definition
* new Color(0.1, 0.2, 0.3); // Red, Green, Blue definition
* Every definition can have one more optional parameter for alpha (opacity)
* new Color("violet", 0.5);
*/
constructor (...colorDefinition) {
this[parsedKey] = null;
this.set(...colorDefinition);
}
/**
* Create a new copy of this color
* @return {Color}
*/
clone () {
return new Color(this);
}
/**
* Get the red channel value
* @return {Number}
*/
get red () {
return this[parsedKey].rgba.r / 255;
}
/**
* Set the red channel
* @param {Number} value - New value
*/
set red (value) {
this[parsedKey].rgba.r = constrain(value * 255, 0, 255);
}
/**
* Get the green channel value
* @return {Number}
*/
get green () {
return this[parsedKey].rgba.g / 255;
}
/**
* Set the green channel
* @param {Number} value - New value
*/
set green (value) {
this[parsedKey].rgba.g = constrain(value * 255, 0, 255);
}
/**
* Get the blue channel value
* @return {Number}
*/
get blue () {
return this[parsedKey].rgba.b / 255;
}
/**
* Set the blue channel
* @param {Number} value - New value
*/
set blue (value) {
this[parsedKey].rgba.b = constrain(value * 255, 0, 255);
}
/**
* Get the transparency channel value
* @return {Number}
*/
get alpha () {
return this[parsedKey].rgba.a;
}
/**
* Set the transparency channel
* @param {Number} value - New value
*/
set alpha (value) {
this[parsedKey].rgba.a = constrain(value, 0, 1);
}
/**
* Return an array with red, green and blue value
* @example [0.1, 0.2, 0.3]
* @return {Array<Number>}
*/
get array () {
return [
this.red,
this.green,
this.blue,
];
}
/**
* Return hexadecimal rgb notation
* @example "#123456"
* @return {String}
*/
get hex () {
return this[parsedKey].toHex();
}
/**
* Return hexadecimal rgb notation
* @example "#123456"
* @return {String}
*/
get rgb () {
return this.hex;
}
/**
* Return the closest CSS color name
* @example "aliceblue"
* @return {String}
*/
get name () {
return this[parsedKey].toName({
closest: true,
});
}
// TODO: do we need more getters ? User only need to interact with Color, not read values.
/**
* Change this values
* @param {ColorDefinition} colorDefinition - Any supported color definition (see constructor)
* @return {Color} Itself
*/
set (...colorDefinition) {
const [first] = colorDefinition;
let input;
let alpha;
if (first instanceof Color) {
input = first[parsedKey].rgba;
[, alpha = input.a] = colorDefinition;
}
else if (typeof first === "string") {
input = first;
[, alpha = 1] = colorDefinition;
}
else if (first === undefined || first === null) {
input = "#000";
alpha = 1;
}
else if (colorDefinition.length < 3) {
input = `#${truncate(colorDefinition[0]).toString(16)}`;
[, alpha = 1] = colorDefinition;
}
else {
const [r, g, b] = colorDefinition.slice(0, 3).map(value => value * 255);
[,,, alpha = 1] = colorDefinition;
input = {
r,
g,
b,
};
}
this[parsedKey] = colord(input).alpha(alpha);
return this;
}
/**
* Change to its greyscale value
* @return {Color} Itself
*/
grey () {
this[parsedKey] = this[parsedKey].grayscale();
return this;
}
/**
* Change hue value (0 = red, 0.5 = blue, 1 = red, 1.5 = blue ...)
* @param {Number} value - Any value between 0 and 1
* @return {Color} Itself
*/
hue (value) {
this[parsedKey] = this[parsedKey].hue(value * degreeCircle);
return this;
}
/**
* Change saturation value (0 = grey, 1 = pure color)
* @param {Number} value - Any value between 0 and 1
* @return {Color} Itself
*/
saturation (value) {
const hsl = this[parsedKey].toHsl();
hsl.s = value * 100;
this[parsedKey] = colord(hsl);
return this;
}
/**
* Change lightness value (0 = black, 0.5 = pure color, 1 = white)
* @param {Number} value - Any value between 0 and 1
* @return {Color} Itself
*/
lightness (value) {
const hsl = this[parsedKey].toHsl();
hsl.l = value * 100;
this[parsedKey] = colord(hsl);
return this;
}
/**
* Invert the color value
* @return {Color} Itself
*/
reverse () {
this[parsedKey] = this[parsedKey].invert();
return this;
}
/**
* Restrict the color space to an amount of possible value
* @param {Number} number - Number of allowed value
* @return {Color} Itself
*/
level (number) {
const { r, g, b } = this[parsedKey].rgba;
const p = 255 / number;
this[parsedKey].rgba.r = truncate(r / p) * p + (p / 2);
this[parsedKey].rgba.g = truncate(g / p) * p + (p / 2);
this[parsedKey].rgba.b = truncate(b / p) * p + (p / 2);
return this;
}
/**
* Change the color toward another color
* @param {ColorDefinition} colorDefinition - Any other color
* @param {Number} ratio - Ratio of distance to move (0 = no change, 0.5 = equal mix, 1 = same as target color)
* @return {Color} Itself
*/
lerp (colorDefinition, ratio) {
const color = Color.from(colorDefinition);
this[parsedKey] = this[parsedKey].mix(color[parsedKey], ratio);
return this;
}
/**
* @return {String}
*/
toString () {
return this.hex;
}
/**
* Return a json ready array
* @return {Array<Number>}
*/
toJSON () {
return [
...this.array,
this.alpha,
];
}
/**
* Return an instance from a generic definition
* @param {ColorDefinition} colorDefinition - Any valid color definition (see constructor)
* @return {Color}
*/
static from (...colorDefinition) {
const param = colorDefinition[0];
if (param instanceof Color) {
return param;
}
return new Color(...colorDefinition);
}
}
Color.prototype.gray = Color.prototype.grey;
Color.prototype.mix = Color.prototype.lerp;