31

in my code i have

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

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

$color = '#ffffff';
2

7 Answers 7

113

A simple sprintf will do.

$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
8

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

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

0
8

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

$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

3
  • 4
    First line of code looks wrong, is this meant as an array or did you forget the "rgb" function name?
    – donquixote
    Nov 2, 2017 at 2:31
  • It's an array. Worked out really well for me, so I didn't find anything wrong with it. Nov 2, 2017 at 2:51
  • Yes. Well I'm not much of an expert of PHP, but the above code did the trick for me in this case! I thought it might also help others so put it here... Nov 2, 2017 at 3:00
6

Here is a function that will accept the string version of either an rgb or rgba and return the hex color.

    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

0
0

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

In case you want to use external solutions, spatie has a package for doing exacly what you want:

https://github.com/spatie/color

$rgb = Rgb::fromString('rgb(55,155,255)');

echo $rgb->red(); // 55
echo $rgb->green(); // 155
echo $rgb->blue(); // 255

echo $rgb; // rgb(55,155,255)

$rgba = $rgb->toRgba(); // `Spatie\Color\Rgba`
$rgba->alpha(); // 1
echo $rgba; // rgba(55,155,255,1)

$hex = $rgb->toHex(); // `Spatie\Color\Hex`
echo $hex; // #379bff

$hsl = $rgb->toHsl();
echo $hsl; // hsl(210,100%,100%)
0

Based on the function from Mat Lipe, this changes all instances of rgb colors in a string:

$html = preg_replace_callback("/rgba?\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,*\s*\d*\s*\)/i", function($matches) {
    return strtoupper(sprintf("#%02x%02x%02x", $matches[1], $matches[2], $matches[3]));
}, $html);

You can remove strtoupper if you prefer lowercase hex.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.