Skip to content

Instantly share code, notes, and snippets.

@fchristant
Created November 24, 2017 12:16
Show Gist options
  • Save fchristant/f3fa1f6dc3685d88920400677ed54691 to your computer and use it in GitHub Desktop.
Save fchristant/f3fa1f6dc3685d88920400677ed54691 to your computer and use it in GitHub Desktop.
PHP getcolorluminance
function getcolorluminance($r, $g, $b) {
// type checking
if (!is_numeric($r) || !is_numeric($g) || !is_numeric($b)) return false;
// range checking
if (!($r >= 0 && $r <= 255)) return false;
if (!($g >= 0 && $g <= 255)) return false;
if (!($b >= 0 && $b <= 255)) return false;
// https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
$r_srgb = $r/255;
$g_srgb = $g/255;
$b_srgb = $b/255;
$r_srgb = $r_srgb <= 0.03928 ? ($r_srgb / 12.92) : pow((($r_srgb + 0.055)/1.055),2.4);
$g_srgb = $g_srgb <= 0.03928 ? ($g_srgb / 12.92) : pow((($g_srgb + 0.055)/1.055),2.4);
$b_srgb = $b_srgb <= 0.03928 ? ($b_srgb / 12.92) : pow((($b_srgb + 0.055)/1.055),2.4);
return 0.2126 * $r_srgb + 0.7152 * $g_srgb + 0.0722 * $b_srgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment