Home > Backend Development > PHP Tutorial > PHP method to convert color hex value into rgb_php tips

PHP method to convert color hex value into rgb_php tips

WBOY
Release: 2016-05-16 19:53:25
Original
1715 people have browsed it

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"));

Copy after login

The running results are as follows:

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

Copy after login

For color value conversion and acquisition, you can also refer to the online tools 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 the special topics on this site: "Comprehensive collection of PHP array (Array) operation skills", "Summary of PHP mathematical operation skills", "Summary of PHP graphics and image operation skills", "Summary of PHP operation skills for office documents (including word, excel, access, ppt)", "PHP date and time usage Summary》, "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial》and《Summary of common database operation skills in PHP

I hope this article will be helpful to everyone in PHP programming.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template