Home  >  Article  >  Backend Development  >  How to convert php hexadecimal color to rgb

How to convert php hexadecimal color to rgb

藏色散人
藏色散人Original
2021-05-08 10:15:102213browse

php How to convert hexadecimal color to rgb: First create a PHP sample file; then use the "public static function hex2rgb($color){...}" method to convert hexadecimal color to RGB color Just value.

How to convert php hexadecimal color to rgb

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

PHP hexadecimal color to RGB color value

/**
     * 十六进制转RGB
     * 
     * @param string $color 16进制颜色值
     * @return array
     */
    public static function hex2rgb($color) {
        $hexColor = str_replace('#', '', $color);
        $lens = strlen($hexColor);
        if ($lens != 3 && $lens != 6) {
            return false;
        }
        $newcolor = '';
        if ($lens == 3) {
            for ($i = 0; $i < $lens; $i++) {
                $newcolor .= $hexColor[$i] . $hexColor[$i];
            }
        } else {
            $newcolor = $hexColor;
        }
        $hex = str_split($newcolor, 2);
        $rgb = [];
        foreach ($hex as $key => $vls) {
            $rgb[] = hexdec($vls);
        }
        return $rgb;
    }

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert php hexadecimal color to rgb. For more information, please follow other related articles on the PHP Chinese website!

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