Skip to content

Instantly share code, notes, and snippets.

@jayarjo
Created March 24, 2013 17:25
Show Gist options
  • Save jayarjo/5232728 to your computer and use it in GitHub Desktop.
Save jayarjo/5232728 to your computer and use it in GitHub Desktop.
make base color darker by a specified factor
function hex_darker($hex, $factor = 30)
{
$new_hex = '';
$base['R'] = hexdec($hex{0}.$hex{1});
$base['G'] = hexdec($hex{2}.$hex{3});
$base['B'] = hexdec($hex{4}.$hex{5});
foreach ($base as $k => $v)
{
$amount = $v / 100;
$amount = round($amount * $factor);
$new_decimal = $v - $amount;
$new_hex_component = dechex($new_decimal);
if (strlen($new_hex_component) < 2) {
$new_hex_component = "0".$new_hex_component;
}
$new_hex .= $new_hex_component;
}
return $new_hex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment