Created
June 14, 2016 17:11
-
-
Save yannisabel/aa94fb081fe5b51dbb91818707fdf9c1 to your computer and use it in GitHub Desktop.
SASS function to choose the best contrast color between font and background. Work with hexadecimal, RGB, RGBA, HSL and HSLA.
This file contains 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
/* | |
Description : SASS function to choose the best contrast color between font and background. Work with hexadecimal, RGB, RGBA, HSL and HSLA. | |
Author: Yann Isabel | |
URL: https://github.com/yannisabel | |
Website : http://yannisabel.com | |
*/ | |
// Color Variables | |
$grey: #333; | |
$white: #fff; | |
$blue: #264b68; | |
$red: #ac2222; | |
// Choose the best contrast color on background | |
@function colorOnBackground($color) { | |
@if (lightness( $color ) > 50) { | |
@return $grey; | |
} | |
@else { | |
@return $white; | |
} | |
} | |
// Then apply to the main scss file (for exemple) | |
.blue-element{ | |
background: $blue; | |
color: colorOnBackground($blue); | |
} | |
.red-element{ | |
background: $red; | |
color: colorOnBackground($red); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment