Created
August 24, 2018 15:51
-
-
Save kieranstartup/347a72480f5ed41eb3a76fae85a05232 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
| // Credit to https://gist.github.com/apapirovski/2580052 | |
| // Calculate luma (brightness) of a hexadecimal colour | |
| // --------------------------------------------------- | |
| // You can use this to calculate whether two colours | |
| // are suitable to be used as a background and text | |
| function hex2luma(hex) { | |
| return ( | |
| (parseInt(hex.substring(0, 2), 16) * 299) + | |
| (parseInt(hex.substring(2, 4), 16) * 587) + | |
| (parseInt(hex.substring(4, 6), 16) * 114) | |
| ) / 1000; | |
| } | |
| const luminance = hex2luma(hexGoesHere); | |
| if (luminance > 125) { | |
| // Make text black | |
| } else { | |
| //Make text white | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment