Habari\ColorUtils::hex_rgb PHP Метод

hex_rgb() публичный статический Метод

Converts a HTML style hex string ('#7f6699') to an RGB array.
public static hex_rgb ( $hex_string )
    public static function hex_rgb($hex_string)
    {
        $hex_string = ltrim($hex_string, '#');
        if (!preg_match('/^[0-9a-f]+$/i', $hex_string)) {
            return Error::raise(_t('Not a valid hex color.'));
        }
        $normalized = '';
        switch (strlen($hex_string)) {
            case 3:
                // 'fed' = 'ffeedd'
                for ($i = 0; $i < 3; $i++) {
                    $normalized .= $hex_string[$i] . $hex_string[$i];
                }
                break;
            case 6:
                // already normal
                $normalized = $hex_string;
                break;
            case 2:
            case 4:
                // who uses this anyway!
                $normalized = $hex_string . str_repeat('0', 6 - strlen($hex_string));
                break;
            default:
                return Error::raise(_t('Not a valid color format.'));
        }
        return self::rgb_rgbarr(hexdec(substr($normalized, 0, 2)), hexdec(substr($normalized, 2, 2)), hexdec(substr($normalized, 4, 2)));
    }

Usage Example

Пример #1
0
 public static function get_spaminess_style(Comment $comment)
 {
     if (isset($comment->info->defensio_spaminess) && $comment->status == Comment::status('spam')) {
         $grad_hex = create_function('$s,$e,$i', 'return (($e-$s)*$i)+$s;');
         $start_hex = '#FFD6D7';
         $end_hex = '#F8595D';
         $border = ColorUtils::rgb_hex(array_combine(array('r', 'g', 'b'), array_map($grad_hex, ColorUtils::hex_rgb($start_hex), ColorUtils::hex_rgb($end_hex), array_pad(array(), 3, $comment->info->defensio_spaminess))));
         return "border-left-color:#{$border}; border-right-color:#{$border};";
     } elseif ($comment->status == self::COMMENT_STATUS_QUEUED) {
         return 'border-left: 3px solid #BCCFFF; border-right: 3px solid #BCCFFF;';
     }
     return '';
 }