-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
188 lines (99 loc) · 3.88 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const container = document.getElementById("grid-container");
const mode = document.getElementById("mode");
const colors = document.getElementById("colors");
const colorPicker = document.getElementById("color-picker");
const resetButton = document.querySelector("button");
const sizeInput = document.getElementById("size");
let getColor;
let colorModeColor;
let colorModeChanged;
let rgbValue;
let colorSelected;
//change the size of the pixels/cells
function changeSize() {
container.innerHTML = "";
let size = parseInt(sizeInput.value);
makeGrid(size, size);
}
//reset the grid
resetButton.addEventListener("click", function reset() {
container.innerHTML = "";
let defaultSize = 16;
makeGrid(defaultSize, defaultSize);
});
function makeGrid(rows, columns) {
container.style.setProperty('--grid-rows', rows);
container.style.setProperty('--grid-columns', columns);
//make a grid using user input
for(i = 0; i < (rows * columns); i++) {
const cell = document.createElement("div");
container.appendChild(cell).className = "grid-cell";
cell.addEventListener('mouseover', function color(){
//generate opacity, based on cell's previous opacity
window.opacity = function() {
let opacity = window.getComputedStyle(cell).backgroundColor;
const myArray = opacity.split(", ");
let previousOpacity = myArray.slice(-1)[0];
let previousOpacityInt = parseFloat(previousOpacity);
let newOpacity = previousOpacityInt + modeChange(); //grab opacity from modeChange fxn
return newOpacity;
}
//create color based on color Selected
if(colorSelected === true) {
colorPick();
getColor = rgbValue;
} else if(colorModeChanged === true) {
colorChange();
getColor = colorModeColor;
} else {
getColor = "rgb(0, 0, 0)"
}
//onload color is black
cell.style.backgroundColor = getColor;
});
}
}
makeGrid(16, 16);
//increament the opacity depending on the Mode selected
function modeChange() {
colorSelected = false;
let opChange = 0.00;
if(mode.value == "modern") {
opChange = 0.10;
} else if(mode.value == "classic") {
opChange = 1.00;
} else if(mode.value == "mixed") {
opChange = 0.25;
}
return(opChange);
}
//change the color
function colorChange() {
colorSelected = false;
colorModeChanged = true;
//generate random color
const randomNumber = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
// create color with generated opacity
if(colors.value == "rainbow") {
//range is between all colors
const randomByte = () => randomNumber(0, 255);
colorModeColor = `rgba(${[randomByte(), randomByte(), randomByte(), opacity()].join(',')})`;
} else if(colors.value == "greyscale") {
//range between greys(all rbg values same eg. rbg(16, 16, 16))
const randomByte = () => randomNumber(0, 125);
let colorGenerated = randomByte();
colorModeColor = `rgba(${[colorGenerated, colorGenerated, colorGenerated, opacity()].join(',')})`;
}
return colorModeColor;
}
//convert hex from input element to RGB without opacity
function colorPick() {
let hex = colorPicker.value;
colorSelected = true;
colorModeChanged = false;
let r = parseInt(hex.slice(1, 3), 16);
let g = parseInt(hex.slice(3, 5), 16);
let b = parseInt(hex.slice(5, 7), 16);
rgbValue = "rgb(" + r + "," + g + "," + b + ")";
return rgbValue;
}