Skip to content

Instantly share code, notes, and snippets.

@yannisabel
Created June 14, 2016 17:11
Show Gist options
  • Save yannisabel/aa94fb081fe5b51dbb91818707fdf9c1 to your computer and use it in GitHub Desktop.
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.
/*
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