Imagine a valid #RGB color in hexadecimal, which is defined as $color = "#f7b9a0";
Now I want php to get two different colors from this $ color, which are slightly lighter / darker (the same hue / color, but only the changed brightness). What ways could I achieve this? What code will generate this? I have a feeling that I need something simple:
brightness(input rgb color, ± number of steps);
Ideally, I want to have something like this in my html:
.classDefault {color:<?=$color?> }
.classLighter {color:<?=brightness($color,+10)?> }
.classDarker {color:<?=brightness($color,-25)?> }
What PHP code should go to the function brightness();
? for my dream come true?
Any suggestions and / or codes are greatly appreciated!
updated from the answer below:
function alter_brightness($colourstr, $steps) {
$colourstr = str_replace('#','',$colourstr);
$rhex = substr($colourstr,0,2);
$ghex = substr($colourstr,2,2);
$bhex = substr($colourstr,4,2);
$r = hexdec($rhex);
$g = hexdec($ghex);
$b = hexdec($bhex);
$r = max(0,min(255,$r + $steps));
$g = max(0,min(255,$g + $steps));
$b = max(0,min(255,$b + $steps));
return '#'.dechex($r).dechex($g).dechex($b);
}
$color = "#2233FF";
$color1 = brightness($color,25);
$color2 = brightness($color,50);
$color3 = brightness($color,75);
$color4 = brightness($color,-25);
$color5 = brightness($color,-50);
$color6 = brightness($color,-75);
<!-- BRIGHTER -->
<div style=" background-color:<?=$color3?>"><?=$color3?></div>
<div style=" background-color:<?=$color2?>"><?=$color2?></div>
<div style=" background-color:<?=$color1?>"><?=$color1?></div>
<!-- DEFINED CONSTANT -->
<div style=" background-color:<?=$color?>"><?=$color?></div>
<!-- DARKER -->
<div style=" background-color:<?=$color4?>"><?=$color4?></div>
<div style=" background-color:<?=$color5?>"><?=$color5?></div>
<div style=" background-color:<?=$color6?>"><?=$color6?></div>
, . , , - , , , !