-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
77 lines (62 loc) · 2.02 KB
/
main.js
File metadata and controls
77 lines (62 loc) · 2.02 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
window.addEventListener("DOMContentLoaded", (event) => {
let inputs = document.querySelectorAll("input");
let container = document.querySelector(".container");
let colorValue = document.querySelector("p");
let values = {
red: 255,
green: 255,
blue: 255,
opacity: 100,
};
inputs.forEach((input) =>
input.addEventListener("input", function (event) {
let label = event.target.previousElementSibling;
values[event.target.id] = event.target.value;
let colors = `rgba(${values.red},${values.green},${values.blue},${
values.opacity / 100
})`;
console.log(colors);
container.style.backgroundColor = colors;
if (lightOrDark(container.style.backgroundColor) === "dark") {
container.style.color = "white";
} else {
container.style.color = "black";
}
label.innerText = `${event.target.id}: ${
event.target.id === "opacity"
? event.target.value / 100
: event.target.value
}`;
colorValue.innerText = `Current color value: ${colors}`;
})
);
});
console.log("outside window");
function lightOrDark(color) {
// Variables for red, green, blue values
var r, g, b, hsp;
// Check the format of the color, HEX or RGB?
if (color.match(/^rgb/)) {
// If RGB --> store the red, green, blue values in separate variables
color = color.match(
/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/
);
r = color[1];
g = color[2];
b = color[3];
} else {
// If hex --> Convert it to RGB: http://gist.github.com/983661
color = +("0x" + color.slice(1).replace(color.length < 5 && /./g, "$&$&"));
r = color >> 16;
g = (color >> 8) & 255;
b = color & 255;
}
// HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b));
// Using the HSP value, determine whether the color is light or dark
if (hsp > 127.5) {
return "light";
} else {
return "dark";
}
}