0

I'm trying to write a PHP RGB-to-HEX converter and I'm trying to use a function to convert RGB numbers into letters if they're between 10 and 15 (and no, I can't use the "dechex()" function). This is how I have it coded right now:

function convToHex(&$hexInt)
{
    switch($hexInt){
        case 10:
            $hexInt = "A";
            break;
    }
}

//create six hexadecimal variables for "hexMain"

$hex1 = intval($r / 16);
$hex2 = $r % 16;
$hex3 = intval($g / 16);
$hex4 = $g % 16;
$hex5 = intval($b / 16);
$hex6 = $b % 16;

$rgb = "#" . $r . $g . $b;

echo convToHex($hex1);

The problem is that when I try to echo it, the value just comes up as 0. What would be the best way to convert "$hex1", "$hex2", and so-on without using "dechex()"?

3
  • Where you set $r, $g, $b, and when you return the function convToHex ? Apr 15, 2012 at 0:15
  • 1
    @GabrielSantos why would the function return if it is being passed a variable by reference?
    – Cameron
    Apr 15, 2012 at 0:19
  • Thanks, guys! $r, $g, and $b, are being called earlier on in the HTML input page as integer variables. I'm trying to use a placeholder variable so that I can alter six variables ($hex1 - $hex6) without any real trouble.
    – MrMedia715
    Apr 15, 2012 at 19:22

2 Answers 2

1

You need to return the value ($hexInt in this case) in your function. Then, there's no need for working with a reference.

function convToHex($hexInt) {
    // do things
    return $hexInt;
}

echo convToHex($hexInt);
1

Your problem is in this line:

echo convToHex($hex1);

If you want to pass by reference, then you need to call the function to alter the variable, then echo it (since the function won't return the value that it alters), e.g.

convToHex($hex1);
echo $hex1;

...also, any reason not to use something like:

function rgb2hex($r, $g, $b) {
  return sprintf("#%02X%02X%02X", $r, $g, $b);
}

or, if you want something closer to your original logic:

function rgb2hex($r, $g, $b) {
  // takes ints $r, $g, $b in the range 0-255 and returns a hex color string
  $hex_digits = "0123456789ABCDEF";
  $hex_string = "";

  $hex_string .= substr($hex_digits, $r / 16, 1);
  $hex_string .= substr($hex_digits, $r % 16, 1);
  $hex_string .= substr($hex_digits, $g / 16, 1);
  $hex_string .= substr($hex_digits, $g % 16, 1);
  $hex_string .= substr($hex_digits, $b / 16, 1);
  $hex_string .= substr($hex_digits, $b % 16, 1);

  return "#" . $hex_string;
}

to use either of these would involve something like:

$r = 12;
$g = 234;
$b = 45;

$hex_string = rgb2hex($r, $g, $b);
echo $hex_string . "\n";
5
  • Why echo $hex1 does not return $hex1 = intval($r / 16); result? Apr 15, 2012 at 0:25
  • {echo $hex1} isn't the problem here. The problem is trying to use a function to convert any number greater than 9 into a letter starting at A.
    – MrMedia715
    Apr 15, 2012 at 19:44
  • So, where do I call $hex_string outside of the function? I need to display it outside of the function.
    – MrMedia715
    Apr 15, 2012 at 19:53
  • @MrMedia715 I'm not sure I understand your first comment above, sorry. I added a usage example to my answer. I may have missed the point; is it important that you actually overwrite the variables that have the rgb info? I assumed you just wanted to use them to get a string with the hex value.
    – Cameron
    Apr 15, 2012 at 20:26
  • I used the solution you posted on the bottom. Sorry about that. I was trying to explain it, but I wasn't as clear on it. Thank you very much!
    – MrMedia715
    Apr 15, 2012 at 21:50

Your Answer

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.