-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (90 loc) · 3.23 KB
/
index.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
const hexInput = document.querySelector("#hexInput");
const inputColor = document.querySelector("#inputColor");
const alteredColor = document.querySelector("#alteredColor");
const alteredColorText = document.querySelector("#alteredColorText");
const sliderText = document.querySelector("#sliderText");
const slider = document.querySelector("#slider");
const lightenText = document.querySelector("#lightenText");
const darkenText = document.querySelector("#darkenText");
const toggleBtn = document.querySelector("#toggleBtn");
toggleBtn.addEventListener("click", () => {
if (!lightenText.classList.contains("unselected")) {
lightenText.classList.add("unselected");
darkenText.classList.remove("unselected");
toggleBtn.classList.add("toggled");
} else {
darkenText.classList.add("unselected");
lightenText.classList.remove("unselected");
toggleBtn.classList.remove("toggled");
}
reset();
});
hexInput.addEventListener("keyup", () => {
const hex = hexInput.value;
console.log(hex);
if (!checkHex(hex)) return;
const strippedHEx = hex.replace("#", "");
inputColor.style.backgroundColor = "#" + strippedHEx;
reset();
});
const checkHex = (hex) => {
if (!hex) return true;
const strippedHEx = hex.replace("#", "");
return strippedHEx.length === 3 || strippedHEx.length === 6;
};
const convertHexToRGB = (hex) => {
if (!checkHex(hex)) return null;
let strippedHex = hex.replace("#", "");
if (strippedHex.length === 3) {
strippedHex =
strippedHex[0] +
strippedHex[0] +
strippedHex[1] +
strippedHex[1] +
strippedHex[2] +
strippedHex[2];
}
const r = parseInt(strippedHex.substring(0, 2), 16);
const g = parseInt(strippedHex.substring(2, 4), 16);
const b = parseInt(strippedHex.substring(4, 6), 16);
return { r, g, b };
};
const convertRGBToHex = (r, g, b) => {
const firstPair = ("0" + r.toString(16)).slice(-2);
const secondPair = ("0" + g.toString(16)).slice(-2);
const thirdPair = ("0" + b.toString(16)).slice(-2);
const hex = "#" + firstPair + secondPair + thirdPair;
return hex;
};
const alterColor = (hex, percentage) => {
const { r, g, b } = convertHexToRGB(hex);
const amount = Math.floor((percentage / 100) * 255);
const newR = belowOrAboveRange(r, amount);
const newG = belowOrAboveRange(g, amount);
const newB = belowOrAboveRange(b, amount);
return convertRGBToHex(newR, newG, newB);
};
const belowOrAboveRange = (hex, amount) => {
// const newHex = hex + amount;
// if (newHex > 255) return 255;
// if (newHex < 0) return 0;
// return newHex;
return Math.min(255, Math.max(0, hex + amount));
};
alterColor("#fff", 10);
slider.addEventListener("input", () => {
if (!checkHex(hexInput.value)) return;
sliderText.textContent = `${slider.value}%`;
const valueAddition = toggleBtn.classList.contains("toggled")
? -slider.value
: slider.value;
const alteredHex = alterColor(hexInput.value, valueAddition);
alteredColor.style.backgroundColor = alteredHex;
alteredColorText.innerText = `Altered Color ${alteredHex}`;
});
const reset = () => {
slider.value = 0;
sliderText.textContent = `${slider.value}%`;
alteredColor.style.backgroundColor = hexInput.value;
alteredColorText.innerText = `Altered Color ${hexInput.value}`;
};