Source for file Color.php

Documentation is available at Color.php

  1. <?php
  2.  
  3. /**
  4.  * Validates Color as defined by CSS.
  5.  */
  6. {
  7.     
  8.     public function validate($color$config$context{
  9.         
  10.         static $colors null;
  11.         if ($colors === null$colors $config->get('Core''ColorKeywords');
  12.         
  13.         $color trim($color);
  14.         if ($color === ''return false;
  15.         
  16.         $lower strtolower($color);
  17.         if (isset($colors[$lower])) return $colors[$lower];
  18.         
  19.         if (strpos($color'rgb('!== false{
  20.             // rgb literal handling
  21.             $length strlen($color);
  22.             if (strpos($color')'!== $length 1return false;
  23.             $triad substr($color4$length 1);
  24.             $parts explode(','$triad);
  25.             if (count($parts!== 3return false;
  26.             $type false// to ensure that they're all the same type
  27.             $new_parts array();
  28.             foreach ($parts as $part{
  29.                 $part trim($part);
  30.                 if ($part === ''return false;
  31.                 $length strlen($part);
  32.                 if ($part[$length 1=== '%'{
  33.                     // handle percents
  34.                     if (!$type{
  35.                         $type 'percentage';
  36.                     elseif ($type !== 'percentage'{
  37.                         return false;
  38.                     }
  39.                     $num = (float) substr($part0$length 1);
  40.                     if ($num 0$num 0;
  41.                     if ($num 100$num 100;
  42.                     $new_parts["$num%";
  43.                 else {
  44.                     // handle integers
  45.                     if (!$type{
  46.                         $type 'integer';
  47.                     elseif ($type !== 'integer'{
  48.                         return false;
  49.                     }
  50.                     $num = (int) $part;
  51.                     if ($num 0$num 0;
  52.                     if ($num 255$num 255;
  53.                     $new_parts[= (string) $num;
  54.                 }
  55.             }
  56.             $new_triad implode(','$new_parts);
  57.             $color "rgb($new_triad)";
  58.         else {
  59.             // hexadecimal handling
  60.             if ($color[0=== '#'{
  61.                 $hex substr($color1);
  62.             else {
  63.                 $hex $color;
  64.                 $color '#' $color;
  65.             }
  66.             $length strlen($hex);
  67.             if ($length !== && $length !== 6return false;
  68.             if (!ctype_xdigit($hex)) return false;
  69.         }
  70.         
  71.         return $color;
  72.         
  73.     }
  74.     
  75. }

Documentation generated on Thu, 19 Jun 2008 18:48:56 -0400 by phpDocumentor 1.4.2