Convert Hex Color to Rgb Values in PHP

Convert hex color to RGB values in PHP

Check out PHP's hexdec() and dechex() functions:
http://php.net/manual/en/function.hexdec.php

Example:

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

how do you convert a hex to rgb so that it can be used with imagecolourset?

You are trying to pass a variable of the data type STRING, when the PHP function imagecolorset() instead requires you to pass the rgb-values as three separate parameters of data type INTEGER.

In general, PHP is rather tolerant when it comes to Type Juggling, but you can not pass a comma separated string as a function parameter expecting the content of that string to be treated as separate parameters. It will be treated as one parameter, even if the string contains commas.

Instead change your function to return an array:

 function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);

if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
return array($r, $g, $b); // RETURN ARRAY INSTEAD OF STRING
}

And then use it like:

$homeRGB = hex2rgb($homeColour);
imagecolorset($him,$hindex, $homeRGB[0], $homeRGB[1], $homeRGB[2]);

If you for some reason absolutely can't or won't alter the hex2rgb-function, a workaround would be to use call_user_func_array():

$homeRGB = hex2rgb($homeColour);
call_user_func_array('imagecolorset', array_merge(array($him, $hindex), explode(',', $homeRGB)));

It's not recommended though because of the overhead of unnecessary imploding and exploding etc..

How to convert a string color to its hex code or RGB value?

There is no way to my knowledge to convert HTML color names or CSS color names to their Hex or RGB value in PHP. You'd have to create a map that knows the names (see Wiki article).

Someone has done this work for you already:

  • http://psoug.org/snippet/CSS_Colornames_to_RGB_values_415.htm

Excerpt:

$Colors  =  ARRAY( 
"black"=>array( "red"=>0x00, "green"=>0x00, "blue"=>0x00),
"maroon"=>array( "red"=>0x80, "green"=>0x00, "blue"=>0x00),
// ...
// more colors inbetween
// ...
"wheat"=>array( "red"=>0xF5, "green"=>0xDE, "blue"=>0xB3),
"whitesmoke"=>array( "red"=>0xF5, "green"=>0xF5, "blue"=>0xF5),
"yellowgreen"=>array( "red"=>0x9A, "green"=>0xCD, "blue"=>0x32)
);

and then simply get the RGB values via $maroon = $Colors['maroon'];

Further reference:

  • http://www.w3.org/TR/css3-color/#html4
  • http://www.crockford.com/wrrrld/color.html

PHP: Convert hex color from color input to PHP hex number

Perhaps this function is what you need?

http://www.php.net/manual/en/function.hexdec.php

"Returns the decimal equivalent of the hexadecimal number represented by the hex_string argument. hexdec() converts a hexadecimal string to a decimal number.

hexdec() will ignore any non-hexadecimal characters it encounters."

Some added info:

dechex() will give you a string containing a hex representation of the number if you need it, but it seems you should be dealing with an integer if it's a number that you need to pass around.

PHP longhand hex to RGB not working correctly

The algorithm is fine, you just have a wrong closing parenthesis at strlen($colorname > 4 ? 2 : 1) it should be strlen($colorname) > 4 ? 2 : 1 so change it to be like this and you'll have it working:

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

You can see it working at sandbox.onlinephpfunctions.com.

PHP function to convert HSL to RGB or Hex

Taking the code from one of the answers in the link of Jim's comment (PHP HSV to RGB formula comprehension), we can compute it as follows:

<?php    
$hue = 209;
$sat = 75;
$lum = 60;

$hue /= 360;
$sat /= 100;
$lum /= 100;

$result = ColorHSLToRGB($hue, $sat, $lum);
var_dump($result); echo '<br>';
printf("rgb = %d,%d,%d<br>", $result['r'], $result['g'], $result['b']);

function ColorHSLToRGB($h, $s, $l){

$r = $l;
$g = $l;
$b = $l;
$v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s);
if ($v > 0){
$m;
$sv;
$sextant;
$fract;
$vsf;
$mid1;
$mid2;

$m = $l + $l - $v;
$sv = ($v - $m ) / $v;
$h *= 6.0;
$sextant = floor($h);
$fract = $h - $sextant;
$vsf = $v * $sv * $fract;
$mid1 = $m + $vsf;
$mid2 = $v - $vsf;

switch ($sextant)
{
case 0:
$r = $v;
$g = $mid1;
$b = $m;
break;
case 1:
$r = $mid2;
$g = $v;
$b = $m;
break;
case 2:
$r = $m;
$g = $v;
$b = $mid1;
break;
case 3:
$r = $m;
$g = $mid2;
$b = $v;
break;
case 4:
$r = $mid1;
$g = $m;
$b = $v;
break;
case 5:
$r = $v;
$g = $m;
$b = $mid2;
break;
}
}
return array('r' => $r * 255.0, 'g' => $g * 255.0, 'b' => $b * 255.0);
}
?>

Output:

array(3) { ["r"]=> float(76.5) ["g"]=> float(155.55) ["b"]=> float(229.5) } 
rgb = 76,155,229

Convert negative color numbers to HEX or RGB

Your colours are indeed RGB values stored in a slightly twisted way.
All you have to do is, take the decimal number away from 16777216 and convert to HEX.

16777216-1 = 16777215 (FFFFFFh = White)

16777216-12582784 = 4194432 (400080h = Purple)

16777216-65408 = 16711808 (FF0080h = Pink)

16777216-16777216 = 0 (000000h = Black)

16777216-256 = 16776960 (FFFF00h = Yellow)

I hope this helps.
Apologies for not writing a code, but I am afraid I am not quite there yet in my studies. I promise I will work on it when I get there. :)



Related Topics



Leave a reply



Submit