-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (63 loc) · 2.08 KB
/
Copy pathindex.js
File metadata and controls
80 lines (63 loc) · 2.08 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
import Color from "colorjs.io";
import * as contrastAlgorithms from "colorjs.io/src/contrast/index.js";
for (let algo in contrastAlgorithms) {
let algoName = algo.replace(/^contrast/, "");
contrast_algorithm.insertAdjacentHTML("beforeend", `<option>${algoName}</option>`);
}
const root = document.documentElement;
let previousAlgo;
function computeTextColor () {
let algo = contrast_algorithm.value;
previous_algo.textContent = previousAlgo;
current_algo.textContent = algo;
for (let div of colors.children) {
let color = div.color;
let onWhite = Math.abs(color.contrast("white", algo));
let onBlack = Math.abs(color.contrast("black", algo));
let textColor = onWhite > onBlack ? "white" : "black";
let changed = div.style.color && textColor !== div.style.color;
div.style.color = textColor;
div.classList.toggle("changed", changed);
}
previousAlgo = algo;
}
function drawColors () {
colors.innerHTML = "";
let granularity = Math.cbrt(number_of_colors.value);
// root.style.setProperty("--granularity", granularity);
let increment = 1 / granularity;
for (let r = 0; r <= 1; r += increment) {
for (let g = 0; g <= 1; g += increment) {
for (let b = 0; b <= 1; b += increment) {
let color = new Color("srgb", [r, g, b]);
let [l, c, h] = color.getAll("oklch");
l = Math.round(l * 100);
c = Math.round(c * 100);
h = Math.round(h);
let div = document.createElement("div");
div.textContent = "Text";
div.setAttribute("style", `background-color: ${color.display()}; --l: ${l}; --c: ${c}; --h: ${h}`);
div.color = color;
colors.append(div);
}
}
}
computeTextColor();
}
function render (evt) {
if (!evt || evt.target === number_of_colors) {
number_of_colors.title = number_of_colors.value;
drawColors();
}
if (!evt || evt.target === contrast_algorithm) {
computeTextColor();
}
if (!evt || evt.target === show_changes) {
root.classList.toggle("show-changes", show_changes.checked);
}
if (!evt || evt.target === order_by) {
root.dataset.order = order_by.value;
}
}
document.body.addEventListener("input", render);
render();