-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
131 lines (99 loc) · 3.02 KB
/
script.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
const crc = new ContrastRatioChecker.ContrastRatioChecker();
var setFontSizeTimeout = null;
var setCustomRatioTimeout = null;
var setBoldTimeout = null;
const recalculateRatio = () => {
const [rgb1, rgb2] = ["#color-1 div", "#color-2 div"].map((id) =>
getBackgroundRgbById(id)
);
const result = crc.getContrastRatioByRgb(rgb1, rgb2);
$("#result").text(result.toFixed(2));
};
const setColor = (event) => {
if (!window.EyeDropper) {
alert("Error: Your browser does not support the EyeDropper API");
return;
}
const eyeDropper = new EyeDropper();
eyeDropper
.open()
.then((res) => {
const color = res.sRGBHex;
if (isValidHex(color)) {
event.target.style.backgroundColor = color;
} else {
event.target.style.backgroundColor = rgbaToRgbString(color);
}
recalculateRatio();
recalculateValidators();
})
.catch((err) => {
console.error(err);
});
};
const debounceTime = (cb, timeout, duration) => {
this[timeout] && clearTimeout(this[timeout]);
this[timeout] = setTimeout(cb, duration);
};
const setInput = (event) => {
const target = event.target;
const id = target.id;
switch (id) {
case "bold":
debounceTime(
() => {
bold = target.checked;
recalculateValidators();
},
"setBoldTimeout",
300
);
break;
case "font-size":
debounceTime(
() => {
fontSize = target.value;
recalculateValidators();
},
"setFontSizeTimeout",
300
);
break;
case "custom-ratio":
debounceTime(
() => {
customRatio = target.value;
recalculateValidators();
},
"setCustomRatioTimeout",
300
);
break;
}
};
const toggleSettings = (event) => {
const target = $("#tab-header button").get(0);
const action = target.innerText === "expand_more" ? "open" : "close";
$(target).text(action === "open" ? "expand_less" : "expand_more");
if (action === "open") {
$("#inputs-wrapper").addClass("max-h-48");
} else {
$("#inputs-wrapper").removeClass("max-h-48");
}
};
const copyToClipboard = (text) => {
const clipBoard = navigator.clipboard;
clipBoard.writeText(text).then(() => {
toast("Text copied to clipboard.", "info", {
width: "286px",
zIndex: "100",
duration: 3000,
});
});
};
const copyColor = (evt) => {
const colorNumber = evt.target.parentNode.innerText.match(/Color (.)+/)[1];
const rgb = getBackgroundRgbById(`#color-${colorNumber} div`);
const hex = getHexByRgbObject(rgb);
copyToClipboard(hex);
};