Created
July 18, 2010 12:03
-
-
Save charliepark/480358 to your computer and use it in GitHub Desktop.
calculate contrast color in Ruby
This file contains 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 convert_to_brightness_value(background_hex_color) | |
(background_hex_color.scan(/../).map {|color| color.hex}).sum | |
end | |
def contrasting_text_color(background_hex_color) | |
convert_to_brightness_value(background_hex_color) > 382.5 ? '#000' : '#fff' | |
end |
FF FF FF is 255 255 255.
382.5 is (255*3) / 2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how is the 382.5 benchmark decided ? White has brightness value as 0, and for black, it comes as 510, so why not 510/2 = 255 ?