Skip to content

Instantly share code, notes, and snippets.

@torkiljohnsen
Created December 15, 2014 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torkiljohnsen/1ac5cfba20555725b4f4 to your computer and use it in GitHub Desktop.
Save torkiljohnsen/1ac5cfba20555725b4f4 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.4.7)
// Compass (v1.0.1)
// ----
// sRGB GAMMA CORRECTION, per spec: https://en.wikipedia.org/wiki/SRGB
@function re-gamma($n) { @if $n <= 0.0031308 { @return $n * 12.92; } @else { @return 1.055 * pow($n,1/2.4) - 0.055; } }
@function de-gamma($n) { @if $n <= 0.04045 { @return $n / 12.92; } @else { @return pow((($n + 0.055)/1.055),2.4); } }
// sRGB BT-709 BRIGHTNESS
@function luma-ln($c) {
$rlin: de-gamma(red($c)/255);
$glin: de-gamma(green($c)/255);
$blin: de-gamma(blue($c)/255);
@return re-gamma(0.2126 * $rlin + 0.7152 * $glin + 0.0722 * $blin) * 100;
}
@function luminance($color: #000) {
// Formula:
// http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
$rgb: red($color) green($color) blue($color);
// Speed up and increase compatability with precompiled rgb values
$l-precompiled: 0 0.0003 0.00061 0.00091 0.00121 0.00152 0.00182 0.00212 0.00243 0.00273 0.00303 0.00335 0.00368 0.00402 0.00439 0.00478 0.00518 0.00561 0.00605 0.00651 0.007 0.0075 0.00802 0.00857 0.00913 0.00972 0.01033 0.01096 0.01161 0.01229 0.01298 0.0137 0.01444 0.01521 0.016 0.01681 0.01764 0.0185 0.01938 0.02029 0.02122 0.02217 0.02315 0.02416 0.02519 0.02624 0.02732 0.02843 0.02956 0.03071 0.0319 0.0331 0.03434 0.0356 0.03689 0.0382 0.03955 0.04092 0.04231 0.04374 0.04519 0.04667 0.04817 0.04971 0.05127 0.05286 0.05448 0.05613 0.05781 0.05951 0.06125 0.06301 0.0648 0.06663 0.06848 0.07036 0.07227 0.07421 0.07619 0.07819 0.08022 0.08228 0.08438 0.0865 0.08866 0.09084 0.09306 0.09531 0.09759 0.0999 0.10224 0.10462 0.10702 0.10946 0.11193 0.11444 0.11697 0.11954 0.12214 0.12477 0.12744 0.13014 0.13287 0.13563 0.13843 0.14126 0.14413 0.14703 0.14996 0.15293 0.15593 0.15896 0.16203 0.16513 0.16827 0.17144 0.17465 0.17789 0.18116 0.18447 0.18782 0.1912 0.19462 0.19807 0.20156 0.20508 0.20864 0.21223 0.21586 0.21953 0.22323 0.22697 0.23074 0.23455 0.2384 0.24228 0.2462 0.25016 0.25415 0.25818 0.26225 0.26636 0.2705 0.27468 0.27889 0.28315 0.28744 0.29177 0.29614 0.30054 0.30499 0.30947 0.31399 0.31855 0.32314 0.32778 0.33245 0.33716 0.34191 0.3467 0.35153 0.3564 0.36131 0.36625 0.37124 0.37626 0.38133 0.38643 0.39157 0.39676 0.40198 0.40724 0.41254 0.41789 0.42327 0.42869 0.43415 0.43966 0.4452 0.45079 0.45641 0.46208 0.46778 0.47353 0.47932 0.48515 0.49102 0.49693 0.50289 0.50888 0.51492 0.521 0.52712 0.53328 0.53948 0.54572 0.55201 0.55834 0.56471 0.57112 0.57758 0.58408 0.59062 0.5972 0.60383 0.6105 0.61721 0.62396 0.63076 0.6376 0.64448 0.65141 0.65837 0.66539 0.67244 0.67954 0.68669 0.69387 0.7011 0.70838 0.71569 0.72306 0.73046 0.73791 0.7454 0.75294 0.76052 0.76815 0.77582 0.78354 0.7913 0.7991 0.80695 0.81485 0.82279 0.83077 0.8388 0.84687 0.85499 0.86316 0.87137 0.87962 0.88792 0.89627 0.90466 0.9131 0.92158 0.93011 0.93869 0.94731 0.95597 0.96469 0.97345 0.98225 0.9911 1;
$adjusted-rgb: ();
@each $value in $rgb {
$adjusted-rgb:
append(
$adjusted-rgb,
nth($l-precompiled, $value + 1)
);
} $rgb: $adjusted-rgb;
$l: 0.2126 * nth($rgb,1) + 0.7152 * nth($rgb,2) + 0.0722 * nth($rgb,3);
@return $l * 100;
}
.test {
out: luminance(hsl(0,0,50));
out: luminance(rgb(128,128,128));
out: luma-ln(hsl(0,0,50));
out: luma-ln(rgb(128,128,128));
}
.test {
out: 21.586;
out: 21.586;
out: 50.19608;
out: 50.19608;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment