How to alter brightness of a single rgb color simply and easily via php


A quesion about RGB color and finding the simplest, tiniest, php conversion code for manipulating the lightness/darkness of a given RGB hue.

Imagine a variable $colorA containning a valid six char RGB color, like F7A100 which we want to make a bit lighter and/or darker:

$color  = B1B100;                     // original RGB color manually set.

Then, at any page have that color bit darker/lighter on the fly:

$colorX = someFunction($color, +10);  // original color 10 steps lighter
$colorY = someFunction($color, -25);  // original color 25 steps darker

What would be YOUR way of solving this? Keep the RGB as is or first change it to HSL? Hints and suggestions are welcome. Your sample/code is welcome too.

This really focuses to the TINIES / SIMPLES / SHORTEST possible code to just make the same hue bit darker/lighter.

I deliberately do not suggest my code, as I want to keep possibilities open in here.

Best Solution

The absolutely simplest solution is to add some constant (like 1) to each part of the color representation: [R, G, B]. This is due to the fact that max values of all [R, G, B] represent white, while min values - black. In pseudo-code (assuming 255 is max, sorry, I don't know PHP):

lighter(R, G, B) = [
    min(255, R + 1), 
    min(255, G + 1), 
    min(255, B + 1)

You must keep in mind though that this transformation is way too simplistic and the proper implementation would be to convert to HSL/HSB, increase H and transform back to RGB.