Home > Backend Development > PHP Problem > How to convert php hexadecimal color to rgb

How to convert php hexadecimal color to rgb

藏色散人
Release: 2023-03-09 13:58:01
Original
2240 people have browsed it

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;
    }
Copy after login

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!

Related labels:
php
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