Color lightening function

Maybe someone met the finished one?

For example, we give 006a44 as input, we get 00ce44.

That is, the same color, only lighter… Theoretically, there is nothing complicated, we translate the color into 10-decimal, and increase the middle (G), for example, by 100, if it is already a maximum of 255 or more than 200, then increase the first (R).

But I’m having more trouble converting their 16 to decimal.

function lighter($color)
    $x1 = dechex($color[0] . $color[1]);
    $x2 = dechex($color[2] . $color[3]);
    $x3 = dechex($color[4] . $color[5]);
    if ($x2 > 220) { //  
        if ($x1 + 150 > 255) {
            $x1 = 255;
        } else {
            $x1 = $x1 + 100;
    } else {
        $x2 = $x2 + 100;
    return hexdec($x1) . hexdec($x2) . hexdec($x3);

But it does not always correctly translate into 10 or 16, because sometimes 3-digit values ​​skip and the final color consists of 7 characters.

Answer 1, authority 100%

Your algorithm is not quite right. The general idea of ​​such transformations lies in the fact that you need to work with color in the color space HSL– color is represented by Hue, Saturation and Light. Here, by increasing or decreasing the Lightness parameter, you can manipulate the brightness of the color. Therefore, it is necessary to convert the color to HSL, make it brighter, and then back to RGB.

There is a lot of code here with similar manipulations one way or the other.

There is also such a wonderful style sheet language as SASS/SCSS. It has built-in lightening/darkening/color conversion functions. The original implementation of SASS/SCSS is in Ruby, but there is also a PHP port – PHamlP. You can find the implementation of this function in the sources – see SassScriptFunctions.phpand SassColour.php. The color in this case can be set in any form of CSS – "#bc8f8f", "#ba2", "rosybrown", "rgb(100%,0%,0%)"