-
Notifications
You must be signed in to change notification settings - Fork 0
/
coloressascripts.js
692 lines (578 loc) · 18.9 KB
/
coloressascripts.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// STATE VARIABLES
let starterPalette = ["#a91413",
"#8B0000",
"#FF0000",
"#FF0033",
"#FF5367",
"#ff4f00",
"#ff7e00",
"#ff8c00",
"#ffa500",
"#ff9955",
"#fac809",
"#ffd700",
"#ecd540",
"#cba135",
"#fefe33",
"#ffff00",
"#FFFFE0",
"#faec66",
"#fff3ad",
"#D2B48C",
"#F5F5DC",
"#FFFFF0",
"#50771d",
"#00ba84",
"#00ff00",
"#93d207",
"#32cd32",
"#90ee90",
"#85bb65",
"#b6a767",
"#228b22",
"#2F4F2F",
"#90EE90",
"#30d5c8",
"#63cec4",
"#59e1eb",
"#c7d5c7",
"#00ffff",
"#a4f4f9",
"#0bb9dd",
"#329bdb",
"#1e90ff",
"#000080",
"#1f108a",
"#0047ab",
"#0000ff",
"#1c1cf0",
"#4166f5",
"#4169e1",
"#21aefb",
"#ADD8E6",
"#0fc0fc",
"#00bfff",
"#77b5fe",
"#876edf",
"#ffaefb",
"#d584fb",
"#ff77ff",
"#ff00ff",
"#fba0e3",
"#f8def0",
"#ff33cc",
"#e101c1",
"#db03a9",
"#800080",
"#702a5d",
"#cb2aae",
"#ff1493",
"#fc65a5",
"#ffc0cb",
"#ff69b4",
"#773733",
"#672154",
"#000000",
"#808080",
"#C0C0C0",
];
function hexToRgb2(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function HEXtoHSL(hex) {
hex = hex.replace(/#/g, '');
if (hex.length === 3) {
hex = hex.split('').map(function (hex) {
return hex + hex;
}).join('');
}
var result = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})[\da-z]{0,0}$/i.exec(hex);
if (!result) {
return null;
}
var r = parseInt(result[1], 16);
var g = parseInt(result[2], 16);
var b = parseInt(result[3], 16);
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b),
min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0;
} else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
}
h /= 6;
}
s = s * 100;
s = Math.round(s);
l = l * 100;
l = Math.round(l);
h = Math.round(360 * h);
return {
h: h,
s: s,
l: l
};
}
function hexToCMYK(hex) {
var computedC = 0;
var computedM = 0;
var computedY = 0;
var computedK = 0;
hex = (hex.charAt(0) == "#") ? hex.substring(1, 7) : hex;
if (hex.length != 6) {
alert('Invalid length of the input hex value!');
return;
}
if (/[0-9a-f]{6}/i.test(hex) != true) {
alert('Invalid digits in the input hex value!');
return;
}
var r = parseInt(hex.substring(0, 2), 16);
var g = parseInt(hex.substring(2, 4), 16);
var b = parseInt(hex.substring(4, 6), 16);
// BLACK
if (r == 0 && g == 0 && b == 0) {
computedK = 1;
return [0, 0, 0, 1];
}
computedC = 1 - (r / 255);
computedM = 1 - (g / 255);
computedY = 1 - (b / 255);
var minCMY = Math.min(computedC, Math.min(computedM, computedY));
computedC = (computedC - minCMY) / (1 - minCMY);
computedM = (computedM - minCMY) / (1 - minCMY);
computedY = (computedY - minCMY) / (1 - minCMY);
computedK = minCMY;
return [computedC, computedM, computedY, computedK];
}
function randomInteger(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
function cutHex(h) {
return (h.charAt(0) == "#") ? h.substring(1, 7) : h;
}
function hexToR(h) {
return parseInt((cutHex(h)).substring(0, 2), 16);
}
function hexToG(h) {
return parseInt((cutHex(h)).substring(2, 4), 16);
}
function hexToB(h) {
return parseInt((cutHex(h)).substring(4, 6), 16);
}
function tohex(n) {
n = parseInt(n, 10);
if (isNaN(n)) {
return "00";
}
n = Math.max(0, Math.min(n, 255));
var finalhex = "0123456789ABCDEF".charAt((n - n % 16) / 16) + "0123456789ABCDEF".charAt(n % 16);
return finalhex;
}
function rgbtohex(R, G, B) {
var finalhex = "#" + tohex(R) + tohex(G) + tohex(B);
return finalhex;
}
function scrollToPageTop() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
function RGBToHex(r, g, b) {
r = r.toString(16);
g = g.toString(16);
b = b.toString(16);
if (r.length == 1)
r = "0" + r;
if (g.length == 1)
g = "0" + g;
if (b.length == 1)
b = "0" + b;
return "#" + r + g + b;
}
// Zindagi na mileage dubara
function percievedBrightness(r, g, b) {
//alert(Math.sqrt(0.299 * Math.pow(r, 2) + 0.587 * Math.pow(g, 2) + 0.114 * Math.pow(b, 2)));
return parseInt(Math.sqrt(0.299 * Math.pow(r, 2) + 0.587 * Math.pow(g, 2) + 0.114 * Math.pow(b, 2)));
}
function getColorBox(hexColor) {
return `<div class="colorbox" onClick="copyToClipboard(${hexColor})"
data-clipboard-text="${hexColor}"
style="background:${hexColor};"> ${hexColor} </div>`;
}
/*
IDs of relevant DOM elements to change when a new color is selected
currentcolorheaderdisplay - big text at top of the panel. Only hex code placed here.
*/
function setCurrentColor(colorHexCode) {
let hexCodeString = String(colorHexCode);
// Empty Current Containers of possible previous value
$('#currentcolorstorage').empty();
$('#currentcolorstorage').text(hexCodeString);
$('#currentcolorstorage').empty();
$('#currentcolorheaderdisplay').text(hexCodeString);
$('#currentcolorheaderdisplay').css("border-top-color", hexCodeString);
let rValue = hexToR(colorHexCode);
let gValue = hexToG(colorHexCode);
let bValue = hexToB(colorHexCode);
let cmykArray = hexToCMYK(colorHexCode);
$("#currentcolorindicatorcircle").css("background-color", hexCodeString);
let rgbCompositeString = `rgb(${rValue},${gValue},${bValue})`;
$('#currentcolorboxrgbvalue').empty();
$('#currentcolorboxrgbvalue').text(rgbCompositeString);
let hslObject = HEXtoHSL(hexCodeString)
$('#currentcolorboxhslvalue').empty();
$('#currentcolorboxhslvalue').html(`hsl(${hslObject.h}, ${hslObject.s}%, ${hslObject.l}%)`);
$('#currentcolorboxcmykvalue').empty();
$('#currentcolorboxcmykvalue').text(`${cmykArray[0].toFixed(4)}, ${cmykArray[1].toFixed(4)}, ${cmykArray[2].toFixed(4)}, ${cmykArray[3].toFixed(4)}`);
$('#currentcolorluminancevalue').empty();
currentcolorluminancepercentvalue
$('#currentcolorluminancepercentvalue').text(String(parseInt(percievedBrightness(rValue, gValue, bValue) / 255 * 100)) + "%");
$('#currentcolorluminancevalue').text(String(percievedBrightness(rValue, gValue, bValue)) + " / 255");
}
function toggleCurrentColorBox() {
if ($('#currentColorVisibile').is(':visible')) {
$('#currentColorToggle').text("SHOW");
$('#currentColorVisibile').hide("slow");
}
if ($('#currentColorVisibile').is(':hidden')) {
$('#currentColorToggle').text("HIDE");
$('#currentColorVisibile').show("slow");
}
}
function generateVariant(hexColor) {
let hexVariant = RGBToHex(r, g, b);
var elm = getColorBox(hexColor);
$(elm).appendTo($("#variantscontainer"));
}
function uplimit(number) {
if (number > 255) {
return 255;
} else {
return number;
}
}
function downlimit(number) {
if (number < 0) {
return 0;
} else {
return number;
}
}
function getvarianthexcode(hexcolorcode, variantamount) {
let redamount = hexToR(hexcolorcode);
let greenamount = hexToG(hexcolorcode);
let blueamount = hexToB(hexcolorcode);
let redmin = redamount - variantamount;
let redmax = redamount + variantamount;
redmax = uplimit(redmax);
redmin = downlimit(redmin);
var greenmin = greenamount - variantamount;
var greenmax = greenamount + variantamount;
greenmax = uplimit(greenmax);
greenmin = downlimit(greenmin);
var bluemin = blueamount - variantamount;
var bluemax = blueamount + variantamount;
bluemax = uplimit(bluemax);
bluemin = downlimit(bluemin);
var newred = randomInteger(redmin, redmax);
var newgreen = randomInteger(greenmin, greenmax);
var newblue = randomInteger(bluemin, bluemax);
let hexVariant = rgbtohex(newred, newgreen, newblue);
return hexVariant;
}
function getvariant(hexcolorcode, variantamount) {
let redamount = hexToR(hexcolorcode);
let greenamount = hexToG(hexcolorcode);
let blueamount = hexToB(hexcolorcode);
// 20 is 8 percent of 255...so the variants are within
//var variantamount = 20;
let redmin = redamount - variantamount;
let redmax = redamount + variantamount;
redmax = uplimit(redmax);
redmin = downlimit(redmin);
var greenmin = greenamount - variantamount;
var greenmax = greenamount + variantamount;
greenmax = uplimit(greenmax);
greenmin = downlimit(greenmin);
var bluemin = blueamount - variantamount;
var bluemax = blueamount + variantamount;
bluemax = uplimit(bluemax);
bluemin = downlimit(bluemin);
var newred = randomInteger(redmin, redmax);
var newgreen = randomInteger(greenmin, greenmax);
var newblue = randomInteger(bluemin, bluemax);
let hexVariant = rgbtohex(newred, newgreen, newblue);
var elm = getColorBox(hexVariant);
$(elm).appendTo($(".variantscontainer"));
}
function shadeColor(col, amt) {
col = col.replace(/^#/, '')
if (col.length === 3) col = col[0] + col[0] + col[1] + col[1] + col[2] + col[2]
let [r, g, b] = col.match(/.{2}/g);
([r, g, b] = [parseInt(r, 16) + amt, parseInt(g, 16) + amt, parseInt(b, 16) + amt])
r = Math.max(Math.min(255, r), 0).toString(16)
g = Math.max(Math.min(255, g), 0).toString(16)
b = Math.max(Math.min(255, b), 0).toString(16)
const rr = (r.length < 2 ? '0' : '') + r
const gg = (g.length < 2 ? '0' : '') + g
const bb = (b.length < 2 ? '0' : '') + b
return `#${rr}${gg}${bb}`
}
function shadeColorX(color, percent) {
var R = parseInt(color.substring(1, 3), 16);
var G = parseInt(color.substring(3, 5), 16);
var B = parseInt(color.substring(5, 7), 16);
R = parseInt(R * (100 + percent) / 100);
G = parseInt(G * (100 + percent) / 100);
B = parseInt(B * (100 + percent) / 100);
R = (R < 255) ? R : 255;
G = (G < 255) ? G : 255;
B = (B < 255) ? B : 255;
var RR = ((R.toString(16).length == 1) ? "0" + R.toString(16) : R.toString(16));
var GG = ((G.toString(16).length == 1) ? "0" + G.toString(16) : G.toString(16));
var BB = ((B.toString(16).length == 1) ? "0" + B.toString(16) : B.toString(16));
return "#" + RR + GG + BB;
}
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function hslToHex(h, s, l) {
l /= 100;
const a = s * Math.min(l, 1 - l) / 100;
const f = n => {
const k = (n + h / 30) % 12;
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
return Math.round(255 * color).toString(16).padStart(2, '0'); // convert to Hex and prefix "0" if needed
};
return `#${f(0)}${f(8)}${f(4)}`;
}
function displayneutrals() {
let currentInt = 255;
let endingInt = 0;
let currentColorInHex;
for (let i = 0; i < 120; i++) {
if (currentInt >= 0) {
currentColorInHex = rgbToHex(currentInt, currentInt, currentInt);
var elm = getColorBox(currentColorInHex);
$(elm).appendTo($("#neutralscontainer"));
currentInt = currentInt - 3;
}
}
}
function displaymorecolors() {
const factor = [-7, -5, -3, 0, 2, 4, 6, 8, 10];
let currentR = randomInteger(0, 255);
let currentG = randomInteger(0, 225);
let currentB = randomInteger(0, 255);
let randomRFactor = randomInteger(-5, 5);
let randomGFactor = randomInteger(-5, 5);
let randomBFactor = randomInteger(-5, 5);
let endingInt = 0;
let currentColorInHex;
// FIRST ITERATION
for (let i = 0; i < 100; i++) {
currentColorInHex = rgbToHex(
randomInteger(0, 255),
randomInteger(0, 255),
randomInteger(0, 255));
let j = 0;
// Decide if we want to lighten or darken the color in the while loop
let lightenordarken = randomInteger(0, 1);
while (j < 8) {
var elm = getColorBox(currentColorInHex);
$(elm).appendTo($("#morecolorscontainer"));
j++;
if (lightenordarken == 0) {
// This brightens the color
currentColorInHex = shadeColor(currentColorInHex, randomInteger(14, 21));
} else {
// This darkens the color
currentColorInHex = shadeColor(currentColorInHex, randomInteger(-20, -14));
}
if (currentColorInHex == "#ffffff" || currentColorInHex == "#000000") {
break;
}
if (currentR <= 0) {
currentR = 0;
}
if (currentG <= 0) {
currentG = 0;
}
if (currentB <= 0) {
currentB = 0;
}
if (currentR >= 255) {
currentR = 255;
}
if (currentG >= 255) {
currentG = 255;
}
if (currentB >= 255) {
currentB = 255;
}
} // End of While loop
} // End of for loop
}
function createShades(hexColor) {
$("#shadesmegacontainer").fadeIn();
$('html, body').animate({
scrollTop: $("#shadesmegacontainer").offset().top
}, 1000);
$(".shadesContainer").empty();
let currentInt = 180;
let shadeColor1 = shadeColor(hexColor, currentInt);
$("#shadesContainer").empty();
$("#shadeshexlabel").text(hexColor);
// FIRST SHOW THE ORIGINAL COLOR
var elm2 = `<div class="colorbox" onClick="copyToClipboard(${hexColor})"
data-clipboard-text="${hexColor}"
style="background:${hexColor};"> ${hexColor} </div>`;
$(elm2).appendTo($("#shadescontainer"));
let whitecounter = 0;
for (let i = 0; i < 140; i++) {
shadeColor1 = shadeColor(hexColor, currentInt);
if (shadeColor1 === "#ffffff") {
whitecounter++;
}
if (/^#[0-9A-F]{6}$/i.test(shadeColor1)) {
var elm = `<div class="colorbox"
onClick="copyToClipboard(${shadeColor1})"
data-clipboard-text="${shadeColor1}"
data-hexcolor="${shadeColor1}"
style="background:${shadeColor1};"> ${shadeColor1} </div>`;
// If the Color ISNT WHite
if (shadeColor1 !== "#ffffff") {
$(elm).appendTo($("#shadescontainer"));
}
// If the Color is WHITE
else {
if (whitecounter <= 1) {
$(elm).appendTo($("#shadescontainer"));
}
}
}
// Allow only one shade box of black to show
if (shadeColor1 === "#000000") {
return;
}
currentInt = currentInt - 5;
shadeColor1 = shadeColor(hexColor, currentInt);
} // End of For loop
// Highlight the color box of the base color
$(`[data-hexcolor="${hexColor}"]`).removeClass("colorbox");
$(`[data-hexcolor="${hexColor}"]`).removeClass("colorbox2");
$(`[data-hexcolor="${hexColor}"]`).css("background-color", "yellow");
}
function createVariants() {
let hexColor = $("#shadeshexlabel").text();
$("#variantsmegacontainer").fadeIn();
$('html, body').animate({
scrollTop: $("#variantsmegacontainer").offset().top
}, 1000);
$(".variantsContainer").empty();
let currentInt = 180;
let shadeColor1 = shadeColor(hexColor, currentInt);
$("#variantsContainer").empty();
$("#variantshexlabel").text(hexColor);
var elm = `<div class="colorbox" onClick="copyToClipboard(${hexColor})"
data-clipboard-text="${hexColor}"
style="background:${hexColor};"> ${hexColor} </div>`;
$(elm).appendTo($("#variantscontainer"));
for (let i = 0; i < 39; i++) {
let variantR = hexToRgb2(hexColor).r + randomInteger(-32, 32);
let variantG = hexToRgb2(hexColor).g + randomInteger(-32, 32);
let variantB = hexToRgb2(hexColor).b + randomInteger(-32, 32);
if (variantR < 0) {
variantR = 0;
}
if (variantG < 0) {
variantG = 0;
}
if (variantB < 0) {
variantB = 0;
}
if (variantR > 255) {
variantR = 255;
}
if (variantG > 255) {
variantG = 255;
}
if (variantB > 255) {
variantB = 255;
}
let newVariant = rgbtohex(variantR, variantG, variantB);
// var elm = `<div class="colorbox" onClick="copyToClipboard(${newVariant})"
// data-clipboard-text="${newVariant}"
// style="background:${newVariant};"> ${newVariant} </div>`;
let elm = getColorBox(newVariant);
$(elm).appendTo($("#variantscontainer"));
}
}
function createStarterPalette() {
let newVariant = starterPalette[0];
for (let i = 0; i < starterPalette.length; i++) {
newVariant = starterPalette[i];
var elm = `<div class="colorbox" onClick="copyToClipboard(${newVariant})"
data-clipboard-text="${newVariant}"
style="background:${newVariant};"> ${newVariant} </div>`;
$(elm).appendTo($(".startercontainer"));
}
}
function createstarterpalettevariants() {
let newVariant = "";
// Empty the starter palette container
$(".startercontainer").html("");
// Iterate through the array, and create a variant of each color
// and add the color back to the container
for (let i = 0; i < starterPalette.length; i++) {
newVariant = getvarianthexcode(starterPalette[i], randomInteger(3, 19));
var elm = `<div class="colorbox" onClick="copyToClipboard(${newVariant})"
data-clipboard-text="${newVariant}"
style="background:${newVariant};"> ${newVariant} </div>`;
$(elm).appendTo($(".startercontainer"));
}
}
function goToCurrentColor() {
$('html, body').animate({
scrollTop: $("#currentcolorcontainer").offset().top
}, 700);
}
function goToStarterPalette() {
$('html, body').animate({
scrollTop: $("#startermegacontainer").offset().top
}, 700);
}
function goToGrays() {
$('html, body').animate({
scrollTop: $("#grays").offset().top
}, 700);
}
function goToRandomColors() {
$('html, body').animate({
scrollTop: $("#randomcolors").offset().top
}, 700);
}
$(document).ready(function () {
// HIDE the CURRENT COLOR PANEL, since by default there is no current color when page loads
$('#currentColorVisibile').toggle("slow");
});
/* NOTES, TODOs
*/