Created
October 28, 2015 15:36
-
-
Save bjorntheart/f65e04f074f98ab01e3e to your computer and use it in GitHub Desktop.
Sort colors from darkest to lightest
This file contains hidden or 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 | |
/** | |
* Sort colors from dark to light | |
* http://stackoverflow.com/questions/20213421/sorting-color-selections-on-brightness | |
*/ | |
private static function sortColors($colors) { | |
usort( | |
$colors, | |
function ($one, $two) { | |
return self::colorToLum($one) - self::colorToLum($two); | |
} | |
); | |
return $colors; | |
} | |
private static function colorToLum($color) { | |
$red = hexdec(substr($color, 1, 2)); | |
$green = hexdec(substr($color, 3, 2)); | |
$blue = hexdec(substr($color, 5, 2)); | |
return (0.299 * $red + 0.587 * $green + 0.114 * $blue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment