Scripts

Color Tool

Dit script kan een hexcolor omrekenen in rgb en andersom, een websafecolor bij de opgegeven kleur zoeken of dat allemaal met een random color.

color-tool
[code]<?php

function safeColor($hexcolor) 
{ 
   if(!eregi("^#", $hexcolor)) {
      $hexcolor = "#".$hexcolor; 
   }
   if(eregi("^#[a-f0-9]{6}$", $hexcolor)){ 
      $color[0] = hexdec(substr($hexcolor,1,2));
      $color[1] = hexdec(substr($hexcolor,3,2));
      $color[2] = hexdec(substr($hexcolor,5,2));

      for($i=0; $i<count($color); $i++) { 
         $mod = $color[$i]%51; 
         if($mod>26) {
            $hex[$i] = str_pad(dechex($color[$i] + (51-$mod)),2,0,STR_PAD_LEFT); 
         }
         else {
            $hex[$i] = str_pad(dechex($color[$i] - $mod),2,0,STR_PAD_LEFT); 
         }
      } 
      $safecolor =  strtoupper("#".$hex[0].$hex[1].$hex[2]); 
      return $safecolor; 
   } 
   else { 
      return "#000000";
   } 
} 

function randomColor() {
   $hexarray = array('0','1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F');
   $tmpa = rand(0, 15);
   $tmpb = rand(0, 15);
   $tmpc = rand(0, 15);
   $tmpd = rand(0, 15);
   $tmpe = rand(0, 15);
   $tmpf = rand(0, 15);
   return "#" . $hexarray[$tmpa] . $hexarray[$tmpb] . $hexarray[$tmpc] . $hexarray[$tmpd] . $hexarray[$tmpe] . $hexarray[$tmpf];
}

function hex2RGB($hexcolor) {
   if(!eregi("^#", $hexcolor)) {
      $hexcolor = "#".$hexcolor; 
   }
   if(eregi("^#[a-f0-9]{6}$", $hexcolor)){ 
      $color[0] = hexdec(substr($hexcolor,1,2));
      $color[1] = hexdec(substr($hexcolor,3,2));
      $color[2] = hexdec(substr($hexcolor,5,2));
      return $color; 
   } 
   else { 
      return array(0, 0, 0);
   } 
}

function RGB2Hex($r, $g, $b) {
   if($r < 0 || $r > 255 || !is_numeric($r)) { $r = 0; }
   if($g < 0 || $g > 255 || !is_numeric($g)) { $g = 0; }
   if($b < 0 || $b > 255 || !is_numeric($b)) { $b = 0; }
   $color = array($r, $g, $b);
   $hexcolor = "#" . sprintf("%02X", $r) . sprintf("%02X", $g) . sprintf("%02X", $b);
   return $hexcolor; 
}

echo("<table>\n");
echo("   <tr>\n");
echo("      <td colspan=\"6\"><b>Color Tool</b></td>\n");
echo("   </tr>\n");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if ($_POST["type"] == "hex") {
      $hexcolor = $_POST["hexcolor"];
      $sel["hex"] = " checked";
      $safecolor = safeColor($hexcolor);
      $color = hex2RGB($hexcolor);
      $hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
      $type = "hex";
   }
   elseif ($_POST["type"] == "rgb") {
      $color = $_POST["color"];
      $sel["rgb"] = " checked";
      $hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
      $color = hex2RGB($hexcolor);
      $safecolor = safeColor($hexcolor);
      $type = "rgb";
   }
   else {
      $hexcolor = randomColor();
      $sel["random"] = " checked";
      $safecolor = safeColor($hexcolor);
      $color = hex2RGB($hexcolor);
      $hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
      $type = "random";
   }
      echo("   <tr>\n");
      echo("      <td></td>\n");
      echo("      <td>Hex: </td>\n");
      echo("      <td colspan=\"3\">" . $hexcolor . "</td>\n");
      echo("      <td width=\"50\" style=\"background-color: " . $hexcolor . ";\">&#32;</td>\n");
      echo("   </tr>\n");
      echo("   <tr>\n");
      echo("      <td></td>\n");
      echo("      <td>RGB: </td>\n");
      echo("      <td colspan=\"3\">R: " . $color[0] . ", G: " . $color[1] . ", B:  " . $color[2] . "</td>\n");
      echo("      <td width=\"50\" style=\"background-color: " . $hexcolor . ";\">&#32;</td>\n");
      echo("   </tr>\n");
      echo("   <tr>\n");
      echo("      <td></td>\n");
      echo("      <td>Websafecolor: </td>\n");
      echo("      <td colspan=\"3\">" . $safecolor . "</td>\n");
      echo("      <td width=\"50\" style=\"background-color: " . $safecolor . ";\">&#32;</td>\n");
      echo("   </tr>\n");
}
else {
   $hexcolor = "#000000";
   $sel["hex"] = " checked";
}

?>
   <form method="post" action="<?php echo($_SERVER["PHP_SELF"]); ?>">
   <tr>
      <td><input type="radio" name="type" value="hex"<?php echo($sel["hex"]); ?>></td>
      <td>Hexcolor: </td>
      <td colspan="3"><input type="text" name="hexcolor" size="7" value="<?php echo($hexcolor); ?>"></td>
      <td></td>
   </tr>
   <tr>
      <td><input type="radio" name="type" value="rgb"<?php echo($sel["rgb"]); ?>></td>
      <td>RGB: </td>
      <td><input type="text" name="color[]" size="3" value="<?php echo($color[0]); ?>"></td>
      <td><input type="text" name="color[]" size="3" value="<?php echo($color[1]); ?>"></td>
      <td><input type="text" name="color[]" size="3" value="<?php echo($color[2]); ?>"></td>
      <td></td>
   </tr>
   <tr>
      <td><input type="radio" name="type" value="random"<?php echo($sel["random"]); ?>></td>
      <td colspan=\"4\">Random color </td>
      <td></td>
   </tr>
   <tr>
      <td colspan="6" align="center"><input type="submit" value="Go"></td>
   </tr>
   </form>
</table>
[/code]

Reacties

0
Nog geen reacties.