<?php
// david's magic colour matcherer

function colormatch($color$table='randall.txt') {
  
$color trim($color);
  
$rgbtxt explode("\n"file_get_contents($table));
  
$colornames = array();
  foreach (
$rgbtxt as $pair) {
    
$pair explode("\t"$pair);
    
$colornames[$pair[0]] = $pair[1];
  }
  
  if (isset(
$colornames[strtolower($color)])) {
    
$color $colornames[strtolower($color)]; // the hex triplet will be converted to RGB dec values later.
  
}
  
  if (
preg_match("/^(rgb(a)?)?\(?([0-9]{1,3}),? *?([0-9]{1,3}),? *?([0-9]{1,3})(,? *?[0-9]{1,3})?\)?$/i"$color$matches)) {
    
$red = ($matches[3] > 255 255 $matches[3]);
    
$green = ($matches[4] > 255 255 $matches[4]);
    
$blue = ($matches[5] > 255 255 $matches[5]);
    
$alpha = ($matches[7] ? ($matches[7] > 255 255 $matches[7]) : ); // alpha is optional
    
    
return array($red$green$blue$alpha);
    
  } elseif (
preg_match("/^#?(([0-9a-f])([0-9a-f])([0-9a-f]))(([0-9a-f])([0-9a-f])([0-9a-f]))?$/i"$color$matches)) { // colors from rgb.txt are converted to (r, g, b) here.
    
if ($matches[5]) { // not the short form
      
$red $matches[2].$matches[3];
      
$green $matches[4].$matches[6];
      
$blue =  $matches[7].$matches[8];
    } else { 
// short form triplet
      
$red $matches[2].$matches[2];
      
$green $matches[3].$matches[3];
      
$blue =  $matches[4].$matches[4];
    }
    
    return array(
hexdec($red), hexdec($green), hexdec($blue), 0); // 0 for alpha--not set be hex triplet
  
} else {
    return 
0// 0 == error, unmatchable. try and avoid this.
  
}
}
?>