-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
86 lines (67 loc) · 2.26 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 body = document.querySelector('#body');
const btnRandom = document.querySelector('#btn-random');
const btnCopy = document.querySelector('#btn-copy');
const btnUndo = document.querySelector('#btn-undo');
const bgColor = document.querySelector('#bg-color');
const bgColorTextbox = document.querySelector('#bg-color-textbox');
const github = document.querySelector('#github');
var previousColor = '';
const getRandomColorInRgb = () => {
const hex = 'BCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += hex[Math.floor(Math.random() * hex.length)];
}
return color;
};
const rgbToHex = (rgbColor) => {
var colorValues = rgbColor
.substring(4)
.substring(0, rgbColor.substring(4).length - 1)
.split(',');
var hexColor =
'#' +
Number(colorValues[0]).toString(16).toUpperCase() +
Number(colorValues[1]).toString(16).toUpperCase() +
Number(colorValues[2]).toString(16).toUpperCase();
return hexColor;
};
const setBackground = () => {
previousColor = body.style.background;
btnUndo.disabled = false;
body.style.background = getRandomColorInRgb();
bgColor.textContent = rgbToHex(body.style.background);
};
const copyBackground = () => {
bgColorTextbox.value = rgbToHex(body.style.background);
bgColorTextbox.select();
document.execCommand('copy');
bgColorTextbox.value = '';
bgColorTextbox.blur();
btnCopy.innerHTML = 'Copied!';
btnCopy.style.background = 'springgreen';
btnCopy.style.color = 'black';
setTimeout(() => {
btnCopy.innerHTML = 'Copy';
btnCopy.style.background = '';
btnCopy.style.color = '';
}, 1000);
};
const browseCode = () => {
window.open('https://github.com/patel-priyank/Pastel-Color-Generator');
};
const setPreviousBackground = () => {
body.style.background = previousColor;
bgColor.textContent = rgbToHex(body.style.background);
previousColor = '';
btnUndo.disabled = true;
};
btnRandom.addEventListener('click', setBackground);
btnCopy.addEventListener('click', copyBackground);
btnUndo.addEventListener('click', setPreviousBackground);
github.addEventListener('click', browseCode);
bgColorTextbox.addEventListener('keydown', (event) => {
event.preventDefault();
});
body.style.background = getRandomColorInRgb();
bgColor.textContent = rgbToHex(body.style.background);