在PHP中将十六进制颜色转换为RGB值

use*_*456 65 php rgb hex colors

使用PHP 将十六进制颜色值#ffffff转换为单个RGB值的好方法是255 255 255什么?

小智 241

如果要将hex转换为rgb,可以使用sscanf:

<?php
$hex = "#ff9900";
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
echo "$hex -> $r $g $b";
?>
Run Code Online (Sandbox Code Playgroud)

输出:

#ff9900 -> 255 153 0
Run Code Online (Sandbox Code Playgroud)

  • 使用`sscanf()`很干净的方法,我把它放在我的魔术盒中.不幸的是,看起来OP太懒了,无法弄清楚你在做什么以及你在想这个想法. (14认同)
  • 而对于速记颜色(`#ccc`):`(strlen($ hex)=== 4)?list($ r,$ g,$ b)= sscanf($ hex,"#%1x%1x%1x"):list($ r,$ g,$ b)= sscanf($ hex,"#%2x% 2×2×%");` (9认同)
  • @iiic只是为你的单行编写测试.#ccc将返回12,12,12而不是204,204,204,所以如果你将它恢复为十六进制,你将获得颜色#0c0c0c. (3认同)

Nie*_*een 46

查看PHP hexdec()dechex()函数:http: //php.net/manual/en/function.hexdec.php

例:

$value = hexdec('ff'); // $value = 255
Run Code Online (Sandbox Code Playgroud)

  • 要使用此答案将给定的十六进制值完全转换为 RGB,一个选项如下: `$split_hex_color = str_split( $hex_color, 2 ); $rgb1 = hexdec( $split_hex_color[0] ); $rgb2 = hexdec( $split_hex_color[1] ); $rgb3 = hexdec( $split_hex_color[2] ); ` (11认同)

Jak*_*ake 34

我创建了一个函数,如果提供alpha作为代码在下面的第二个参数,它也返回alpha.

功能

function hexToRgb($hex, $alpha = false) {
   $hex      = str_replace('#', '', $hex);
   $length   = strlen($hex);
   $rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0));
   $rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0));
   $rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0));
   if ( $alpha ) {
      $rgb['a'] = $alpha;
   }
   return $rgb;
}
Run Code Online (Sandbox Code Playgroud)

功能响应的示例

print_r(hexToRgb('#19b698'));
Array (
   [r] => 25
   [g] => 182
   [b] => 152
)

print_r(hexToRgb('19b698'));
Array (
   [r] => 25
   [g] => 182
   [b] => 152
)

print_r(hexToRgb('#19b698', 1));
Array (
   [r] => 25
   [g] => 182
   [b] => 152
   [a] => 1
)

print_r(hexToRgb('#fff'));
Array (
   [r] => 255
   [g] => 255
   [b] => 255
)
Run Code Online (Sandbox Code Playgroud)

如果你想以CSS格式返回rgb(a),只需用return $rgb;函数替换函数中的行return implode(array_keys($rgb)) . '(' . implode(', ', $rgb) . ')';


aro*_*ino 26

对于任何有兴趣的人来说,这是另一种非常简单的方法.此示例假定正好有6个字符且没有前面的井号.

list($r, $g, $b) = array_map('hexdec', str_split($colorName, 2));
Run Code Online (Sandbox Code Playgroud)

以下是支持4种不同输入(abc,aabbcc,#abc,#aabbcc)的示例:

list($r, $g, $b) = array_map(function($c){return hexdec(str_pad($c, 2, $c));}, str_split(ltrim($colorName, '#'), strlen($colorName) > 4 ? 2 : 1));
Run Code Online (Sandbox Code Playgroud)

  • 使用`ltrim($ colorName,'#')`代替`$ colorName`来处理#如果它可能在那里 (10认同)

Rat*_*ajS 13

您可以使用该函数hexdec(hexStr: String)来获取十六进制字符串的十进制值.

请参阅下面的示例:

$split = str_split("ffffff", 2);
$r = hexdec($split[0]);
$g = hexdec($split[1]);
$b = hexdec($split[2]);
echo "rgb(" . $r . ", " . $g . ", " . $b . ")";
Run Code Online (Sandbox Code Playgroud)

这将打印 rgb(255, 255, 255)


IAm*_*tel 6

你可以试试下面这段简单的代码。

list($r, $g, $b) = sscanf(#7bde84, "#%02x%02x%02x");
echo $r . "," . $g . "," . $b;
Run Code Online (Sandbox Code Playgroud)

这将返回 123,222,132


use*_*641 6

我编写了一个像这样的简单函数,它支持带或不带开头的输入值#,并且还可以接受 3 或 6 个字符的十六进制代码输入:


function hex2rgb( $color ) {

    if ($color[0] == '#') {
        $color = substr($color, 1);
    }
    list($r, $g, $b) = array_map("hexdec", str_split($color, (strlen( $color ) / 3)));
    return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}
Run Code Online (Sandbox Code Playgroud)

这将返回一个关联数组,可以通过$color['red'], $color['green'], $color['blue'];进行访问。

另请参阅此处的 CSS 技巧。


小智 5

我处理带有或不带有散列、单个值或对值的十六进制颜色的方法:

function hex2rgb ( $hex_color ) {
    $values = str_replace( '#', '', $hex_color );
    switch ( strlen( $values ) ) {
        case 3;
            list( $r, $g, $b ) = sscanf( $values, "%1s%1s%1s" );
            return [ hexdec( "$r$r" ), hexdec( "$g$g" ), hexdec( "$b$b" ) ];
        case 6;
            return array_map( 'hexdec', sscanf( $values, "%2s%2s%2s" ) );
        default:
            return false;
    }
}
// returns array(255,68,204)
var_dump( hex2rgb( '#ff44cc' ) );
var_dump( hex2rgb( 'ff44cc' ) );
var_dump( hex2rgb( '#f4c' ) );
var_dump( hex2rgb( 'f4c' ) );
// returns false
var_dump( hex2rgb( '#f4' ) );
var_dump( hex2rgb( 'f489' ) );
Run Code Online (Sandbox Code Playgroud)


gre*_*994 5

借用 @jhon 的答案 - 这将以字符串格式返回 rgb,并带有不透明度选项。

function convert_hex_to_rgba($hex, $opacity = 1){
    list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
    return sprintf('rgba(%s, %s, %s, %s)', $r, $g, $b, $opacity);
}

convert_hex_to_rgba($bg_color, 0.9) // rgba(2,2,2,0.9)
Run Code Online (Sandbox Code Playgroud)