-
Notifications
You must be signed in to change notification settings - Fork 0
/
huedle1.js
355 lines (293 loc) · 10.4 KB
/
huedle1.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
let answerValues = []; //an array containing the r, g, and b values of the answer color
let answerColor = ""; //answer color
let turnCounter = 0; //counts the number of turns for the labels
//raw input variables
//input from each guess
let guessInput = []; //each array holds the r, g, b of the guess
var valid; //boolean checks if the input is valid
//initializes circle sector colors to black
let guessColors = ["black", "black", "black", "black", "black", "black"];
function guess() { // numberentered
++turnCounter; //checks turn number
if (turnCounter == 1) {
guessInput[0] = document.getElementById("r1").value; //assigns raw input to the variables
guessInput[1] = document.getElementById("g1").value;
guessInput[2] = document.getElementById("b1").value;
}
if (turnCounter == 2) {
guessInput[0] = document.getElementById("r2").value;
guessInput[1] = document.getElementById("g2").value;
guessInput[2] = document.getElementById("b2").value;
}
if (turnCounter == 3) {
guessInput[0] = document.getElementById("r3").value;
guessInput[1] = document.getElementById("g3").value;
guessInput[2] = document.getElementById("b3").value;
}
if (turnCounter == 4) {
guessInput[0] = document.getElementById("r4").value;
guessInput[1] = document.getElementById("g4").value;
guessInput[2] = document.getElementById("b4").value;
}
if (turnCounter == 5) {
guessInput[0] = document.getElementById("r5").value;
guessInput[1] = document.getElementById("g5").value;
guessInput[2] = document.getElementById("b5").value;
}
if (turnCounter == 6) {
guessInput[0] = document.getElementById("r6").value;
guessInput[1] = document.getElementById("g6").value;
guessInput[2] = document.getElementById("b6").value;
}
valid = isValid(guessInput[0], guessInput[1], guessInput[2]); //checks if the variables are valid
checkRGB(guessInput[0], guessInput[1], guessInput[2]); //checks how correct the values are
}
function run() {
enableInput();
selectRGB(); //selects answer color
answerColor = "rgb(" + answerValues[0] + ", " + answerValues[1] + ", " + answerValues[2] + ")"; //prints the rgb values
generateCircle(guessColors); //initializes circle values to all black
}
function enableInput() { //allows the user to enter guess values for the next turn in the input boxes
document.getElementById("r" + (turnCounter + 1)).disabled = false;
document.getElementById("g" + (turnCounter + 1)).disabled = false;
document.getElementById("b" + (turnCounter + 1)).disabled = false;
}
function disableInput(num) { //prevents the user from entering the guess values for previous turns or skipping ahead
document.getElementById("r" + num).disabled = true;
document.getElementById("g" + num).disabled = true;
document.getElementById("b" + num).disabled = true;
}
function isValid(r, g, b) {
if (r == Number(r) && r > 0 && r <= 255) {
if (g == Number(g) && g > 0 && g <= 255) {
if (b == Number(b) && b > 0 && b <= 255)
return true;
}
}
return false;
}
function checkRGB(r, g, b) { //checks how accurate rgb is
let errorMessage = "!isvalid error <br>";
if (valid) {
disableInput(turnCounter);
if(turnCounter < 6) //checks if turnCounter is less than six to implement the enableInput
enableInput(); //enableInput doesn't need to be called on the last guess
let thisGuess = [checkR(r), checkG(g), checkB(b)];
let thisColor = "rgb(" + r + "," + g + "," + b + ")";
guessColors[turnCounter - 1] = thisColor;
generateCircle(guessColors);
document.getElementById("r" + turnCounter).style.backgroundColor = thisGuess[0];
document.getElementById("g" + turnCounter).style.backgroundColor = thisGuess[1];
document.getElementById("b" + turnCounter).style.backgroundColor = thisGuess[2];
if (thisGuess[0] == "#00cc66" && thisGuess[1] == "#00cc66" && thisGuess[2] == "#00cc66")
win();
if ((thisGuess[0] != "#00cc66" || thisGuess[1] != "#00cc66" || thisGuess[2] != "#00cc66") && turnCounter == 6)
lose();
}
else {
turnCounter--;
error();
}
}
function disableButton() {
document.getElementById("click-to-check").disabled = true;
}
function error() {
var modal = document.getElementById("errorModal");
document.getElementById("error-message-continued").innerHTML = "check that each value is between 1<br>and 255 and that all values have been entered";
modal.style.display = "block";
window.onclick = function(event) {
if (event.target == modal)
modal.style.display = "none";
}
}
function win() {
var modal = document.getElementById("winModal");
var span = document.getElementsByClassName("close")[0];
document.getElementById("win-message").innerHTML = "<br> the exact answer was " + answerColor;
modal.style.display = "block";
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
disableInput(1);
disableInput(2);
disableInput(3);
disableInput(4);
disableInput(5);
disableInput(6);
disableButton();
}
function lose() {
var modal = document.getElementById("loseModal");
var span = document.getElementsByClassName("close")[0];
document.getElementById("lose-message").innerHTML = "<br> the exact answer was " + answerColor;
modal.style.display = "block";
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
disableInput(1);
disableInput(2);
disableInput(3);
disableInput(4);
disableInput(5);
disableInput(6);
disableButton();
}
function instructions() {
var modal = document.getElementById("instructionModal");
var span = document.getElementsByClassName("close")[0];
modal.style.display = "block";
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
}
function checkR(rVal) { //checks r value
if (Math.abs(rVal - answerValues[0]) <= 5) //if guess is correct --> green
return "#00cc66";
if (Math.abs(rVal - answerValues[0]) <= 15) //if guess is rlly close --> red
return "#ff3300";
if (Math.abs(rVal - answerValues[0]) <= 25) //if guess is pretty close --> organey pink
return "#ff6666";
if (Math.abs(rVal - answerValues[0]) <= 50) //if guess is close --> pink
return "#ff66ff";
if (Math.abs(rVal - answerValues[0]) <= 100) //if guess is far --> purple
return "#9966ff";
if (Math.abs(rVal - answerValues[0]) <= 150) //if guess is pretty far --> blueish purple
return "#6666ff";
if (Math.abs(rVal - answerValues[0]) <= 200) //if guess is really far --> blue
return "#3399ff";
return "#99ccff"; //if guess is way off --> light blue
}
function checkG(gVal) { //checks g value
if (Math.abs(gVal - answerValues[1]) <= 5)
return "#00cc66"; // if guess is correct --> green
if (Math.abs(gVal - answerValues[1]) <= 15)
return "#ff3300"; // if guess is rlly close --> red
if (Math.abs(gVal - answerValues[1]) <= 25)
return "#ff6666"; // if guess is pretty close --> organey pink
if (Math.abs(gVal - answerValues[1]) <= 50)
return "#ff66ff"; // if guess is close --> pink
if (Math.abs(gVal - answerValues[1]) <= 100)
return "#9966ff"; // if guess is far --> purple
if (Math.abs(gVal - answerValues[1]) <= 150)
return "#6666ff"; // if guess is pretty far --> blueish purple
if (Math.abs(gVal - answerValues[1]) <= 200)
return "#3399ff"; // if guess is really far --> blue
return "#99ccff"; // if guess is way off --> light blue
}
function checkB(bVal) { //checks b value
if (Math.abs(bVal - answerValues[2]) <= 5)
return "#00cc66"; // if guess is correct --> green
if (Math.abs(bVal - answerValues[2]) <= 15)
return "#ff3300"; // if guess is rlly close --> red
if (Math.abs(bVal - answerValues[2]) <= 25)
return "#ff6666"; // if guess is pretty close --> organey pink
if (Math.abs(bVal - answerValues[2]) <= 50)
return "#ff66ff"; // if guess is close --> pink
if (Math.abs(bVal - answerValues[2]) <= 100)
return "#9966ff"; // if guess is far --> purple
if (Math.abs(bVal - answerValues[2]) <= 150)
return "#6666ff"; // if guess is pretty far --> blueish purple
if (Math.abs(bVal - answerValues[2]) <= 200)
return "#3399ff"; // if guess is really far --> blue
return "#99ccff"; // if guess is way off --> light blue
}
/* generating circle and color to guess */
function selectRGB() {
answerValues[0] = Math.floor(Math.random() * 240) + 10;
answerValues[1] = Math.floor(Math.random() * 240) + 10;
answerValues[2] = Math.floor(Math.random() * 240) + 10;
}
function generateCircle(guessColors) {
var c = document.getElementById("circleCanvas");
var ctx = c.getContext("2d");
ctx.lineWidth = 10;
ctx.strokeStyle = answerColor;
//outer circle
ctx.beginPath();
ctx.arc(375, 330, 325, 0, 2 * Math.PI, true);
ctx.stroke(); //https://www.w3schools.com/tags/canvas_arc.asp
ctx.moveTo(375, 330);
ctx.closePath();
ctx.fillStyle = 'black';
ctx.fill(); //https://www.html5canvastutorials.com/tutorials/html5-canvas-shape-fill/
//sector 1
ctx.beginPath();
ctx.moveTo(375, 330);
ctx.arc(375, 330, 325, 0, -1/3 * Math.PI, true);
ctx.stroke();
ctx.closePath();
ctx.fillStyle = guessColors[0];
ctx.fill();
//sector 2
ctx.beginPath();
ctx.moveTo(375, 330);
ctx.arc(375, 330, 325, -1/3 * Math.PI, -2/3 * Math.PI, true);
ctx.stroke();
ctx.closePath();
ctx.fillStyle = guessColors[1];
ctx.fill();
//sector 3
ctx.beginPath();
ctx.moveTo(375, 330);
ctx.arc(375, 330, 325, -2/3 * Math.PI, -3/3 * Math.PI, true);
ctx.stroke();
ctx.closePath();
ctx.fillStyle = guessColors[2];
ctx.fill();
//sector 4
ctx.beginPath();
ctx.moveTo(375, 330);
ctx.arc(375, 330, 325, -3/3 * Math.PI, -4/3 * Math.PI, true);
ctx.stroke();
ctx.closePath();
ctx.fillStyle = guessColors[3];
ctx.fill();
//sector 5
ctx.beginPath();
ctx.moveTo(375, 330);
ctx.arc(375, 330, 325, -4/3 * Math.PI, -5/3 * Math.PI, true);
ctx.stroke();
ctx.closePath();
ctx.fillStyle = guessColors[4];
ctx.fill();
//sector 6
ctx.beginPath();
ctx.moveTo(375, 330);
ctx.arc(375, 330, 325, -5/3 * Math.PI, -6/3 * Math.PI, true);
ctx.stroke();
ctx.closePath();
ctx.fillStyle = guessColors[5];
ctx.fill();
//last line
ctx.beginPath();
ctx.lineWidth = 5;
ctx.moveTo(375, 330);
ctx.lineTo(700, 330);
ctx.stroke();
ctx.closePath();
//inner circle
ctx.beginPath();
ctx.moveTo(375, 330);
ctx.arc(375, 330, 150, 0, 2 * Math.PI);
ctx.lineWidth = 10;
ctx.stroke();
ctx.fillStyle = answerColor;
ctx.fill();
}