Skip to content

Instantly share code, notes, and snippets.

@vancouverwill
Created September 10, 2013 23:28
Show Gist options
  • Save vancouverwill/6517187 to your computer and use it in GitHub Desktop.
Save vancouverwill/6517187 to your computer and use it in GitHub Desktop.
blend two colours in PHP
/**
*
* $color1 - hexdecimal color 1 without the hash at the beginning e.g. ffffff
* $color2 - hexdecimal color 1 without the hash at the beginning e.g. 000000
* $ratio - ration between the two colors as a fraction of 1 e.g. for 65% use 0.65
*
**/
function mix_colors_blend($color1, $color2, $ratio = 0.5) {
$color1_red = hexdec(substr($color1, 0, 2));
$color1_green = hexdec(substr($color1, 2, 2));
$color1_blue = hexdec(substr($color1, 4, 2));
$color2_red = hexdec(substr($color2, 0, 2));
$color2_green = hexdec(substr($color2, 2, 2));
$color2_blue = hexdec(substr($color2, 4, 2));
$color1_ratio = $ratio;
$color2_ratio = 1 - $ratio;
$newcolor_red = (($color1_red * $color1_ratio + $color2_red * $color2_ratio));
$newcolor_green = (($color1_green * $color1_ratio + $color2_green * $color2_ratio));
$newcolor_blue = (($color1_blue * $color1_ratio + $color2_blue * $color2_ratio));
return dechex($newcolor_red).dechex($newcolor_green).dechex($newcolor_blue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment