Ejemplo n.º 1
0
	/**
	Returns the mapping of hex strings to arrays of color words
	
	@access static
	@return Array An array of HexString => ColorWords[]
	*/
	function getHexToWordsMap ()
	{
		$hexToWordMap = array();
		$wordToHexMap = ColorMap::getWordToHexMap();
		
		foreach ($wordToHexMap as $word => $hex)
			$hexToWordMap[$hex][] = $word;
		
		return $hexToWordMap;
	}
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 public function Draw($aTitle, $aStart, $aEnd, $n = 64, $aReverse = false, $addColorNames = false)
 {
     // Setup to draw colormap with names platoe colors
     $lmarg = ColorMapDriver::LMARG;
     // left margin
     $rmarg = ColorMapDriver::RMARG;
     // right margin
     $width = ColorMapDriver::WIDTH;
     // Overall image width
     // Module height
     $mh = ColorMapDriver::MODHEIGHT;
     // Step between each map
     $ymarg = $mh + ColorMapDriver::MAPMARG;
     if ($addColorNames) {
         $ymarg += 50;
     }
     // Start position
     $xs = $lmarg;
     $ys = ColorMapDriver::YSTART;
     // Setup a basic canvas graph
     $height = ($aEnd - $aStart + 1) * $ymarg + 50;
     $graph = new CanvasGraph($width, $height);
     $graph->img->SetColor('darkgray');
     $graph->img->Rectangle(0, 0, $width - 1, $height - 1);
     $t = new Text($aTitle, $width / 2, 5);
     $t->SetAlign('center', 'top');
     $t->SetFont(FF_ARIAL, FS_BOLD, 14);
     $t->Stroke($graph->img);
     // Instantiate a colormap
     $cm = new ColorMap();
     $cm->InitRGB($graph->img->rgb);
     for ($mapidx = $aStart; $mapidx <= $aEnd; ++$mapidx, $ys += $ymarg) {
         $cm->SetMap($mapidx, $aReverse);
         $n = $cm->SetNumColors($n);
         list($mapidx, $maparray) = $cm->GetCurrMap();
         $ncols = count($maparray);
         $colbuckets = $cm->GetBuckets();
         // The module width will depend on the actual number of colors
         $mw = round(($width - $lmarg - $rmarg) / $n);
         // Draw color map title (name)
         $t->Set('Basic colors: ' . $ncols . ',   Total colors: ' . $n);
         $t->SetAlign('center', 'bottom');
         $t->SetAngle(0);
         $t->SetFont(FF_TIMES, FS_NORMAL, 14);
         $t->Stroke($graph->img, $width / 2, $ys - 3);
         // Add the name/number of the map to the left
         $t->SetAlign('right', 'center');
         $t->Set('Map: ' . $mapidx);
         $t->SetFont(FF_ARIAL, FS_NORMAL, 14);
         $t->Stroke($graph->img, $xs - 20, round($ys + $mh / 2));
         // Setup text properties for the color names
         if ($addColorNames) {
             $t->SetAngle(30);
             $t->SetFont(FF_ARIAL, FS_NORMAL, 12);
             $t->SetAlign('right', 'top');
         }
         // Loop through all colors in the map
         $x = $xs;
         $y = $ys;
         $k = 0;
         for ($i = 0; $i < $n; ++$i) {
             $graph->img->SetColor($colbuckets[$i]);
             $graph->img->FilledRectangle($x, $y, $x + $mw, $y + $mh);
             // Mark all basic colors in the map with a bar and name
             if ($i % (($n - $ncols) / ($ncols - 1) + 1) == 0) {
                 $graph->img->SetColor('black');
                 $graph->img->FilledRectangle($x, $y + $mh + 4, $x + $mw - 1, $y + $mh + 6);
                 if ($addColorNames) {
                     $t->Set($maparray[$k++]);
                     $t->Stroke($graph->img, $x + $mw / 2, $y + $mh + 10);
                 }
             }
             $x += $mw;
         }
         // Draw a border around the map
         $graph->img->SetColor('black');
         $graph->img->Rectangle($xs, $ys, $xs + $mw * $n, $ys + $mh);
     }
     // Send back to client
     $graph->Stroke();
 }
Ejemplo n.º 4
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;
 }