Exemplo n.º 1
0
 function toRGB($color)
 {
     $named = ColorMap::wordToHex($color);
     if ($named) {
         $color = $named;
     }
     $color = str_replace('#', '', $color);
     $rgb = array();
     foreach (range(0, 2) as $i) {
         array_push($rgb, hexdec(substr($color, $i * 2, 2)));
     }
     return $rgb;
 }
Exemplo n.º 2
0
 function convert($color, $to = false)
 {
     if (!$to) {
         $to = $this->defaultReturnType;
     }
     if (is_array($color) && count($color) == 3) {
         //could be rgb or hsl, make a judgement call
         if ($color['r'] || $color['R']) {
             $color = new Color_RGB($color);
         } elseif ($color['h'] || $color['H']) {
             $color = new Color_HSL($color);
         } else {
             return false;
         }
         // could not decide
     } elseif (is_string($color) || is_numeric($color)) {
         $tryword = ColorMap::wordToHex($color);
         if ($tryword) {
             $color = $tryword;
         }
         // could be a hex code or a color name
         $color = new Color_Hex($color);
         if (!$color->isHex()) {
             return false;
         }
         // whups not hex.
     }
     $class = strtolower(get_class($color));
     if (!$class) {
         return false;
     }
     // not color usable
     $hash = FALSE;
     switch (strtolower($to)) {
         case '#hex':
             $hash = TRUE;
         case 'hex':
             if ($class == 'color_hex') {
                 return $color->toString($hash);
             }
             if (method_exists($color, 'toHex')) {
                 return $color->toHex($hash);
             }
             break;
         case 'color_hex':
             if ($class == 'color_hex') {
                 return $color;
             }
             if (method_exists($color, 'toHex')) {
                 return new Color_Hex($color->toHex());
             }
             break;
         case 'rgb':
             if ($class == 'color_rgb') {
                 return $color->toArray();
             }
             if (method_exists($color, 'toRGB')) {
                 return $color->toRGB();
             }
             break;
         case 'color_rgb':
             if ($class == 'color_rgb') {
                 return $color;
             }
             if (method_exists($color, 'toRGB')) {
                 return new Color_RGB($color->toRGB());
             }
             break;
         case 'hsl':
             if ($class == 'color_hsl') {
                 return $color->toArray();
             }
             if (method_exists($color, 'toHSL')) {
                 return $color->toHSL();
             }
             break;
         case 'color_hsl':
             if ($class == 'color_hsl') {
                 return $color;
             }
             if (method_exists($color, 'toHSL')) {
                 return new Color_HSL($color->toHSL());
             }
             break;
     }
     return null;
 }