Last active
December 27, 2015 10:49
-
-
Save thanh4890/7313752 to your computer and use it in GitHub Desktop.
Some php color helper functions fork from avia framework by Christian "Kriesi" Budschedl
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
if(!function_exists('avia_backend_get_hex_from_rgb')) | |
{ | |
/** | |
* converts an rgb string into a hex value and returns the string | |
* @param string $r red | |
* @param string $g green | |
* @param string $B blue | |
* @return string returns the converted string | |
*/ | |
function avia_backend_get_hex_from_rgb($r=FALSE, $g=FALSE, $b=FALSE) { | |
$x = 255; | |
$y = 0; | |
$r = (is_int($r) && $r >= $y && $r <= $x) ? $r : 0; | |
$g = (is_int($g) && $g >= $y && $g <= $x) ? $g : 0; | |
$b = (is_int($b) && $b >= $y && $b <= $x) ? $b : 0; | |
return sprintf('#%02X%02X%02X', $r, $g, $b); | |
} | |
} | |
if(!function_exists('avia_backend_calculate_similar_color')) | |
{ | |
/** | |
* calculates a darker or lighter color variation of a color | |
* @param string $color hex color code | |
* @param string $shade darker or lighter | |
* @param int $amount how much darker or lighter | |
* @return string returns the converted string | |
*/ | |
function avia_backend_calculate_similar_color($color, $shade, $amount) | |
{ | |
//remove # from the begiining if available and make sure that it gets appended again at the end if it was found | |
$newcolor = ""; | |
$prepend = ""; | |
if(strpos($color,'#') !== false) | |
{ | |
$prepend = "#"; | |
$color = substr($color, 1, strlen($color)); | |
} | |
//iterate over each character and increment or decrement it based on the passed settings | |
$nr = 0; | |
while (isset($color[$nr])) | |
{ | |
$char = strtolower($color[$nr]); | |
for($i = $amount; $i > 0; $i--) | |
{ | |
if($shade == 'lighter') | |
{ | |
switch($char) | |
{ | |
case 9: $char = 'a'; break; | |
case 'f': $char = 'f'; break; | |
default: $char++; | |
} | |
} | |
else if($shade == 'darker') | |
{ | |
switch($char) | |
{ | |
case 'a': $char = '9'; break; | |
case '0': $char = '0'; break; | |
default: $char = chr(ord($char) - 1 ); | |
} | |
} | |
} | |
$nr ++; | |
$newcolor.= $char; | |
} | |
$newcolor = $prepend.$newcolor; | |
return $newcolor; | |
} | |
} | |
if(!function_exists('avia_backend_hex_to_rgb_array')) | |
{ | |
/** | |
* converts an hex string into an rgb array | |
* @param string $color hex color code | |
* @return array $color | |
*/ | |
function avia_backend_hex_to_rgb_array($color) | |
{ | |
if(strpos($color,'#') !== false) | |
{ | |
$color = substr($color, 1, strlen($color)); | |
} | |
$color = str_split($color, 2); | |
foreach($color as $key => $c) $color[$key] = hexdec($c); | |
return $color; | |
} | |
} | |
if(!function_exists('avia_backend_calc_preceived_brightness')) | |
{ | |
/** | |
* calculates if a color is dark or light, | |
* if a second parameter is passed it will return true or false based on the comparison of the calculated and passed value | |
* @param string $color hex color code | |
* @return array $color | |
* @resource: http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx | |
*/ | |
function avia_backend_calc_preceived_brightness($color, $compare = false) | |
{ | |
$rgba = avia_backend_hex_to_rgb_array($color); | |
$brighntess = sqrt( | |
$rgba[0] * $rgba[0] * 0.241 + | |
$rgba[1] * $rgba[1] * 0.691 + | |
$rgba[2] * $rgba[2] * 0.068); | |
if($compare) | |
{ | |
$brighntess = $brighntess < $compare ? true : false; | |
} | |
return $brighntess; | |
} | |
} | |
if(!function_exists('avia_backend_merge_colors')) | |
{ | |
/** | |
* merges to colors | |
* @param string $color1 hex color code | |
* @param string $color2 hex color code | |
* @return new color | |
*/ | |
function avia_backend_merge_colors($color1, $color2) | |
{ | |
if(empty($color1)) return $color2; | |
if(empty($color2)) return $color1; | |
$prepend = array("", ""); | |
$colors = array(avia_backend_hex_to_rgb_array($color1), avia_backend_hex_to_rgb_array($color2)); | |
$final = array(); | |
foreach($colors[0] as $key => $color) | |
{ | |
$final[$key] = (int) ceil(($colors[0][$key] + $colors[1][$key]) / 2); | |
} | |
return avia_backend_get_hex_from_rgb($final[0], $final[1], $final[2]); | |
} | |
} | |
function avia_backend_counter_color($color) | |
{ | |
$color = avia_backend_hex_to_rgb_array($color); | |
foreach($color as $key => $value) | |
{ | |
$color[$key] = (int) (255 - $value); | |
} | |
return avia_backend_get_hex_from_rgb($color[0], $color[1], $color[2]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment