Created
November 22, 2013 20:45
-
-
Save ddavison/7606578 to your computer and use it in GitHub Desktop.
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
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