2

Below is a concept for a brightness/saturation alterarion programm with brightness() and saturation().

function brightness($colorstr, $steps) { 
    ...
    return sprintf("%02x%02x%02x", $r, $g, $b);
} 
function saturation(){ 
    ...
    return sprintf("%02x%02x%02x", $r, $g, $b);
}

Are there any existing simple to use fashions online to complement this ane make the following possible:

$color2 = saturation($color,-10); /* less staurated*/
$color3 = saturation($color,+10); /* more staurated*/
hakre
  • 193,403
  • 52
  • 435
  • 836
Sam
  • 15,254
  • 25
  • 90
  • 145

3 Answers3

2

Saturation and brightness cannot be handled the same (one could argue that your aren't handling brightness correctly using this code but it's probably close enough). See this question RGB to HSV in PHP for how to convert the color to an HSV value then you can modify the saturation (the S value). Then convert back using the answer to this question PHP HSV to RGB.

Community
  • 1
  • 1
James Keesey
  • 1,218
  • 9
  • 13
  • Indeed +1 for perfection but for now Im really fine something easy, simple and rough. By the way, I find the incorrect values PHP giving me from this indeed incorrect program, fascinatingly surprising and somehow I experience them as correct. But thats personal, my friend:) – Sam Mar 24 '11 at 23:46
1

You can do this easily using the phpColors library:

Once included in your project you can mess with the saturation like this:

use Mexitek\PHPColors\Color;

// Convert my HEX
$myBlue = Color::hexToHsl("#336699");

// Get crazy with the saturation
$myBlue["S"] = 0.2;

// Gimme my new color!!
echo Color::hslToHex($myBlue);
rtpHarry
  • 13,019
  • 4
  • 43
  • 64
1

I can't answer this with code but I this wikipedia article about hue and chroma describes the theory very well.

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108