-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.js
400 lines (336 loc) · 9.23 KB
/
colors.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
/*global window*/
"use strict";
(function() {
/**
* Represents a single color
*
* @param {string} color - A representation of a color as RGB(A),HSL(A),Hexadecimal(with/without hash)
*
* @returns {void} Returns false on parsing error
*/
var Color = function(color) {
var red = null,
green = null,
blue = null,
alpha = null,
valid = false,
convert = null;
/**
* Has the color been validated yet?
*
* @returns {bool}
*/
this.isValid = function() {
return valid;
};
/**
* Gets the RGB(A) representation of this color
*
* @param {boolean} withAlpha - Whether or not to include alpha in the outputted string
*
* @returns {string} the RGB(A) representation of this color
*/
this.getRGB = function(withAlpha) {
if ( withAlpha === true ) {
return "rgba(" + red + "," + green + "," + blue + "," + alpha.toFixed(2) + ")";
} else {
return "rgb(" + red + "," + green + "," + blue + ")";
}
};
/**
* Get the amount of red in this color in base 10
*
* @returns {int}
*/
this.getRed = function() {
return red;
};
/**
* Get the amount of green in this color in base 10
*
* @returns {int}
*/
this.getGreen = function() {
return green;
};
/**
* Get the amount of blue in this color in base 10
*
* @returns {int}
*/
this.getBlue = function() {
return blue;
};
/**
* Get the amount of alpha in this color
*
* @returns {double}
*/
this.getAlpha = function() {
return alpha;
};
/**
* Gets the HSL representation of this color
*
* @param {boolean} withAlpha - Whether or not to include alpha in the outputted string
*
* @returns {string} the HSL(A) representation of this color
*/
this.getHSL = function(withAlpha) {
var str,
convert = new Convert(),
hsl;
if ( withAlpha ) {
str = "hsla(";
} else {
str = "hsl(";
}
hsl = convert.rgbToHsl(this.getRed(), this.getGreen(), this.getBlue());
str += Math.round(hsl[0] * 360) + ",";
str += Math.round( hsl[1] * 100 ) + "%,";
str += Math.round( hsl[2] * 100 ) + "%";
if ( withAlpha ) {
str += "," + alpha;
}
str += ")";
return str;
};
/**
* Gets the Hexadecimal representation of this color
*
* @param {boolean} withHash - Whether or not to include the hash mark at the front of the code
*
* @returns {string} the Hexadecimal representation of this color
*/
this.getHex = function(withHash) {
var str;
if ( withHash ) {
str = "#";
} else {
str = "";
}
str += this.getRed().toString(16).toUpperCase();
str += this.getGreen().toString(16).toUpperCase();
str += this.getBlue().toString(16).toUpperCase();
return str;
};
/**
* Retuns a new Colors.Color object that is the complement color of this one
*
* @returns {Colors.Color} the complement of this color
*/
this.getComplement = function() {
var hsl, rgb,
color,
convert;
convert = new Convert();
hsl = convert.rgbToHsl(this.getRed(), this.getGreen(), this.getBlue());
hsl[0] = hsl[0] * 360;
hsl[0] += 180;
rgb = convert.hslToRgb(hsl[0] / 360, hsl[1], hsl[2]);
return new Color(rgb[0], rgb[1], rgb[2]);
};
/**
* Retuns a new Colors.Color object that is the greyscale of this one
*
* @returns {Colors.Color} the greyscale color of this one
*/
this.getGreyscale = function() {
};
/**
* mix another color into this one
*
* @param {Colors.Color} mixin - The color you want to mix in
* @param {double} amount - Between 0 and 1, how much you want to mix it in (defaults to 0.5)
*
* @returns {void}
*/
this.mix = function(mixin, amount) {
};
/**
* lighten this color
*
* @param {double} amount - Between 0 and 1, how much lighter do you want this? (defaults to 0.05 or 1/20th of a step)
*
* @returns {void}
*/
this.lighten = function(amount) {
};
/**
* darken this color
*
* @param {double} amount - Between 0 and 1, how much darker do you want this? (defaults to 0.05 or 1/20th of a step)
*
* @returns {void}
*/
this.darken = function(amount) {
};
/**
* is this a "dark" color?
*
* @returns {boolean} Whether or not this is a "dark" color
*/
this.isDark = function() {
};
/**
* is this a "light" color?
*
* @returns {boolean} Whether or not this is a "light" color
*/
this.isLight = function() {
};
/**
* is this a "warm" color?
*
* @returns {boolean} Whether or not this is a "warm" color
*/
this.isWarm = function() {
};
/**
* is this a "cold" color?
*
* @returns {boolean} Whether or not this is a "cold" color
*/
this.isCold = function() {
};
/**
* randomizes the color
*
* @returns {void}
*/
this.randomize = function() {
};
/**
* Parse out what we've been given coming in
*/
var parseColor = function(color) {
var matches = [],
hsl;
if ( color.match(/^#?[0-9a-f]{6}$/i) ) {
if ( color.length == 7 ) {
color = color.substr(1);
}
red = parseInt(color.substr(0,2), 16);
green = parseInt(color.substr(2,2), 16);
blue = parseInt(color.substr(4,2), 16);
alpha = 1;
// successful parsing, continue
valid = true;
return true;
} else if ( matches = color.match(/rgba\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*([0-9\.]+)\)/i) ) {
red = matches[1];
green = matches[2];
blue = matches[3];
alpha = matches[4];
valid = true;
return true;
} else if ( matches = color.match(/rgb\((\d{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\)/i) ) {
red = matches[1];
green = matches[2];
blue = matches[3];
alpha = 1;
valid = true;
return true;
} else if ( matches = color.match(/hsl\((\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\)/i) ) {
alpha = 1;
convert = new Convert();
hsl = convert.hslToRgb(parseInt(matches[1]) / 360, matches[2] / 100, matches[3] / 100);
red = hsl[0];
green = hsl[1];
blue = hsl[2];
valid = true;
return true;
} else if ( matches = color.match(/hsla\((\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*([0-9\.]+)\)/i) ) {
convert = new Convert();
hsl = convert.hslToRgb(parseInt(matches[1]) / 360, matches[2] / 100, matches[3] / 100);
red = hsl[0];
green = hsl[1];
blue = hsl[2];
alpha = matches[4];
valid = true;
return true;
} else {
valid = false;
return false;
}
};
if ( arguments.length == 3 ) {
//special case for being handed rgb directly
red = arguments[0];
green = arguments[1];
blue = arguments[2];
alpha = 1;
valid = true;
} else if ( !parseColor(color) ) {
valid = false;
}
};
var Convert = function() {
this.hslToRgb = function(h, s, l) {
var r, g, b,
ret;
if ( s == 0 ) {
ret = [l,l,l];
return [Math.round(l * 255),Math.round(l * 255),Math.round(l * 255)];
} else {
var q = l < 0.5 ? l * ( 1.0 + s ) : l + s - l * s;
var p = 2.0 * l - q;
r = hueToRgb(p, q, h + 1 / 3);
g = hueToRgb(p, q, h);
b = hueToRgb(p, q, h - 1 / 3);
ret = [r,g,b];
}
return [Math.round(ret[0] * 255), Math.round(ret[1] * 255), Math.round(ret[2] * 255)];
function hueToRgb(p, q, t) {
if ( t < 0 ) t += 1;
/* istanbul ignore if */
if ( t > 1 ) t -= 1;
if ( t < 1 / 6 ) return p + (q - p) * 6 * t;
if ( t < 1 / 2 ) return q;
if ( t < 2 / 3 ) return p + (q - p) * ( 2 / 3 - t ) * 6;
return p;
}
};
this.rgbToHsl = function(r, g, b) {
var h, s, l,
r, g, b,
min, max,
d;
r = r / 255;
g = g / 255;
b = b / 255;
max = ( r > g && r > b ) ? r : ( g > b ) ? g : b;
min = ( r < g && r < b ) ? r : ( g < b ) ? g : b;
h = s = l = (min + max) / 2;
//achromatic
if ( max == min ) {
h = s = 0;
} else {
d = max - min;
s = ( l > 0.5) ? d / ( 2 - max - min) : d / ( max + min );
if ( r > g && r > b ) {
h = ( g - b ) / d + ( g < b ? 6 : 0 );
} else if ( g > b ) {
h = ( b - r ) / d + 2;
} else {
h = ( r - g ) / d + 4;
}
h = h / 6;
}
return [h,s,l];
};
};
var Colors = function() {
// Assign our internal color class
this.Color = Color;
this.Convert = new Convert();
return this;
};
/**
* only drop it indow global window scope if a window exists
*/
/* istanbul ignore else */
if ( window && !window.Colors ) {
window.Colors = new Colors();
}
})();