/** * @access public * @static * @author Laurent Laville <*****@*****.**> */ function color2RGB($color) { $c = array(); if ($color[0] == '#') { $c = Image_Color::hex2rgb($color); } else { $c = Image_Color::namedColor2RGB($color); } return $c; }
/** * Convert any color-representation into an array of 4 ints (RGBA). * * Userdefined color specifications get translated into * an array of rgb values. * * @param mixed $color Any color representation supported by Image_Canvas_Color::color2RGB() * * @return array Array of 4 ints (RGBA-representation) * @access public * @static */ function color2RGB($color) { if (is_array($color)) { if (!is_numeric($color[0])) { return null; // error } if (count($color) == 3) { // assume RGB-color // 255 = alpha-value; full opaque return array((int) $color[0], (int) $color[1], (int) $color[2], 255); } if (count($color) == 4) { // assume RGBA-color // 255 = alpha-value; full opaque return array((int) $color[0], (int) $color[1], (int) $color[2], (int) $color[3]); } return null; // error } elseif (is_string($color)) { $alphaPos = strpos($color, '@'); if ($alphaPos === false) { $alpha = 255; } else { $alphaFloat = (double) substr($color, $alphaPos + 1); // restrict to range 0..1 $alphaFloat = max(min($alphaFloat, 1), 0); $alpha = (int) round((double) 255 * $alphaFloat); $color = substr($color, 0, $alphaPos); } if ($color[0] == '#') { // hex-color given, e.g. #FFB4B4 $tempColor = parent::hex2rgb($color); return array((int) $tempColor[0], (int) $tempColor[1], (int) $tempColor[2], $alpha); } if (strpos($color, '%') !== false) { $tempColor = parent::percentageColor2RGB($color); return array((int) $tempColor[0], (int) $tempColor[1], (int) $tempColor[2], $alpha); } else { $tempColor = parent::namedColor2RGB($color); return array((int) $tempColor[0], (int) $tempColor[1], (int) $tempColor[2], $alpha); } } else { return null; // error } }
/** * Draws a clipped line from ($x1, $y1) to ($x2, $y2) * using $color. * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 * @param mixed $color * @access public */ function drawClippedLine($x1, $y1, $x2, $y2, $color) { if (($x1 > $this->max['x'] || $x1 < $this->min['x'] || $y1 > $this->max['y'] || $y1 < $this->min['y']) && ($x2 > $this->max['x'] || $x2 < $this->min['x'] || $y2 > $this->max['y'] || $y2 < $this->min['y'])) { if ($this->debug) { printf('clipped x1: %d %d %d<br />', $x1, $this->min['x'], $this->max['x']); printf('clipped y1: %d %d %d<br />', $y1, $this->min['y'], $this->max['y']); printf('clipped x2: %d %d %d<br />', $x2, $this->min['x'], $this->max['x']); printf('clipped y2: %d %d %d<br />', $y2, $this->min['y'], $this->max['y']); } } else { if (!is_array($color)) { $color = Image_Color::namedColor2RGB($color); } $x1 = $this->polar2image($x1, 'x'); $y1 = $this->polar2image($y1, 'y'); $x2 = $this->polar2image($x2, 'x'); $y2 = $this->polar2image($y2, 'y'); if ($this->debug) { printf('Drawing line (%s, %s) -> (%s, %s)<br />', $x1, $y1, $x2, $y2); } $this->drawLine($x1, $y1, $x2, $y2, $color[0], $color[1], $color[2]); } }
function testNamedColor2RGB_InvalidReturnsBlack() { $result = Image_Color::namedColor2RGB('NOT A REAL COLOR'); $this->assertEquals(array(0, 0, 0), $result); }