Created
September 16, 2013 16:13
-
-
Save ellenmva/6582830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function adjustBrightness($hex, $steps) { | |
// Steps should be between -255 and 255. Negative = darker, positive = lighter | |
$steps = max(-255, min(255, $steps)); | |
// Format the hex color string | |
$hex = str_replace('#', '', $hex); | |
if (strlen($hex) == 3) { | |
$hex = str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2); | |
} | |
// Get decimal values | |
$r = hexdec(substr($hex,0,2)); | |
$g = hexdec(substr($hex,2,2)); | |
$b = hexdec(substr($hex,4,2)); | |
// Adjust number of steps and keep it inside 0 to 255 | |
$r = max(0,min(255,$r + $steps)); | |
$g = max(0,min(255,$g + $steps)); | |
$b = max(0,min(255,$b + $steps)); | |
$r_hex = str_pad(dechex($r), 2, '0', STR_PAD_LEFT); | |
$g_hex = str_pad(dechex($g), 2, '0', STR_PAD_LEFT); | |
$b_hex = str_pad(dechex($b), 2, '0', STR_PAD_LEFT); | |
return '#'.$r_hex.$g_hex.$b_hex; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment