-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorgen.php
executable file
·343 lines (290 loc) · 10.9 KB
/
colorgen.php
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
<!DOCTYPE html>
<!--
Classes:
- Title="PageTitle"
- Bottom Table = "BottomTable"
- printInfo = 'printPage'
-->
<link href="./style.css" rel="stylesheet">
<?php
require("./navbar/navbar.php");
?>
<br>
<!--- FORM --->
<div id = "Container">
<h1 class="PageTitle">Color Generator</h1>
<form method="get">
<div class="dimension Form">
<label for="dimension_num">Dimension = </label>
<?php
$dimension_num = 1;
if(isset($_GET["dimension_num"]))
$dimension_num = $_GET["dimension_num"];
echo "<output name=\"out_d\" for=\"color_num\">$dimension_num</output>";
echo "<br><input type=\"range\" id=\"dimension_num\" name=\"dimension_num\" min=\"1\" max=\"26\" value=\"$dimension_num\" oninput=\"out_d.value=this.value\">";
?>
<div class="color Form">
<label for="color_num">Number of Colors = </label>
<?php
$color_num = 1;
if(isset($_GET["color_num"]))
$color_num = $_GET["color_num"];
echo "<output name=\"out_c\" for=\"color_num\">$color_num</output>";
echo "<br><input type=\"range\" id=\"color_num\" name=\"color_num\" min=\"1\" max=\"10\" value=\"$color_num\" oninput=\"out_c.value=this.value\">";
?>
<br>
<br>
<button type="submit">Update</button>
</form>
<form id= "colorSelectForm" method="post" action="colorgen.php?dimension_num=<?php echo($dimension_num);?>&color_num=<?php echo($color_num);?>">
<table style="border: 3px solid black; border-collapse: collapse;">
<?php
$colorsPossible = ["red", "orange", "yellow", "green", "blue", "purple", "grey", "brown", "black", "teal"];
$colorsSelected = isset($_POST["colors"])?$_POST["colors"]:$colorsPossible;
//echo("[".implode(",",$colorsSelected)."]"."<br>");
for ($i = 0; $i < 10; $i++) {
echo("<tr>");
$hidden = $i>$color_num-1;
if ($hidden)
{
echo " <select style='display:none' name='colors[]' onchange='setColor($i,this.value)' id='color$i'> ";
}
else
{
//generate the radio forms beside the colors
echo("<td>
<input type=\"radio\" id=\"selected_color\" name=\"selected_color\" onchange='displaySelectedColor()' value=\"$i\">
<label for=\"option1\"></label>
</td>" );
//generate the color selector
echo("<td style=\"border: 3px solid black; border-collapse: collapse;\">");
echo "<select name='colors[]' onchange='setColor($i,this.value)' id='color$i'>";
foreach ($colorsPossible as $color)
{
$selected = $colorsSelected[$i]==$color?"selected":"";
echo "<option id='color{$i}{$color}' value='$color'$selected>". strtoupper($color) . "</option>";
}
echo "</select>";
echo("</td>");
echo("<td style=\"border: 3px solid black; border-collapse: collapse;\"id='cellcontainer$i'>");
}
echo("</tr>");
}
?>
</table>
<br>
</form>
<p id="selectedcolorvalue">none</p>
<p id="selectedcolornumber">none</p>
<script>
let color_num = <?php echo(isset($_GET["color_num"])?$_GET["color_num"]:1);?>;
let usedColors = getUsedColors();
function setColor(index,color)
{
currentColor = usedColors[index];
updatedColor = color;
if (colorAlreadyUsed(color))
{
usedColors[getAlreadyUsedIndex(color)]=currentColor;
}
usedColors[index]=color;
updateValues();
displaySelectedColor();
updateAllCellContainer();
}
function updateValues()
{
for (index = 0; index<10; index++)
{
document.getElementById("color"+index).value=usedColors[index];
//ddocument.getElementById("colorprint"+index).innerHTML=usedColors[index];
}
}
function colorAlreadyUsed(color)
{
return usedColors.indexOf(color)!=-1;
}
function getAlreadyUsedIndex(color)
{
return usedColors.indexOf(color);
}
function getUsedColors()
{
return <?php
$colorsPossible = ["red", "orange", "yellow", "green", "blue", "purple", "grey", "brown", "black", "teal"];
$colorsSelected = isset($_POST["colors"])?$_POST["colors"]:$colorsPossible;
echo("[\"".implode("\",\"",$colorsSelected)."\"]");
?> ;
}
function displaySelectedColor(colorNumber)
{
colorNumber = document.querySelector('input[name="selected_color"]:checked').value
document.getElementById("selectedcolornumber").innerHTML=(colorNumber);
document.getElementById("selectedcolorvalue").innerHTML=(usedColors[colorNumber]);
}
</script>
<!-- BOTTOM TABLE -->
<?php
//Creating a string that will be used to hold a new class for easy formatting
//This formatted_print variable will be what our print menu will be based on
$formatted_print = '<h1 id = "PrintMenu">Print Menu</h1>';
//This creates a Exit Button from the print Menu
$formatted_print.='<button onclick="printReturn()" id="Exit">Return to Color Generator</button>';
$formatted_print.='<br><button onclick="printInner()">Print</button>';
//Throwing the table info into a div for easy printing
$formatted_print .= '<div id = "printPage">';
$formatted_print .= '<h2>Selected Colors:</h2>';
//Creating a string that is going to hold the colors picked for printing
$colorInfo = '<ul>';
if(isset($_GET["color_num"])){
$color_num = $_GET["color_num"];
$colors = isset($_POST["colors"])?$_POST["colors"]:["red", "orange", "yellow", "green", "blue", "purple", "grey", "brown", "black", "teal"];
for ($i = 0; $i<$color_num; $i++)
{
$colorInfo.='<li id="colorprint'.$i.'"></li>';
}
}
$colorInfo .= '</ul>';
//echo($colorInfo);
//adding color info to the print information
$formatted_print.=$colorInfo;
//Creating the n+1 x n+1 table
$table = '<table class = "BottomTable" id="bottomTable">';
$dim = isset($_GET["dimension_num"])?$_GET["dimension_num"]:1;
$letterNum = ord('A');
for($i = 0; $i < $dim+1; $i++){
$table.="<tr>";
//echo("<tr>");
for($j = 0; $j < $dim+1;$j++){
$cellLetter = chr($j+65-1);
$cellNumber = $i;
$cellId = $cellLetter.$cellNumber;
//Left uppermost empty
if($i == 0 && $j == 0){
$table.='<td class = "Cell"></td>';
continue;
}
//Echos out letter for top rows
if($i == 0){
$letter = chr($letterNum);
$table.='<td class = "Cell">'.$letter.'</td>';
$letterNum++;
continue;
}
//Echos out numbers for leftmost columns
if($j == 0){
$table.='<td class = "Cell">'.$i.'</td>';
continue;
}
$table.='<td onclick="addCell(this.id)", class = "Cell" id="'.$cellId.'" color="">';
//content here
$table.='</td>';
}
$table.='</tr>';
}
$table.='</table>';
echo($table);
//Adding escape characters to the table so that way it can be treated and passed as a string to the printScreen function
$formatted_print.=$table;
$formatted_print.='</div>';
$formatted_print = addslashes($formatted_print);
echo("<br><button id = 'print' onclick = 'printScreen()'>Print Preview</button>");
?>
<script>
let oldContainer = null;
function addCell(cellid)
{
colorNumber = document.getElementById("selectedcolornumber").innerHTML;
colorValue = document.getElementById("selectedcolorvalue").innerHTML;
cellElement = document.getElementById(cellid);
previousColor = cellElement.getAttribute("color");
if (colorNumber!=previousColor)
{
cellElement.setAttribute("color",colorNumber);
cellElement.style.backgroundColor=colorValue;
}
else
{
cellElement.setAttribute("color","");
cellElement.style.backgroundColor="white";
}
updateCellContainer(colorNumber);
updateCellContainer(previousColor);
}
function updateCellContainer(colorNumber)
{
if (colorNumber=="")
return;
var cellElements = document.querySelectorAll(`[color="${colorNumber}"]`);
var container = document.getElementById("cellcontainer"+colorNumber);
var ids ="";
// Correct loop using for...of for iterating over NodeList
for (let element of cellElements) {
element.style.backgroundColor = usedColors[colorNumber];
ids += element.id + " "; // Collect all IDs
}
container.innerHTML = ids;
}
function updateAllCellContainer()
{
for (i = 0; i< color_num; i++)
{
updateCellContainer(i);
}
}
function printScreen(){
contents ='<button onclick="printReturn()" id="Exit">Return to Color Generator</button><br><br><br><button onclick="printInner()" id="Print">Print</button><br><br>' + document.getElementById('bottomTable').outerHTML + '<br><p>Color Key on Next Page</p><br>';
for (i = 0; i<color_num; i++)
{
if (document.getElementById("cellcontainer"+i).innerHTML) {
contents+='<h2>'+usedColors[i]+' {'+getHexValue(usedColors[i])+'}: '+document.getElementById("cellcontainer"+i).innerHTML+'</h2>';
}
}
oldContainer = document.getElementById('Container').innerHTML;
document.getElementById('Container').innerHTML = contents;
for (let cell of document.getElementsByClassName("Cell"))
{
if (!cell.innerHTML) {
cell.innerHTML=cell.getAttribute("color");
}
}
}
function getHexValue(colorName) {
//create a canvas
const canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
//fill in canvas with color
const ctx = canvas.getContext('2d');
ctx.fillStyle = colorName;
ctx.fillRect(0, 0, 1, 1);
//get RGB color values
[r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data;
//Convert RGB color Values to HEX
return ` #${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
}
function printReturn() {
document.getElementById('Container').innerHTML = oldContainer;
oldContainer = null;
}
function printInner() {
var content = '<img src="./Images/logo_transparent.png" class="TeamLogo">'+document.getElementById("Container").innerHTML;
var newWin = window.open("");
newWin.document.write("<link href=\"./print_style.css\" rel=\"stylesheet\">");
newWin.document.write(content);
newWin.document.getElementById("Exit").remove();
newWin.document.getElementById("Print").remove();
for (const br of newWin.document.getElementsByTagName("br")) {
br.remove();
}
setTimeout(() => {
newWin.print();
newWin.close();
}, 100);
}
//quick methods
{
}
</script>
</div>
</html>