tags:

views:

1719

answers:

4

I've been using 3-digit Hex color values in CSS for a long time: #fff, #999, #069 etc., I can see how the repeating alphabets/numbers are merged to create a 3-digit hex color code, but I don't fully understand the pattern to be able to write a converter in PHP. Is there a documentation for this?

Edit: Oh, perhaps my question wasn't clear. I need to know how some of the 6 digit hex color values are converted to 3-digits? xxxxxx (ffffff) xxyyzz (006699), these are the only two patterns, correct?

I'd appreciate any help you can offer. Thanks!

+1  A: 

3 digit CSS code is short for 6 digits": #06a; is #0066aa;
Each two digits represent a number from 0 to 255.
Converting these values to hex and back is all you need.

Kobi
+3  A: 

To convert a 3-character hex code into a 6 character one, you need to repeat each character:

$hex = '#fff';
$hex6 = '#' . $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];

If you want to convert it to decimal you can use the hexdec function

Greg
+1  A: 

#f0f is expanded to #ff00ff so basically you just need to calculate the value and the value times 16 for each character, e.g.:

#f98: f = 15 => red = 15 + 15*16 = 255 etc.

jensgram
Ah, I seem to have missed the point - and my sense of consulting the API (`hexdec()`).
jensgram
A: 

In any 6 char hex code, omitting the even numbered character shall give the three char code. You can use $three_char_code = $six_char_code[0].$six_char_code[2].$six_char_code[4];

As far as color patterns are concerned, most of the browsers also decode color names well. e.g. {color:green;} instead of {color:#0F0;} or {color:#00FF00}. You may have further information at my site http://www.deepriya.net/colors/all_three_char_hex_code_colors.php , if you wish for.

DKSRathore