-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (53 loc) · 1.77 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
window.onload = pgInit;
rgb = [127,127,127];
rgbQuest = [0,0,0];
onQuest = 0;
errorTotal = 100
function updateBackground (rgb) {
document.body.style.backgroundColor = `rgb(${rgb[0]},${rgb[1]},${rgb[2]})`;
}
function launchQuest () {
onQuest = 1;
newR = Math.floor( Math.random() * 256 );
newG = Math.floor( Math.random() * 256 );
newB = Math.floor( Math.random() * 256 );
rgbQuest = [newR, newG, newB];
updateBackground(rgbQuest);
document.getElementById('questionButton').classList.add('iconReversed');
}
function checkQuest () {
onQuest = 0;
updateBackground(rgb);
differenceR = Math.abs( rgb[0] - rgbQuest[0] ) / 255;
differenceG = Math.abs( rgb[1] - rgbQuest[1] ) / 255;
differenceB = Math.abs( rgb[2] - rgbQuest[2] ) / 255;
errorTotal = ((differenceR + differenceG + differenceB) / 3 * 100).toFixed(2);
console.log(errorTotal);
document.getElementById('errorData').innerHTML = errorTotal + ' %';
document.getElementById('errorData').style.color = `rgb(${rgbQuest[0]},${rgbQuest[1]},${rgbQuest[2]})`;
document.getElementById('questionButton').classList.remove('iconReversed');
}
function pgInit () {
sRed = document.getElementById('s-red');
sGreen = document.getElementById('s-green');
sBlue = document.getElementById('s-blue');
[sRed, sGreen, sBlue].forEach((iterator, index)=>{
iterator.oninput = element =>{
rgb[index] = element.srcElement.value;
if( onQuest == 0 ) {
updateBackground(rgb);
}
}
});
updateBackground(rgb);
setClicks();
}
function setClicks () {
document.getElementById('questionButton').onclick = function () {
if( onQuest == 0 ){
launchQuest();
}else if ( onQuest == 1 ){
checkQuest();
}
}
}