-
Notifications
You must be signed in to change notification settings - Fork 834
Expand file tree
/
Copy pathcolors.html
More file actions
240 lines (222 loc) · 6.39 KB
/
colors.html
File metadata and controls
240 lines (222 loc) · 6.39 KB
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
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Color syntax converter</title>
<style>
dialog {
border: 1px solid;
border-radius: 5px;
box-shadow: 3px 3px 10px rgb(0 0 0 / 0.2);
background-color: white;
font-family: segue, arial, helvetica, sans-serif;
margin-top: 5vh;
}
table {
width: 100%;
}
td,
th {
padding: 3px 15px;
}
th {
background-color: #ededed;
}
td {
background-color: #dedede;
font-family: monospace;
}
body {
background: linear-gradient(
-90deg,
transparent 0 38%,
47%,
rgb(0 0 0 / 0.7) 50%,
white 50% 100%
),
conic-gradient(
black 0 90deg,
transparent 90deg 180deg,
black 180deg 270deg,
transparent 270deg 360deg
);
background-size:
100% 100%,
20px 20px;
padding: 0;
margin: 0;
}
div {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<div>
<dialog>
<table>
<caption>
Current color values:
</caption>
<tbody>
<tr id="RGB">
<th>RGB</th>
<td></td>
</tr>
<tr id="HEX">
<th>HEX</th>
<td></td>
</tr>
<tr id="HSL">
<th>HSL</th>
<td></td>
</tr>
<tr id="HWB">
<th>HWB</th>
<td></td>
</tr>
<tr id="colorfunc">
<th>color()</th>
<td></td>
</tr>
</tbody>
</table>
<p>
<label for="color">Select a color: </label
><input type="color" id="color" value="#bada55" />
</p>
<p>
<label for="opacity">Select an opacity: </label
><input
type="range"
id="opacity"
value="1"
min="0"
max="1"
step="0.1" />
</p>
</dialog>
</div>
<script>
const colorPicker = document.getElementById("color");
const opacityPicker = document.getElementById("opacity");
document.addEventListener("readystatechange", () => {
document.querySelector("dialog").show();
createColor();
});
colorPicker.addEventListener("change", () => {
createColor();
});
opacityPicker.addEventListener("change", () => {
createColor();
});
function createColor() {
const currentColor = colorPicker.value;
const currentOpacity = Number(opacityPicker.value);
setBackgroundColor(currentColor, currentOpacity);
createHEX(currentColor, currentOpacity);
createRGB(currentColor, currentOpacity);
}
function createHEX(color, opacity) {
const cell = document.querySelector("#HEX td");
if (opacity === 1) {
cell.textContent = color;
} else {
cell.textContent = hexOpacity(color, opacity);
}
}
function createRGB(color, opacity) {
const cell = document.querySelector("#RGB td");
color = color.substring(1, 7);
const hexArray = color.match(/.{1,2}/g);
const R = parseInt(hexArray[0], 16);
const G = parseInt(hexArray[1], 16);
const B = parseInt(hexArray[2], 16);
if (opacity === 1) {
cell.textContent = `rgb(${R} ${G} ${B})`;
} else {
cell.textContent = `rgb(${R} ${G} ${B} / ${opacity})`;
}
createHSL(R, G, B, opacity);
createColorFunc(R, G, B, opacity);
}
function createColorFunc(r, g, b, opacity) {
const cell = document.querySelector("#colorfunc td");
const R = Number((r / 255).toFixed(2));
const G = Number((g / 255).toFixed(2));
const B = Number((b / 255).toFixed(2));
if (opacity === 1) {
cell.textContent = `color(srgb ${R} ${G} ${B})`;
} else {
cell.textContent = `color(srgb ${R} ${G} ${B} / ${opacity})`;
}
}
function createHSL(r, g, b, opacity) {
const cell = document.querySelector("#HSL td");
// Let's have r, g, b in the range [0, 1]
r = r / 255;
g = g / 255;
b = b / 255;
const cmin = Math.min(r, g, b);
const cmax = Math.max(r, g, b);
const delta = cmax - cmin;
let 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);
// We want an angle between 0 and 360°
if (h < 0) {
h += 360;
}
l = (cmax + cmin) / 2;
s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
s = Number((s * 100).toFixed(1));
l = Number((l * 100).toFixed(1));
if (opacity === 1) {
cell.textContent = `hsl(${h} ${s}% ${l}%)`;
} else {
cell.textContent = `hsl(${h} ${s}% ${l}% / ${opacity})`;
}
createHWB(h, s, l, opacity);
}
function createHWB(h, s, l, opacity) {
const cell = document.querySelector("#HWB td");
const chroma = s * (1 - Math.abs(l / 50 - 1));
let W = l - chroma / 2;
let B = 100 - l - chroma / 2;
W = W.toFixed(1);
B = B.toFixed(1);
if (opacity === 1) {
cell.textContent = `hwb(${h} ${W}% ${B}%)`;
} else {
cell.textContent = `hwb(${h} ${W}% ${B}% / ${opacity})`;
}
}
function setBackgroundColor(color, opacity) {
const body = document.querySelector("div");
if (opacity !== 1) {
color = hexOpacity(color, opacity);
}
body.style.backgroundColor = color;
opacityPicker.style.accentColor = color;
colorPicker.style.accentColor = color;
}
function hexOpacity(color, opacity) {
let char = "00";
if (opacity > 0) {
char = Math.floor(opacity * 255).toString(16);
}
return `${color}${char}`;
}
</script>
</body>
</html>