Skip to content

Instantly share code, notes, and snippets.

@elmontoya7
Created February 10, 2018 01:42
Show Gist options
  • Save elmontoya7/718d2f0af9a53f291d4fea245051a76e to your computer and use it in GitHub Desktop.
Save elmontoya7/718d2f0af9a53f291d4fea245051a76e to your computer and use it in GitHub Desktop.
Returns a contrasting color (black or white) depending on the background color. This is a functions used in Vue.js.
getContrastFontColor () {
if(this.colorSelected.length == 7) {
let r = parseInt(this.colorSelected.substr(1,2),16);
let g = parseInt(this.colorSelected.substr(3,2),16);
let b = parseInt(this.colorSelected.substr(5,2),16);
r *= 0.299;
g *= 0.587;
b *= 0.114;
if((r + g + b) > 186)
return '#000000';
else
return '#ffffff';
} else {
return '#000000';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment