Change colour similarity metric
[pixel-calc.git] / public_html / upload.php
1 <html>
2 <head>
3 <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
4
5 <style>
6 .highlighted {
7 background-color: blue;
8 }
9
10 .sprite {
11 float: left;
12 margin-right: 20px;
13 }
14
15 </style>
16
17 </head>
18
19 <body>
20
21 <?php
22
23 // Check if image file is a actual image or fake image
24 if(isset($_POST["submit"])) {
25 $ironlak_colours = array(
26 "532f18" => "Earth",
27 "753e00" => "Swiss",
28 "be854b" => "Mocha",
29 "e5ba83" => "Cuppa",
30 "f9e6c4" => "Irwin",
31 "fcf2e0" => "Fraser",
32
33 "be854b" => "Acacia",
34 "e5b53a" => "Tanami",
35 "f5d29c" => "Oat",
36 "ffdc01" => "Blaze",
37 "ffe04e" => "Pineapple Park",
38 "fffac4" => "Sorbet",
39
40 "de791b" => "Dieci Poes",
41 "f47d31" => "Halloween",
42 "f58427" => "Tues Afterburn",
43 "faae5d" => "Bowen",
44 "fdd29d" => "Smoothie",
45 "fcbc5e" => "Eureka",
46 "fed378" => "Butter",
47 "feefbb" => "Bondi",
48
49 "571b1e" => "Sirum Black-Red",
50 "8b0a02" => "Matador",
51 "d20d44" => "Soviet",
52 "e51a23" => "Autumn",
53 "cd4552" => "Isla Rose",
54 "f2797e" => "Guava",
55
56 "9c0159" => "Moberry",
57 "ea4498" => "Flirt",
58 "f38ab4" => "Potion",
59 "f6aec4" => "Delicious",
60 "f9cbd7" => "Crush",
61 "be76ad" => "Dusk",
62 "dd9ba5" => "Fantasia",
63 "fde6e0" => "Teegs Love",
64
65 "7e3e98" => "Pose Sushi",
66 "592049" => "Venom",
67 "78486a" => "Vino",
68 "c2739d" => "Furious",
69 "d4aed1" => "Gypsy",
70
71 "0a024e" => "Panther",
72 "62609a" => "Eggplant",
73 "7f76b7" => "Phantom",
74 "7d81be" => "Sofles Violence",
75 "8ba4d4" => "Granny",
76
77 "231f20" => "Augor's Blackout",
78 "00447b" => "Midnight",
79 "0068a9" => "Phat1's True Royal",
80 "0082c8" => "Smurf",
81 "00aeef" => "Enue's Bonggg!",
82 "78bde8" => "Torquay",
83 "89d3f4" => "Atmosphere",
84 "b9e1f7" => "Ozone",
85
86 "006b6e" => "Neverland",
87 "007b85" => "Hunter",
88 "00b5cb" => "Reef",
89 "2abdb1" => "Frazetta",
90 "8fd1c5" => "Linz Iceberg",
91 "c0e3da" => "Placid",
92 "70c6a2" => "Jante",
93
94 "006224" => "Huey",
95 "3f9537" => "Field",
96 "6cb43f" => "Cameleon",
97 "b3d78b" => "Reals Sublime",
98 "e6eebc" => "Whizbang",
99
100 "514e25" => "Askew's Olivia",
101 "677717" => "Guacamole",
102 "9ea615" => "Gangrene",
103 "ac9601" => "Banos Asbestos",
104 "d4d110" => "Kryptonite",
105 "f4eb0a" => "Keen",
106 "fcf26f" => "Nitro",
107
108 "434c3d" => "Lazy Grey",
109 "a0a1a5" => "Battleship",
110 "c5c6c8" => "Washington",
111 "f0f1f2" => "Smoulder",
112
113 "a29161" => "Gold",
114 "a8a9ad" => "Bright Chrome",
115
116 "ffffff" => "Whitest Possible",
117 "000000" => "Blackest Possible"
118 );
119
120 function colourDiff($rgb1, $rgb2) {
121 $red1 = hexdec(substr($rgb1,0,2));
122 $green1 = hexdec(substr($rgb1,2,2));
123 $blue1 = hexdec(substr($rgb1,4,2));
124
125 $red2 = hexdec(substr($rgb2,0,2));
126 $green2 = hexdec(substr($rgb2,2,2));
127 $blue2 = hexdec(substr($rgb2,4,2));
128
129 return sqrt(pow($red1 - $red2, 2) + pow($green1 - $green2, 2) + pow($blue1 - $blue2, 2)) ;
130 }
131
132 function closest_colour($rgb, array $colours) {
133 $smallest_diff = 766;
134 $closest = 'none';
135
136 foreach($colours as $hex => $name)
137 {
138 if(colourDiff($rgb, $hex) < $smallest_diff) {
139 $smallest_diff = colourDiff($rgb, $hex);
140 $closest = $hex;
141 }
142 }
143
144 return $closest;
145 }
146
147 $cost_per_length = $_POST['wood_cost'];
148 $wood_length = $_POST['wood_length'];
149 $wood_size = $_POST['wood_size'];
150 $num_cans = 0;
151 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
152 if($check !== false) {
153 $colour_count = array();
154 $icolour_count = array();
155 $image = imagecreatefrompng($_FILES["fileToUpload"]["tmp_name"]);
156 $ironlak_sprite = array();
157
158 echo "<div class='sprite'>Original sprite:<br />";
159
160 echo "<table border='0' cellpadding='0' cellspacing='0'>";
161 for($i=0; $i<$check[1]; $i++) {
162 echo '<tr>';
163 for($j=0; $j<$check[0]; $j++) {
164 $rgb = imagecolorsforindex($image, imagecolorat($image, $j, $i));
165 $r = $rgb['red'];
166 $g = $rgb['green'];
167 $b = $rgb['blue'];
168 $a = $rgb['alpha'];
169 $hex =
170 str_pad(dechex($rgb['red']), 2, '0', STR_PAD_LEFT) .
171 str_pad(dechex($rgb['green']), 2, '0', STR_PAD_LEFT) .
172 str_pad(dechex($rgb['blue']), 2, '0', STR_PAD_LEFT);
173 if($a == 127) $hex = "ffffff";
174
175 if($a == 0) {
176 echo "<td width='4' height='4' class='$hex' bgcolor='#$hex' r='$r' g='$g' b='$b' a='$a'></td>";
177 $colour_count[$hex] = isset($colour_count[$hex]) ? $colour_count[$hex]+1 : 1;
178 } else {
179 echo "<td width='4' height='4' bgcolor='#$hex' r='$r' g='$g' b='$b' a='$a'></td>";
180 }
181
182 $ironlak_sprite[$i][$j] = closest_colour($hex, $ironlak_colours);
183 }
184 echo '</tr>';
185 }
186 echo '</table></div>';
187
188 echo "<div class='sprite'>Ironlak sprite:<br />";
189
190 echo "<table border='0' cellpadding='0' cellspacing='0'>";
191 for($i=0; $i<$check[1]; $i++) {
192 echo '<tr>';
193 for($j=0; $j<$check[0]; $j++) {
194 $rgb = imagecolorsforindex($image, imagecolorat($image, $j, $i));
195 $r = $rgb['red'];
196 $g = $rgb['green'];
197 $b = $rgb['blue'];
198 $a = $rgb['alpha'];
199 $ihex = $ironlak_sprite[$i][$j];
200 if($a == 127) $ihex = "ffffff";
201
202 if($a == 0) {
203 echo "<td width='4' height='4' class='$ihex' bgcolor='#$ihex' r='$r' g='$g' b='$b' a='$a'></td>";
204 $icolour_count[$ihex] = isset($icolour_count[$ihex]) ? $icolour_count[$ihex]+1 : 1;
205 } else {
206 echo "<td width='4' height='4' bgcolor='#$ihex' r='$r' g='$g' b='$b' a='$a'></td>";
207 }
208 }
209 echo '</tr>';
210 }
211 echo '</table></div>';
212 echo '<div style="clear: both;"></div>';
213 $total_blocks = array_sum($colour_count);
214 $lengths_required = ceil($total_blocks*$wood_size/$wood_length);
215 echo '<br />';
216 echo '<br />';
217
218 echo 'Physical dimensions (metres)<br />';
219 echo 'Width: ' . $check[0] * $wood_size . '<br />';;
220 echo 'Height: ' . $check[1] * $wood_size;
221
222 echo '<br /><br />';
223
224 echo 'Wood cost breakdown<br />';
225 echo '<table border=1>
226 <tr>
227 <td>Total blocks</td>
228 <td>No of ' . $wood_length . 'm lengths required</td>
229 <td>Cost @ ' . $cost_per_length . ' per length</td>
230 </tr>
231 <tr>
232 <td>' . $total_blocks . '</td>
233 <td>' . $lengths_required . '</td>
234 <td>' . $lengths_required * $cost_per_length . '</td>
235 </td>
236 </table>';
237
238 $wood_cost = $lengths_required * $cost_per_length;
239 $paint_cost = 0;
240
241 echo '<br /><br />';
242 echo 'Colour cost breakdown<br /><br >';
243 echo '<div class="sprite">Original colour table:<br />';
244
245 echo '<table border=1>
246 <tr>
247 <td>Colour</td>
248 <td>RGB</td>
249 <td>Num blucks</td>
250 </tr>';
251 foreach($colour_count as $colour => $qty) {
252 echo "<tr class='colour-row' id='$colour'><td bgcolor='$colour'></td><td>$colour</td><td>$qty</td></tr>";
253 }
254
255 echo '</table></div>';
256
257 echo '<div class="sprite">Ironlak colour table:<br />';
258
259 echo '<table border=1>
260 <tr>
261 <td>Colour</td>
262 <td>Name</td>
263 <td>Coverage (m^2)</td>
264 <td>Cans needed</td>
265 </tr>';
266 foreach($icolour_count as $colour => $qty) {
267 echo "<tr class='colour-row' id='$colour'><td bgcolor='$colour'></td><td>" . $ironlak_colours[$colour] . "</td><td>" . $qty * ($wood_size * $wood_size) . "</td><td>" . ceil(($qty * ($wood_size * $wood_size))/2.2) . "</td></tr>";
268 $num_cans+=+ceil(($qty * ($wood_size * $wood_size))/2.2);
269 }
270
271 echo '</table></div>';
272
273 echo '<div style="clear: both;"></div>';
274 echo '<br />';
275
276 echo 'Paint cost:<br />';
277
278 $cost_per_can = 6.36;
279 if($num_cans <= 199 ) {
280 $cost_per_can = 6.60;
281 }
282
283 if($num_cans <= 59 ) {
284 $cost_per_can = 6.76;
285 }
286
287 if($num_cans <= 35 ) {
288 $cost_per_can = 7;
289 }
290
291 if($num_cans <= 11 ) {
292 $cost_per_can = 7.16;
293 }
294
295 if($num_cans <= 5 ) {
296 $cost_per_can = 7.65;
297 }
298 $paint_cost = $num_cans * $cost_per_can;
299 echo "$num_cans cans @ $cost_per_can each: $paint_cost";
300 echo '<br /><br />';
301
302 $total_cost = $wood_cost + $paint_cost;
303 echo '<div style="font-size: 40px">Total cost (wood+paint): ' . $total_cost . '</div>';
304
305 }
306 }
307 ?>
308
309 <script>
310 $( ".colour-row" ).hover(function() {
311 var colour = $(this).attr('id');
312 console.log(colour);
313 $("." + colour).toggleClass("highlighted");
314 });
315 </script>
316
317 </body>
318 </html>