Skip to content

Instantly share code, notes, and snippets.

@bjorntheart
Created October 28, 2015 15:36
Show Gist options
  • Save bjorntheart/f65e04f074f98ab01e3e to your computer and use it in GitHub Desktop.
Save bjorntheart/f65e04f074f98ab01e3e to your computer and use it in GitHub Desktop.
Sort colors from darkest to lightest
<?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