2

I need to call a function, in PHP, that accepts 3 parameters, RGB Values. This function converts RGB color values to HSL values, so (R,G,B) is needed in the parenthesis.

This is my function:

function RGBtoHSL($red, $green, $blue) {
  // convert colors
}

Which, if I make a test call of the following, it works just fine:

RGBtoHSL(255,0,0);

and also works like this:

RGBtoHSL(255,000,000);

Now, further down my page I have a variable $displayRGB which holds the current pixels RGB values in this format xxx,xxx,xxx. I've echoed this variable to test the format matches my requirements and it does, but when I try and add this variable to my function caller, it fails with the error "Missing argument 2, Missing argument 3" and points to this line:

RGBtoHSL($displayRGB);

I'm still teething in PHP (come from ASP), can somebody please help point me in the right direction and pass me my dummy?

3
  • 2
    @David - That's not how upvotes work - People upvote answers based on their merit, not when they were posted. Commented Jul 25, 2012 at 21:44
  • 2
    @David: consider yourself downvoted for being cheeky. That's not how it works. Commented Jul 25, 2012 at 21:49
  • 1
    He withdrew that comment pretty quickly buhahahaha Commented Jul 25, 2012 at 21:51

6 Answers 6

4

Your $displayRGB is a single variable (of type string, I presume). What you can do is split this string into an array:

$rgbArray = explode(',', $displayRGB);

Then pass it to your function

RGBtoHSL($rgbArray[0], $rgbArray[1], $rgbArray[2]);
Sign up to request clarification or add additional context in comments.

Comments

3

You can't pass in an array (I assume $displayRGB is an array) as "all three arguments" in PHP. Try

RGBtoHSL($displayRGB[0], $displayRGB[1], $displayRGB[2]);

or modify your function to accept an array.

If $displayRGB is a string of "xxx,yyy,zzz" you can run an explode on it

$colors = explode(",", $displayRGB);

and it will set $colors as an array with indices containing xxx, yyy and zzz.

Then pass it as I mentioned above.

3 Comments

I suspect that OP is providing a string, but yeah, the error is pretty clear.
Edited to reflect these comments.
Thanks Matt, it worked fine. It was a string so thanks for the edit too.
1

try this instead of eval

call_user_func_array('RGBtoHSL', explode(',', $displayRGB));

Comments

0

what's your $displayRGB value?

if it's "255,0,0" you should first do "explode"

e.g.

<?php

list($r,$g,$b)=explode(',',$displayRGB);
RGBtoHSL($r,$g,$b);

1 Comment

Nice usage of list(). I didn't think of that.
-2

I will probably get flamed for this, but this would definitely work:

eval("RGBtoHSL($displayRGB);");

Don't do it. It will work... but don't do it.

1 Comment

That's funny. "Don't do it. It will work... but don't do it" totally put me off your answer. I said to myself "Don't try it". Nice one though.
-2

You can't put a string in a function and expect it to explode the string for you.

You need to go like this:

$string = '255,0,0';
$array = explode(',', $string);
RGBtoHSL($array[0], $array[1], $array[2]);

1 Comment

-1 Should be explode(',', $string); with a comma. This will throw syntax error.

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.