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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32962624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:18:30  来源:igfitidea点击:

Convert RGB to hex color values in PHP

php

提问by ravichandra

in my code i have

在我的代码中我有

$color = rgb(255, 255, 255);

i want to convert this into hex color code.Out put like

我想把它转换成十六进制颜色代码。输出就像

$color = '#ffffff';

回答by user3942918

A simple sprintfwill do.

一个简单的sprintf就行了。

$color = sprintf("#%02x%02x%02x", 13, 0, 255); // #0d00ff

To break down the format:

分解格式:

  • #- the literal character #
  • %- start of conversion specification
  • 0- character to be used for padding
  • 2- minimum number of characters the conversion should result in, padded with the above as necessary
  • x- the argument is treated as an integer and presented as a hexadecimal number with lowercase letters
  • %02x%02x- the above four repeated twice more
  • #- 文字字符#
  • %- 转换规范的开始
  • 0- 用于填充的字符
  • 2- 转换应产生的最少字符数,必要时用上述内容填充
  • x- 参数被视为整数,并表示为带有小写字母的十六进制数
  • %02x%02x- 以上四个重复了两次

回答by IAmMilinPatel

You can try this simple piece of code below. You can pass the rgb code dynamically as well in the code.

你可以试试下面这段简单的代码。您也可以在代码中动态传递 rgb 代码。

$rgb = (123,222,132);
$rgbarr = explode(",",$rgb,3);
echo sprintf("#%02x%02x%02x", $rgbarr[0], $rgbarr[1], $rgbarr[2]);

This will code return like #7bde84

这将代码返回如#7bde84

回答by Suyog

You can use following function

您可以使用以下功能

function fromRGB($R, $G, $B)
{

    $R = dechex($R);
    if (strlen($R)<2)
    $R = '0'.$R;

    $G = dechex($G);
    if (strlen($G)<2)
    $G = '0'.$G;

    $B = dechex($B);
    if (strlen($B)<2)
    $B = '0'.$B;

    return '#' . $R . $G . $B;
}

Then, echo fromRGB(115,25,190);will print #7319be

然后,echo fromRGB(115,25,190);将打印#7319be

Source: RGB to hex colors and hex colors to RGB - PHP

来源:RGB 到十六进制颜色和十六进制颜色到 RGB - PHP

回答by Mat Lipe

Here is a function that will accept the string version of either an rgbor rgbaand return the hexcolor.

这里是一个将接受一个字符串版本的函数rgbrgba并返回hex颜色。

    function rgb_to_hex( string $rgba ) : string {
        if ( strpos( $rgba, '#' ) === 0 ) {
            return $rgba;
        }

        preg_match( '/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i', $rgba, $by_color );

        return sprintf( '#%02x%02x%02x', $by_color[1], $by_color[2], $by_color[3] );
    }

Example: rgb_to_hex( 'rgba(203, 86, 153, 0.8)' );// Returns #cb5699

示例: rgb_to_hex( 'rgba(203, 86, 153, 0.8)' );// 返回#cb5699

回答by Rajarshi Das

You can try this

你可以试试这个

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