Skip to content

Instantly share code, notes, and snippets.

@floq-design
Created May 7, 2013 13:07
Show Gist options
  • Save floq-design/5532413 to your computer and use it in GitHub Desktop.
Save floq-design/5532413 to your computer and use it in GitHub Desktop.
Mix 2 hexadecimal colours with strength control. Code credit: http://www.syndrotech.com/programming/php-mix-two-hex-colors/
<?php
function mixColors($color1, $color2, $strength = 0.5) {
$strengthOppose = 1 - $strength;
$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));
$newcolor_red = round(($color1_red * $strengthOppose) + ($color2_red * $strength));
$newcolor_green = round(($color1_green * $strengthOppose) + ($color2_green * $strength));
$newcolor_blue = round(($color1_blue * $strengthOppose) + ($color2_blue * $strength));
return str_pad(dechex($newcolor_red), 2, '0', STR_PAD_LEFT).str_pad(dechex($newcolor_green), 2, '0', STR_PAD_LEFT).str_pad(dechex($newcolor_blue), 2, '0', STR_PAD_LEFT);
}
$color1 = "FF0000";
$color2 = "ffffff";
echo( '#' . mixColors($color1, $color2, 0.3) );
?>
@rowdyrotifer
Copy link

I was googling stuff about my old website Syndrotech that I made years ago. How fun to find something like this!

Unfortunately, 15-year-old me got bored of that website I had put so much work into, so I stopped paying for it, and then I accidentally deleted the backups that I had laying around afterwards :( All that I have left are a few things like this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment