Search code examples
phpcolorshex

Get Color Shade with PHP


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?


Solution

  • 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.