Calculate paint cost
authorCameron Ball <cameron@getapproved.com.au>
Thu, 16 Jul 2015 08:45:10 +0000 (16:45 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Thu, 16 Jul 2015 08:45:10 +0000 (16:45 +0800)
public_html/upload.php

index 22321d6..cf1e765 100644 (file)
@@ -6,6 +6,12 @@
             .highlighted {
                 background-color: blue;
             }
+            
+            .sprite {
+                float: left;
+                margin-right: 20px;
+            }
+
         </style>
             
     </head>
 
 // Check if image file is a actual image or fake image
 if(isset($_POST["submit"])) {
+    $ironlak_colours = array(
+        "532f18" => "Earth",
+        "753e00" => "Swiss",
+        "be854b" => "Mocha",
+        "e5ba83" => "Cuppa",
+        "f9e6c4" => "Irwin",
+        "fcf2e0" => "Fraser",
+
+        "be854b" => "Acacia",
+        "e5b53a" => "Tanami",
+        "f5d29c" => "Oat",
+        "ffdc01" => "Blaze",
+        "ffe04e" => "Pineapple Park",
+        "fffac4" => "Sorbet",
+
+        "de791b" => "Dieci Poes",
+        "f47d31" => "Halloween",
+        "f58427" => "Tues Afterburn",
+        "faae5d" => "Bowen",
+        "fdd29d" => "Smoothie",
+        "fcbc5e" => "Eureka",
+        "fed378" => "Butter",
+        "feefbb" => "Bondi",
+
+        "571b1e" => "Sirum Black-Red",
+        "8b0a02" => "Matador",
+        "d20d44" => "Soviet",
+        "e51a23" => "Autumn",
+        "cd4552" => "Isla Rose",
+        "f2797e" => "Guava",
+
+        "9c0159" => "Moberry",
+        "ea4498" => "Flirt",
+        "f38ab4" => "Potion",
+        "f6aec4" => "Delicious",
+        "f9cbd7" => "Crush",
+        "be76ad" => "Dusk",
+        "dd9ba5" => "Fantasia",
+        "fde6e0" => "Teegs Love",
+
+        "7e3e98" => "Pose Sushi",
+        "592049" => "Venom",
+        "78486a" => "Vino",
+        "c2739d" => "Furious",
+        "d4aed1" => "Gypsy",
+
+        "0a024e" => "Panther",
+        "62609a" => "Eggplant",
+        "7f76b7" => "Phantom",
+        "7d81be" => "Sofles Violence",
+        "8ba4d4" => "Granny",
+
+        "231f20" => "Augor's Blackout",
+        "00447b" => "Midnight",
+        "0068a9" => "Phat1's True Royal",
+        "0082c8" => "Smurf",
+        "00aeef" => "Enue's Bonggg!",
+        "78bde8" => "Torquay",
+        "89d3f4" => "Atmosphere",
+        "b9e1f7" => "Ozone",
+
+        "006b6e" => "Neverland",
+        "007b85" => "Hunter",
+        "00b5cb" => "Reef",
+        "2abdb1" => "Frazetta",
+        "8fd1c5" => "Linz Iceberg",
+        "c0e3da" => "Placid",
+        "70c6a2" => "Jante",
+
+        "006224" => "Huey",
+        "3f9537" => "Field",
+        "6cb43f" => "Cameleon",
+        "b3d78b" => "Reals Sublime",
+        "e6eebc" => "Whizbang",
+
+        "514e25" => "Askew's Olivia",
+        "677717" => "Guacamole",
+        "9ea615" => "Gangrene",
+        "ac9601" => "Banos Asbestos",
+        "d4d110" => "Kryptonite",
+        "f4eb0a" => "Keen",
+        "fcf26f" => "Nitro",
+
+        "434c3d" => "Lazy Grey",
+        "a0a1a5" => "Battleship",
+        "c5c6c8" => "Washington",
+        "f0f1f2" => "Smoulder",
+
+        "a29161" => "Gold",
+        "a8a9ad" => "Bright Chrome",
+
+        "ffffff" => "Whitest Possible",
+        "000000" => "Blackest Possible"  
+    );
+        
+    function colourDiff($rgb1, $rgb2) {
+        $red1   = hexdec(substr($rgb1,0,2));
+        $green1 = hexdec(substr($rgb1,2,2));
+        $blue1  = hexdec(substr($rgb1,4,2));
+
+        $red2   = hexdec(substr($rgb2,0,2));
+        $green2 = hexdec(substr($rgb2,2,2));
+        $blue2  = hexdec(substr($rgb2,4,2));
+
+        return abs($red1 - $red2) + abs($green1 - $green2) + abs($blue1 - $blue2) ;
+    }
+    
+    function closest_colour($rgb, array $colours) {
+        $smallest_diff = 766;
+        $closest = 'none';
+        
+        foreach($colours as $hex => $name)
+        {
+            if(colourDiff($rgb, $hex) < $smallest_diff) {
+                $smallest_diff = colourDiff($rgb, $hex);
+                $closest = $hex;
+            }
+        }
+        
+        return $closest;
+    }
+        
     $cost_per_length = $_POST['wood_cost'];
     $wood_length = $_POST['wood_length'];
     $wood_size = $_POST['wood_size'];
+    $num_cans = 0;
     $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
     if($check !== false) {
         $colour_count = array();
+        $icolour_count = array();
         $image = imagecreatefrompng($_FILES["fileToUpload"]["tmp_name"]);
+        $ironlak_sprite = array();
+        
+        echo "<div class='sprite'>Original sprite:<br />";
+        
         echo "<table border='0' cellpadding='0' cellspacing='0'>";
         for($i=0; $i<$check[1]; $i++) {
             echo '<tr>';
@@ -44,11 +178,38 @@ if(isset($_POST["submit"])) {
                 } else {
                     echo "<td width='4' height='4' bgcolor='#$hex' r='$r' g='$g' b='$b' a='$a'></td>";
                 }
+                
+                $ironlak_sprite[$i][$j] = closest_colour($hex, $ironlak_colours);
             }
             echo '</tr>';
         }
-        echo '</table>';
+        echo '</table></div>';
+
+        echo "<div class='sprite'>Ironlak sprite:<br />";
         
+        echo "<table border='0' cellpadding='0' cellspacing='0'>";
+        for($i=0; $i<$check[1]; $i++) {
+            echo '<tr>';
+            for($j=0; $j<$check[0]; $j++) {
+                $rgb = imagecolorsforindex($image, imagecolorat($image, $j, $i));
+                $r = $rgb['red'];
+                $g = $rgb['green'];
+                $b = $rgb['blue'];
+                $a = $rgb['alpha'];
+                $ihex = $ironlak_sprite[$i][$j];
+                if($a == 127) $ihex = "ffffff";
+                
+                if($a == 0) {
+                    echo "<td width='4' height='4' class='$ihex' bgcolor='#$ihex' r='$r' g='$g' b='$b' a='$a'></td>";
+                    $icolour_count[$ihex] = isset($icolour_count[$ihex]) ? $icolour_count[$ihex]+1 : 1;
+                } else {
+                    echo "<td width='4' height='4' bgcolor='#$ihex' r='$r' g='$g' b='$b' a='$a'></td>";
+                }
+            }
+            echo '</tr>';
+        }
+        echo '</table></div>';
+        echo '<div style="clear: both;"></div>';
         $total_blocks = array_sum($colour_count);
         $lengths_required = ceil($total_blocks*$wood_size/$wood_length);
         echo '<br />';
@@ -60,7 +221,7 @@ if(isset($_POST["submit"])) {
         
         echo '<br /><br />';
         
-        echo 'Cost breakdown<br />';
+        echo 'Wood cost breakdown<br />';
         echo '<table border=1>
                 <tr>
                     <td>Total blocks</td>
@@ -74,20 +235,73 @@ if(isset($_POST["submit"])) {
                 </td>
               </table>';
         
+        $wood_cost = $lengths_required * $cost_per_length;
+        $paint_cost = 0;
+        
         echo '<br /><br />';
-        echo 'Colour breakdown<br />';
+        echo 'Colour cost breakdown<br /><br >';
+        echo '<div class="sprite">Original colour table:<br />';
         
         echo '<table border=1>
                 <tr>
                     <td>Colour</td>
                     <td>RGB</td>
-                    <td>Quantity</td>
+                    <td>Num blucks</td>
                     </tr>';
         foreach($colour_count as $colour => $qty) {
             echo "<tr class='colour-row' id='$colour'><td bgcolor='$colour'></td><td>$colour</td><td>$qty</td></tr>";
         }
         
-        echo '</table>';
+        echo '</table></div>';
+        
+        echo '<div class="sprite">Ironlak colour table:<br />';
+        
+        echo '<table border=1>
+                <tr>
+                    <td>Colour</td>
+                    <td>Name</td>
+                    <td>Coverage (m^2)</td>
+                    <td>Cans needed</td>
+                    </tr>';
+        foreach($icolour_count as $colour => $qty) {
+            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>";
+            $num_cans+=+ceil(($qty * ($wood_size * $wood_size))/2.2);
+        }
+        
+        echo '</table></div>';
+        
+        echo '<div style="clear: both;"></div>';
+        echo '<br />';
+        
+        echo 'Paint cost:<br />';
+        
+        $cost_per_can = 6.36;
+        if($num_cans <= 199 ) {
+            $cost_per_can = 6.60;
+        }
+        
+        if($num_cans <= 59 ) {
+            $cost_per_can = 6.76;
+        }
+        
+        if($num_cans <= 35 ) {
+            $cost_per_can = 7;
+        }
+        
+        if($num_cans <= 11 ) {
+            $cost_per_can = 7.16;
+        }
+        
+        if($num_cans <= 5 ) {
+            $cost_per_can = 7.65;
+        }
+        $paint_cost = $num_cans * $cost_per_can;
+        echo "$num_cans cans @ $cost_per_can each: $paint_cost";
+        echo '<br /><br />';
+        
+        $total_cost = $wood_cost + $paint_cost;
+        echo '<div style="font-size: 40px">Total cost (wood+paint): ' . $total_cost . '</div>';
+        
     }
 }
 ?>