SimpleImage::normalize_color PHP Method

normalize_color() protected method

Converts a hex color value to its RGB equivalent
protected normalize_color ( string $color ) : array | boolean
$color string Hex color string, array(red, green, blue) or array(red, green, blue, alpha). Where red, green, blue - integers 0-255, alpha - integer 0-127
return array | boolean
    protected function normalize_color($color)
    {
        if (is_string($color)) {
            $color = trim($color, '#');
            if (strlen($color) == 6) {
                list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
            } elseif (strlen($color) == 3) {
                list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
            } else {
                return false;
            }
            return array('r' => hexdec($r), 'g' => hexdec($g), 'b' => hexdec($b), 'a' => 0);
        } elseif (is_array($color) && (count($color) == 3 || count($color) == 4)) {
            if (isset($color['r'], $color['g'], $color['b'])) {
                return array('r' => $this->keep_within($color['r'], 0, 255), 'g' => $this->keep_within($color['g'], 0, 255), 'b' => $this->keep_within($color['b'], 0, 255), 'a' => $this->keep_within(isset($color['a']) ? $color['a'] : 0, 0, 127));
            } elseif (isset($color[0], $color[1], $color[2])) {
                return array('r' => $this->keep_within($color[0], 0, 255), 'g' => $this->keep_within($color[1], 0, 255), 'b' => $this->keep_within($color[2], 0, 255), 'a' => $this->keep_within(isset($color[3]) ? $color[3] : 0, 0, 127));
            }
        }
        return false;
    }