Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save valdemarua/76f371cf471785b098453e2bef55f293 to your computer and use it in GitHub Desktop.
Save valdemarua/76f371cf471785b098453e2bef55f293 to your computer and use it in GitHub Desktop.
calculate contrast color in Ruby
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment