/** * 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 } }
function testPercentageColor2RGB_2digits() { $result = Image_Color::percentageColor2RGB("10%,50%,90%"); $this->assertEquals(array(26, 127, 229), $result); }