1

I want to get brighter hex colour shade output from a given hex value with PHP. For example, I give the colour #cc6699 as input, and I want #ee88aa as the output colour. How would I go about doing this in PHP?

3 Answers 3

7

You need to convert the color to RGB, make the additions, and convert back:

// Convert string to 3 decimal values (0-255)
$rgb = array_map('hexdec', str_split("cc6699", 2));

// Modify color
$rgb[0] += 34;
$rgb[1] += 34;
$rgb[2] += 17;

// Convert back
$result = implode('', array_map('dechex', $rgb));

echo $result;

See it in action here.

8
  • @Jon Thanks very much your solution is well and i will do this
    – Anjum
    Commented Mar 20, 2011 at 11:06
  • @Jon Jon 1 thing i can not understand in third value difference $rgb[2] += 17; please clear me
    – Anjum
    Commented Mar 20, 2011 at 11:10
  • @jon Jon my input value is with # from my database and i need output value also with # hash for using in my css and i can not like to change values from database and my structure there is 32+ values saved with colorpicker.js
    – Anjum
    Commented Mar 20, 2011 at 11:16
  • @Anjum: 34 == 0x22 and 17 = 0x11. 0xcc + 0x22 => 0xcc, 0x66 + 0x22 => 0x88, 0x99c + 0x11 => 0xaa. All together: #cc6699 => #ee88aa.
    – Jon
    Commented Mar 20, 2011 at 11:17
  • @Jon oh sorry i am understand
    – Anjum
    Commented Mar 20, 2011 at 11:19
1
  1. split the color in three elements: cc, 66, 99
  2. Convert it to decimal with http://php.net/manual/de/function.hexdec.php
  3. Increment three decimal values
  4. Convert decimal to hex again
  5. put the three elements together
0

The best solution is to convert the RGB to HSL or HSV (just search google for php converter hsl/hsv).

Then you can play with the 'lightness' or 'value' values of the colorspace.

Afterwards convert it back to RGB colorspace.

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.