开发者

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


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.


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


You can do this easily using the phpColors library:

  • https://github.com/mexitek/phpColors

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜