-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
178 lines (155 loc) · 4.78 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Color Test</title>
<style>
html,
body {
margin: 0;
height: 100%;
overflow: hidden;
padding: 10px;
}
p {
margin: 5px;
}
</style>
</head>
<body id="body" style="background-color: rgb(0, 0, 0); color: #000000" onload="createColor(1)">
<p id="hex">HEX #000000</p>
<p id="rgb">RGB (0, 0, 0)</p>
<p id="cmyk">CMYK (0, 0, 0, 0)</p>
<p id="hsl">HSL (0, 0%, 0%)</p>
<script>
var elBody = document.getElementById('body');
var elHex = document.getElementById('hex');
var elRGB = document.getElementById('rgb');
var elCMYK = document.getElementById('cmyk');
var elHSL = document.getElementById('hsl');
var counter = 1;
function createColor(value) {
var v = 255/value;
for( var rStep = 0, r = 0; rStep < v; rStep++) {
for( var gStep = 0, g = 0; gStep < v; gStep++ ) {
for( var bStep = 0, b = 0; bStep < v; bStep++ ) {
changeBackground(r,g,b);
counter += 1;
b += value;
}
g += value;
}
r += value;
}
}
function changeBackground(r, g, b) {
setTimeout(function() {
var rgb = {
r: r,
g: g,
b: b,
};
var hex = convertRGBToHEX(rgb);
var cmyk = convertRGBToCMYK(rgb);
var hsl = convertRGBToHSL(rgb);
var textColor = invertColor(hex);
elBody.style.backgroundColor = 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
elBody.style.color = textColor;
elHex.innerHTML = 'HEX ' + hex;
elRGB.innerHTML = 'RGB (' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ')';
elCMYK.innerHTML = 'CMYK (' + cmyk.c + ', ' + cmyk.m + ', ' + cmyk.y + ', ' + cmyk.k + ')';
elHSL.innerHTML = 'HSL (' + hsl.h + ', ' + hsl.s + ', ' + hsl.l + ')';
}, 1000 * counter)
}
function convertRGBToHEX(rgb) {
var r = rgb.r.toString(16);
var g = rgb.g.toString(16);
var b = rgb.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).toUpperCase();
}
function convertRGBToHSL(rgb) {
var r = rgb.r / 255;
var g = rgb.g / 255;
var b = rgb.b / 255;
var cmin = Math.min(r,g,b),
cmax = Math.max(r,g,b),
delta = cmax - cmin,
h = 0,
s = 0,
l = 0;
if (delta == 0)
h = 0;
else if (cmax == r)
h = ((g - b) / delta) % 6;
else if (cmax == g)
h = (b - r) / delta + 2;
else
h = (r - g) / delta + 4;
h = Math.round(h * 60);
if (h < 0)
h += 360;
l = (cmax + cmin) / 2;
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
s = +(s * 100).toFixed(1);
l = +(l * 100).toFixed(1);
return {
h: h,
s: s + '%',
l: l + '%',
}
}
function convertRGBToCMYK(rgb) {
var c = 1 - (rgb.r / 255);
var m = 1 - (rgb.g / 255);
var y = 1 - (rgb.b / 255);
var k = Math.min(c, Math.min(m, y));
c = (c - k) / (1 - k);
m = (m - k) / (1 - k);
y = (y - k) / (1 - k);
c = Math.round(c * 10000) / 100;
m = Math.round(m * 10000) / 100;
y = Math.round(y * 10000) / 100;
k = Math.round(k * 10000) / 100;
c = isNaN(c) ? 0 : c;
m = isNaN(m) ? 0 : m;
y = isNaN(y) ? 0 : y;
k = isNaN(k) ? 0 : k;
return {
c: c,
m: m,
y: y,
k: k
}
}
function invertColor(hex) {
if (hex.indexOf('#') === 0) {
hex = hex.slice(1);
}
// convert 3-digit hex to 6-digits.
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
if (hex.length !== 6) {
throw new Error('Invalid HEX color.');
}
// invert color components
var r = (255 - parseInt(hex.slice(0, 2), 16)).toString(16),
g = (255 - parseInt(hex.slice(2, 4), 16)).toString(16),
b = (255 - parseInt(hex.slice(4, 6), 16)).toString(16);
// pad each with zeros and return
return '#' + padZero(r) + padZero(g) + padZero(b);
}
function padZero(str, len) {
len = len || 2;
var zeros = new Array(len).join('0');
return (zeros + str).slice(-len);
}
</script>
</body>
</html>