-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
86 lines (77 loc) · 2.44 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
const banners = document.querySelectorAll('.banner');
const locks = document.querySelectorAll('.lock');
const pickers = document.querySelectorAll('#picker');
const texts = document.querySelectorAll('.hex-value');
const generateBtn = document.getElementById('generate');
const localStr = JSON.parse(localStorage.getItem('colors'));
if (localStr) {
banners.forEach((banner, i) => {
banner.style.backgroundColor = localStr[i];
texts[i].innerText = localStr[i];
pickers[i].value = localStr[i];
});
}
locks.forEach((lock, i) => {
lock.addEventListener('click', () => {
banners[i].classList.toggle('locked');
pickers[i].toggleAttribute('disabled');
lock.classList.toggle('locked');
});
});
pickers.forEach((picker, i) => {
picker.addEventListener('input', () => {
banners[i].style.backgroundColor = `${picker.value}`;
texts[i].innerHTML = `${picker.value}`;
updateLS();
});
/*picker.addEventListener('change', () => {
updateLS();
});*/
});
texts.forEach((text) => {
text.addEventListener('click', () => {
let textarea = document.createElement('textarea');
textarea.innerText = text.innerText;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
textarea.remove();
});
});
function generateRGB() {
let rgb = '';
let r = Math.floor(Math.random() * 255);
let g = Math.floor(Math.random() * 255);
let b = Math.floor(Math.random() * 255);
rgb = `rgb(${r}, ${g}, ${b})`;
return rgb;
}
function rgb2hex(rgb) {
rgb = rgb.match(
/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i
);
return rgb && rgb.length === 4
? '#' +
('0' + parseInt(rgb[1], 10).toString(16)).slice(-2) +
('0' + parseInt(rgb[2], 10).toString(16)).slice(-2) +
('0' + parseInt(rgb[3], 10).toString(16)).slice(-2)
: '';
}
generateBtn.addEventListener('click', () => {
banners.forEach((banner, i) => {
if (banner.classList.contains('locked') === false) {
color = rgb2hex(generateRGB());
banner.style.backgroundColor = `${color}`;
texts[i].innerText = `${color}`;
pickers[i].value = `${color}`;
}
});
updateLS();
});
function updateLS() {
colorsArray = [];
banners.forEach((banner) => {
colorsArray.push(rgb2hex(banner.style.backgroundColor));
});
localStorage.setItem('colors', JSON.stringify(colorsArray));
}