2

My $_POST value contains css friendly hex color value, for example: #ffffff. In order to make it friendly towards the ColorJizz library, I should have it in hexadecimal integer value. The code below doesn't work. If I replace the $color_hex variable with a hard coded value for example: 0xffffff, it works.

include('colorjizz.php');   
$color_hex = '0x' . substr($_POST['color'], 1); 
$color = new Hex($color_hex);

This is most likely a very noob level problem, but after hitting my head to the wall for a quite few hours, I'd be grateful for any advice. Thank you.

4
  • 1
    ColorJizz may be hosted on code.google.com, but it is not "Google's" package. Commented Apr 14, 2011 at 0:49
  • 1
    Can you put (int) at the front? Commented Apr 14, 2011 at 0:49
  • What is the actual output of $color_hex in this example? I can't see an issue with it, assuming you are indeed getting a valid 6 digit hex value for $_POST['color'], WITH the # number sign character. Commented Apr 14, 2011 at 0:49
  • Hi Madmartigan, thanks for the reply. The actual $color_hex output in this example could be for example "0xFFFFFF" when the $_POST['color'] value has been #FFFFFF. Unfortunately ColorJizz doesn't like to work with it, most likely due to a wrong data type. Commented Apr 15, 2011 at 0:42

3 Answers 3

2

"Hexadecimal integer value" doesn't make much sense. Hex is a representation of a number (thus a string), while integer value speaks of the machine format of a number.

If you want the number, a quick Google search found hexdec

$color = hexdec(substr($_POST['color'], 1));

It appears to ignore leading "junk", so you could even use

$color = hexdec($_POST['color']);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply ikegami. And oh, I had tried the hexdec method, but since it didn't output a value with a preceding "0x", for example "0xFFFFFF" I though it doesn't fit. But it sure does accept it. And it works with this now. Great! Thank you.
@Tuomas, 0xFFFFFF is a numeric literal (a piece of the code) that produces the number 16,777,215. hexdec('FFFFFF') is another piece of code that produces the same number. Having the string 0xFFFFFF in a variable is much different than having 0xFFFFFF in the code.
1

If you have a string containing a hexadecimal representation of a number and you want to have it as a integer you have to convert from hex to dec. If I get your question right.

$dec = hexdec("ff0000");

http://php.net/hexdec

Comments

0

In PHP, the data type for a hexadecimal integer and a decimal integer is the same; being able to type 0xDEAD in your source code is just syntactic sugar. Thus, you can use intval() to convert the form input to a normal PHP integer from a string:

$color_hex = intval(substr($_POST['color'], 1), 16);

The final parameter, 16, specifies that the input string is in hexadecimal.

Comments

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.