Created
May 14, 2020 12:23
-
-
Save adamdehaven/4b2f8dcca5161ad46e544ecb0a5c5918 to your computer and use it in GitHub Desktop.
Determine the inverse color (white or black) for a given RGB value
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
| function getRgbInverse(red, green, blue) { | |
| const rgb = [parseInt(red), parseInt(green), parseInt(blue)] | |
| let hsp = Math.sqrt( | |
| 0.299 * (rgb[0] * rgb[0]) + | |
| 0.587 * (rgb[1] * rgb[1]) + | |
| 0.114 * (rgb[2] * rgb[2]) | |
| ) | |
| // Determine if text color for background should be light or dark | |
| if (hsp > 127.5) { | |
| return [0, 0, 0] // black | |
| } else { | |
| return [255, 255, 255] // white | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment