Skip to content

Instantly share code, notes, and snippets.

@lunelson
Last active March 8, 2022 14:15
Show Gist options
  • Save lunelson/4d255ae2bff501647422 to your computer and use it in GitHub Desktop.
Save lunelson/4d255ae2bff501647422 to your computer and use it in GitHub Desktop.
sRGB BT-709 Brightness (Luma) function and HSY() color function for Sass
// 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 brightness($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;
}
// HSY: HSL per BRIGHTNESS
@function hsy($h, $s, $y) {
@if $y == 0 { @return rgb(0,0,0); }
@else if $y == 100 { @return rgb(255,255,255); }
@else if $s == 0 { @return hsl($h, 0, $y); }
@else { $min-l: 0; $max-l: 100; $mid-l: 50;
@while ($max-l - $min-l) > 0.01 {
$mid-y: brightness(hsl($h, $s, $mid-l));
@if $mid-y > $y { $max-l: $mid-l; } @else { $min-l: $mid-l; }
$mid-l: ($min-l + $max-l) / 2;
} @return hsl($h, $s, $mid-l);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment