0

I am converting a Hexcode into RGB and when I echo this is outputs correctly EG, 55,55,55 . I am wanting to assign this RGB Value to a variable called RGBColour but it is just assigning the wordd 'array' to the variable. Can someone please give me a pointer as to what I am doing wrong.

$hexcolour = $_POST['hexcolour'];
$RGBcolour = list($r, $g, $b) = sscanf($charitycolour1, "#%02x%02x%02x"));
4
  • 2
    Sounds like $RGBcolour is an array. Try var_dump($RBGcolour); to see what it contains. Commented Jan 14, 2020 at 13:35
  • 3
    The code you're showing here will give a syntax error. Commented Jan 14, 2020 at 13:36
  • If it outputs a "55, 55, 55" string, you should use the split method like so: $RGBcolor = list($r, $g, $b) = split(",", "55,55,55"). Commented Jan 14, 2020 at 13:40
  • The given code does not define the variable $charitycolour1 after all. What have you tried to spot the problem in your real code? Commented Jan 14, 2020 at 16:06

1 Answer 1

2

RGBcolour is an array as the var_dump shows, but then you can get the rgb color again with a sprintf:

$charitycolour1 = '#ff3300';
$RGBcolour = list($r, $g, $b) = sscanf($charitycolour1, "#%02x%02x%02x");
var_dump($RGBcolour);


$RGBcolour = sprintf("#%02x%02x%02x", $r, $g, $b);
echo 'RGBcolour: '.$RGBcolour;

outputs

array(3) {
  [0]=>
  int(255)
  [1]=>
  int(51)
  [2]=>
  int(0)
}
RGBcolour: #ff3300

If you want the RGB as 255,51,0 you can use join:

$charitycolour1 = '#ff3300';
$RGBarray = sscanf($charitycolour1, "#%02x%02x%02x");
$RGBcolour = join(',', $RGBarray);
echo 'RGBcolour: '.$RGBcolour;

This outputs: RGBcolour: 255,51,0

And if later on you want the hexa value again from that 255,51,0 value:

$RGBcolour = '255,51,0';
$RGBarray = explode(',', $RGBcolour);
$hexaColour = sprintf("#%02x%02x%02x", $RGBarray[0], $RGBarray[1], $RGBarray[2]);
echo 'Hexa Colour: '.$hexaColour;

that outputs: Hexa Colour: #ff3300

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you. I have tried the below but it just sets the Hex code as the value of $RGBcolour and not the R,G,B, value. Any ideas?
Thanks so if the hex code was #ff3300 I was to be able to set $RGBcolour to 255,51,0 for its RGB value.
Perfect, thank you. If I want to post this value to a db, can I do so just referencing $RGBcolour ?
Exactly, now it's a string. Well, but keep in mind that when you work in a db it's a good practice to use prepared statements to prevent from SQL injection attacks. Don't just concatenate strings.
By the way, if you then restore that value from a database you'll need to split it (the oposite of the join to get the array again from the string) and then use the sprintf expression I used before to get it's original value in hexa
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.