在PHP中将hex颜色转换为RGB值

什么是一个很好的方法来转换hex颜色值,如#ffffff到单个RGB值255 255 255使用PHP?

查看PHP的hexdec()和dechex()函数http://php.net/manual/en/function.hexdec.php示例:;

 $value = hexdec('ff'); // $value = 255 

如果你想把hex转换为rgb,你可以使用sscanf

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

输出:

 #ff9900 -> 255 153 0 

我做了一个函数,如果alpha作为代码下面的第二个参数提供,也返回alpha。

function

 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; } 

函数响应的例子

 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 ) 

如果您想以CSS格式返回rgb(a),只需将return $rgb; 在函数中return implode(array_keys($rgb)) . '(' . implode(', ', $rgb) . ')'; return implode(array_keys($rgb)) . '(' . implode(', ', $rgb) . ')';

对于任何感兴趣的人来说,这是另一种非常简单的方法。 这个例子假定只有6个字符,并没有前面的英镑符号。

 list($r, $g, $b) = array_map('hexdec', str_split($colorName, 2)); 

这里是一个例子支持4个不同的input(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)); 

我把@ John的回答和@ iic的评论/想法放在一起,可以同时处理通常的hex颜色代码和速记颜色代码。

简单的解释:

在scanf中,我读取了hex颜色的r,g和b值作为string。 不像@ John的答案那样是hex值。 在使用简写颜色代码的情况下,r,g和bstring在转换为小数之前必须加倍(“f” – >“ff”等)。

 function hex2rgb($hexColor) { $shorthand = (strlen($hexColor) == 4); list($r, $g, $b) = $shorthand? sscanf($hexColor, "#%1s%1s%1s") : sscanf($hexColor, "#%2s%2s%2s"); return [ "r" => hexdec($shorthand? "$r$r" : $r), "g" => hexdec($shorthand? "$g$g" : $g), "b" => hexdec($shorthand? "$b$b" : $b) ]; } 

将颜色代码HEX转换为RGB

 $color = '#ffffff'; $hex = str_replace('#','', $color); if(strlen($hex) == 3): $rgbArray['r'] = hexdec(substr($hex,0,1).substr($hex,0,1)); $rgbArray['g'] = hexdec(substr($hex,1,1).substr($hex,1,1)); $rgbArray['b'] = hexdec(substr($hex,2,1).substr($hex,2,1)); else: $rgbArray['r'] = hexdec(substr($hex,0,2)); $rgbArray['g'] = hexdec(substr($hex,2,2)); $rgbArray['b'] = hexdec(substr($hex,4,2)); endif; print_r($rgbArray); 

产量

 Array ( [r] => 255 [g] => 255 [b] => 255 ) 

我从这里find了这个参考 – 使用PHP将颜色Hex转换为RGB和RGB转换为hex

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

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

这将返回123,222,132

我的方法来处理有或没有散列,单值或配对值的hex颜色:

 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' ) ); 
 //if u want to convert rgb to hex $color='254,125,1'; $rgbarr=explode(",", $color); echo sprintf("#%02x%02x%02x", $rgbarr[0], $rgbarr[1], $rgbarr[2]); 

试试这个,它将它的参数(r,g,b)转换成hexhtml颜色的string#RRGGBB参数被转换为整数,并被修整为0..255的范围

 <?php function rgb2html($r, $g=-1, $b=-1) { if (is_array($r) && sizeof($r) == 3) list($r, $g, $b) = $r; $r = intval($r); $g = intval($g); $b = intval($b); $r = dechex($r<0?0:($r>255?255:$r)); $g = dechex($g<0?0:($g>255?255:$g)); $b = dechex($b<0?0:($b>255?255:$b)); $color = (strlen($r) < 2?'0':'').$r; $color .= (strlen($g) < 2?'0':'').$g; $color .= (strlen($b) < 2?'0':'').$b; return '#'.$color; } ?> 

哦,反之亦然

#开头的字符可以省略。 函数在范围内返回三个整数的数组(0..255),或者在识别颜色格式时返回false。

 <?php function html2rgb($color) { if ($color[0] == '#') $color = substr($color, 1); if (strlen($color) == 6) list($r, $g, $b) = array($color[0].$color[1], $color[2].$color[3], $color[4].$color[5]); elseif (strlen($color) == 3) list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]); else return false; $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); return array($r, $g, $b); } ?>