Skip to content

Instantly share code, notes, and snippets.

@ddavison
Created November 22, 2013 20:45
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 ddavison/7606578 to your computer and use it in GitHub Desktop.
Save ddavison/7606578 to your computer and use it in GitHub Desktop.
class Color
attr_reader :r, :g, :b
def initialize(r,g,b)
@r = r
@g = g
@b = b
end
def brightness_index
# Brightness index is
# ( 299*R1 + 587*G1 + 114*B1) / 1000
(299*@r + 587*@g + 114*@b) / 1000
end
def brightness_difference(another_color)
# Brightness difference is absolute difference in brightness indices
(brightness_index - another_color.brightness_index).abs
end
def hue_difference(another_color)
# Hue difference is R1 - R2 + G1 - G2 + B1 - B2
((@r - another_color.r).abs + (@g - another_color.g).abs + (@b - another_color.b).abs)
end
def enough_contrast?(another_color)
(brightness_difference(another_color) > 125 && hue_difference(another_color) > 500)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment