Last active
March 3, 2019 13:43
-
-
Save abrman/0e804792b5aa1a01c9fac39c1f20c322 to your computer and use it in GitHub Desktop.
Simple hex color mixer in PHP
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
<?php | |
function lerp($min, $max, $value) { return $min*(1-$value)+$max*$value; } | |
function mixColors($colorA, $colorB, $factor){ | |
// Assign R, G and B values for each color | |
list($aR, $aG, $aB) = sscanf($colorA, "#%02x%02x%02x"); | |
list($bR, $bG, $bB) = sscanf($colorB, "#%02x%02x%02x"); | |
// put the colors back together in hex color format with the mixed value | |
return sprintf("#%02x%02x%02x", | |
lerp($aR, $bR, $factor), | |
lerp($aG, $bG, $factor), | |
lerp($aB, $bB, $factor) | |
); | |
} | |
// Usage | |
$colorA = '#f16727'; | |
$colorB = '#fdcc71'; | |
mixColors( $colorA, $colorB, 0 ); // returns $colorA (#f16727) | |
mixColors( $colorA, $colorB, 1 ); // returns $colorB (#fdcc71) | |
mixColors( $colorA, $colorB, 0.5 ); // returns mixed color (#f7994c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment