Home  >  Article  >  Backend Development  >  PHP method to convert color hex value into rgb

PHP method to convert color hex value into rgb

WBOY
WBOYOriginal
2016-07-29 08:51:391466browse

The example in this article describes the method of converting color hex value into rgb in PHP. Share it with everyone for your reference, the details are as follows:

function hex2rgb( $colour ) { 
    if ( $colour[0] == '#' ) { 
        $colour = substr( $colour, 1 ); 
    } 
    if ( strlen( $colour ) == 6 ) { 
        list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] ); 
    } elseif ( strlen( $colour ) == 3 ) { 
        list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] ); 
    } else { 
        return false; 
    } 
    $r = hexdec( $r ); 
    $g = hexdec( $g ); 
    $b = hexdec( $b ); 
    return array( 'red' => $r, 'green' => $g, 'blue' => $b ); 
} 
//测试 
var_dump(hex2rgb("#eeeeee"));

The running results are as follows:

array(3) {
 ["red"]=>
 int(238)
 ["green"]=>
 int(238)
 ["blue"]=>
 int(238)
}

For color value conversion and acquisition, you can also refer to the online tool of this site:

RGB Color Coding Generator

Online web color matching tool

RGB color query comparison table_Color code table_Complete English names of colors

Readers who are interested in more PHP-related content can check out this site's special topic: "Complete PHP Array (Array) Operation Skills" ", "Summary of PHP mathematical operation skills", "Summary of PHP graphics and image operation skills", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "php "Object-oriented programming introductory tutorial", "php string (string) usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope that this article will be helpful to everyone's PHP programming design help.

The above introduces the method of converting color hex value into rgb in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn