Hex Shifta

<div style="background:#<?php echo hex_shift('c1c2b4',10,'+'); ?>">Shifta</div>

<?php
function hex_shift($hex,$percent = 10,$shift_dir = '-'){
$RGB_values = array();
$new_hex = null;
if(strlen($hex) == 3) if(preg_match('/^([0-9a-f])([0-9a-f])([0-9a-f])/i',$hex)) $hex = preg_replace('/^([0-9a-f])([0-9a-f])([0-9a-f])/i','\1\1\2\2\3\3',$hex);
$RGB_values['R'] = hexdec($hex{0}.$hex{1});
$RGB_values['G'] = hexdec($hex{2}.$hex{3});
$RGB_values['B'] = hexdec($hex{4}.$hex{5});
foreach($RGB_values as $c => $v){
if($shift_dir == '+') $amount = round(((255 - $v) / 100) * $percent) + $v;
else $amount = $v - round(($v / 100) * $percent);
$new_hex .= $current_value = (strlen($decimal_to_hex = dechex($amount)) < 2) ? '0' . $decimal_to_hex : $decimal_to_hex;
}
return $new_hex;
}
?>

0 Comments

You must be logged in to post a comment.