Skip to content

Instantly share code, notes, and snippets.

@azinasili
Last active August 15, 2018 15:28
Show Gist options
  • Save azinasili/afefa139e74664f87b14 to your computer and use it in GitHub Desktop.
Save azinasili/afefa139e74664f87b14 to your computer and use it in GitHub Desktop.
Change text color based on background. Color is a darker/lighter hue of background.
// Grab brightness of color
@function brightness($color) {
@return ((red($color)) + (green($color)) + (blue($color))) / 255 * 100%;
}
// Compare bightness and print new text color
@function text-color($color) {
@if $color == null {
@return null;
}
@else {
$color-light: mix(#ffffff, $color, 80%);
$color-dark: mix(#000000, $color, 60%);
$color-brightness: brightness($color);
$light-text-brightness: brightness($color-light);
$dark-text-brightness: brightness($color-dark);
@if abs($color-brightness - $light-text-brightness) > abs($color-brightness - $dark-text-brightness) {
@return $color-light;
}
@else {
@return $color-dark;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment