-
-
Save toddsby/99cbb8e8a6c395f51a97ee0393267dd7 to your computer and use it in GitHub Desktop.
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
// Brightness math based on: | |
// http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx | |
@function sqrt ($r) { | |
$x0: 1; | |
$x1: $x0; | |
@for $i from 1 through 10 { | |
$x1: $x0 - ($x0 * $x0 - abs($r)) / (2 * $x0); | |
$x0: $x1; | |
} | |
@return $x1; | |
} | |
$red-magic-number: 241; | |
$green-magic-number: 691; | |
$blue-magic-number: 68; | |
$brightness-divisor: $red-magic-number + $green-magic-number + $blue-magic-number; | |
@function brightness($color) { | |
// Extract color components | |
$red-component: red($color); | |
$green-component: green($color); | |
$blue-component: blue($color); | |
// Calculate a brightness value in 3d color space between 0 and 255 | |
$number: sqrt((($red-component * $red-component * $red-magic-number) + ($green-component * $green-component * $green-magic-number) + ($blue-component * $blue-component * $blue-magic-number)) / $brightness-divisor); | |
// Convert to percentage and return | |
@return 100% * $number / 255; | |
} | |
@function contrasting-color($color, $light, $dark) { | |
@if brightness($color) < 65% { | |
@return $light; | |
} @else { | |
@return $dark; | |
} | |
} |
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
@import 'brightness'; | |
$dark-background-color: #333; | |
$light-text-color: white; | |
$light-background-color: #eee; | |
$dark-text-color: black; | |
.dark-background { | |
background: $dark-background-color; | |
color: contrasting-color($dark-background-color, $light-text-color, $dark-text-color); | |
} | |
.light-background { | |
background: $light-background-color; | |
color: contrasting-color($light-background-color, $light-text-color, $dark-text-color); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment