Zebra_Image::_hex2rgb PHP Method

_hex2rgb() private method

The RGB values will be a value between 0 and 255 each.
private _hex2rgb ( string $color, string $default_on_error = '#FFFFFF' ) : array
$color string Hexadecimal representation of a color (i.e. #123456 or #AAA). @param string $default_on_error Hexadecimal representation of a color to be used in case $color is not recognized as a hexadecimal color. @return array Returns an associative array with the values of (R)ed, (G)reen and (B)lue @access private
$default_on_error string
return array
    private function _hex2rgb($color, $default_on_error = '#FFFFFF')
    {
        // if color is not formatted correctly
        // use the default color
        if (preg_match('/^#?([a-f]|[0-9]){3}(([a-f]|[0-9]){3})?$/i', $color) == 0) {
            $color = $default_on_error;
        }
        // trim off the "#" prefix from $background_color
        $color = ltrim($color, '#');
        // if color is given using the shorthand (i.e. "FFF" instead of "FFFFFF")
        if (strlen($color) == 3) {
            $tmp = '';
            // take each value
            // and duplicate it
            for ($i = 0; $i < 3; $i++) {
                $tmp .= str_repeat($color[$i], 2);
            }
            // the color in it's full, 6 characters length notation
            $color = $tmp;
        }
        // decimal representation of the color
        $int = hexdec($color);
        // extract and return the RGB values
        return array('r' => 0xff & $int >> 0x10, 'g' => 0xff & $int >> 0x8, 'b' => 0xff & $int);
    }