Convert hex color to RGB values in PHP

前端 未结 15 1786
轮回少年
轮回少年 2020-11-30 21:32

What would be a good way to convert hex color values like #ffffff into the single RGB values 255 255 255 using PHP?

相关标签:
15条回答
  • 2020-11-30 22:30

    My solution: (Supports short notation)

    $color = "#0ab";
    $colort = trim( $color );
    if( $colort and is_string( $color ) and preg_match( "~^#?([abcdef0-9]{3}|[abcdef0-9]{6})$~ui", $colort ))
    {
        if( preg_match( "~^#?[abcdef0-9]{3}$~ui", $colort ))
        {
            $hex = trim( $colort, "#" );
            list( $hexR, $hexG, $hexB ) = str_split( $hex );
            $hexR .= $hexR;
            $hexG .= $hexG;
            $hexB .= $hexB;
        }
        else
        {
            $hex = trim( $colort, "#" );
            list( $hexR, $hexG, $hexB ) = str_split( $hex, 2 );
        }
    
        $colorR = hexdec( $hexR );
        $colorG = hexdec( $hexG );
        $colorB = hexdec( $hexB );
    }
    
    // Test
    echo $colorR ."/" .$colorG ."/" .$colorB;
    // → 0/170/187
    
    0 讨论(0)
  • 2020-11-30 22:32

    This is the only solution that worked for me. Some of the answers were not consistent enough.

        function hex2rgba($color, $opacity = false) {
    
            $default = 'rgb(0,0,0)';
    
            //Return default if no color provided
            if(empty($color))
                  return $default;
    
            //Sanitize $color if "#" is provided
                if ($color[0] == '#' ) {
                    $color = substr( $color, 1 );
                }
    
                //Check if color has 6 or 3 characters and get values
                if (strlen($color) == 6) {
                        $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
                } elseif ( strlen( $color ) == 3 ) {
                        $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
                } else {
                        return $default;
                }
    
                //Convert hexadec to rgb
                $rgb =  array_map('hexdec', $hex);
    
                //Check if opacity is set(rgba or rgb)
                if($opacity){
                    if(abs($opacity) > 1)
                        $opacity = 1.0;
                    $output = 'rgba('.implode(",",$rgb).','.$opacity.')';
                } else {
                    $output = 'rgb('.implode(",",$rgb).')';
                }
    
                //Return rgb(a) color string
                return $output;
        }
        //hex2rgba("#ffaa11",1)
    
    0 讨论(0)
  • 2020-11-30 22:33

    Convert Color Code HEX to RGB

    $color = '#ffffff';
    $hex = str_replace('#','', $color);
    if(strlen($hex) == 3):
       $rgbArray['r'] = hexdec(substr($hex,0,1).substr($hex,0,1));
       $rgbArray['g'] = hexdec(substr($hex,1,1).substr($hex,1,1));
       $rgbArray['b'] = hexdec(substr($hex,2,1).substr($hex,2,1));
    else:
       $rgbArray['r'] = hexdec(substr($hex,0,2));
       $rgbArray['g'] = hexdec(substr($hex,2,2));
       $rgbArray['b'] = hexdec(substr($hex,4,2));
    endif;
    
    print_r($rgbArray);
    

    Output

    Array ( [r] => 255 [g] => 255 [b] => 255 )
    

    I have found this reference from here - Convert Color Hex to RGB and RGB to Hex using PHP

    0 讨论(0)
提交回复
热议问题