Created
April 30, 2015 04:25
-
-
Save be9/54f128ae32d411637551 to your computer and use it in GitHub Desktop.
Brightness function
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
def bright?(color) | |
if color =~ /^#([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})$/ | |
r = $1.to_i(16) | |
g = $2.to_i(16) | |
b = $3.to_i(16) | |
# Use formula to calculate brightness | |
# http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx | |
brightness = Math.sqrt(r*r*0.241+g*g*0.691+b*b*0.068) / 255 * 100.0 | |
brightness >= 65 | |
else | |
raise ArgumentError, "Invalid color! ('#{color}')" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment