Sam
Sam

Reputation: 15506

Altering Saturation of #RGB Color: Whats Missing Here?

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*/

Upvotes: 2

Views: 4090

Answers (3)

rtpHarry
rtpHarry

Reputation: 13125

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);

Upvotes: 1

Nick Weaver
Nick Weaver

Reputation: 47241

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

Upvotes: 1

James Keesey
James Keesey

Reputation: 1217

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.

Upvotes: 2

Related Questions