Skip to content

Instantly share code, notes, and snippets.

@be9
Created April 30, 2015 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save be9/54f128ae32d411637551 to your computer and use it in GitHub Desktop.
Save be9/54f128ae32d411637551 to your computer and use it in GitHub Desktop.
Brightness function
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