Maybe someone met ready?

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

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

But I have more trouble transferring their 16 to the 10th system.

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 he does not always correctly translate into 10 or 16, since sometimes 3-digit values ​​skip and the resulting color consists of 7 characters.

  • Check the input parameter and write it up to 6 characters, if necessary. if (strlen ($ color) == 3) $ color = $ color [0]. $ color [0]. $ color [1]. $ color [1]. $ color [2]. $ color [2]; - out

1 answer 1

Your algorithm is not entirely correct . The general idea of ​​such transformations lies in the fact that it is necessary to work with color in the HSL color space - the color is represented by Color tone, Saturation and Lightness . Here by increasing or decreasing the Lightness parameter you can manipulate the brightness of the color. Therefore, it is necessary to translate the color in HSL, make it brighter, and then back to RGB.

Here there is a lot of code with similar manipulations in one direction or another .

There is also such a great style sheet building language like SASS / SCSS . It has built-in functions for lightening / dimming / converting colors. The original implementation of SASS / SCSS is made on Ruby, but there is also a PHP port, PHamlP . In the source you can find the implementation of this function - see SassScriptFunctions.php and SassColour.php . Color in this case can be set in any kind of CSS - "#bc8f8f" , "#ba2" , "rosybrown" , "rgb(100%,0%,0%)"

  • If the question is still relevant :-) - mantigatos