Module: LuminosityContrast

Defined in:
lib/luminosity_contrast.rb,
lib/luminosity_contrast/color.rb,
lib/luminosity_contrast/version.rb

Defined Under Namespace

Classes: Color

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.ratio(rgb1, rgb2) ⇒ Object

expects two RGB values as Arrays of three numbers

example: [0, 0, 0], [255.0, 255.0, 255.0]

or hex code strings (3 or 6 characters)

example: '000000', 'fff'

returns the Luminosity Contrast Ratio between 1.0 and 21.0



14
15
16
17
18
# File 'lib/luminosity_contrast.rb', line 14

def ratio(rgb1, rgb2)
  c1, c2 = [rgb1, rgb2].map { |rgb| Color.new(rgb) }
  l1, l2 = [c1, c2].map(&:relative_luminance).sort
  (l2 + 0.05) / (l1 + 0.05)
end

.relative_luminance(r, g, b) ⇒ Object

expects RGB as three numeric values between 0.0 and 255.0 returns relative luminance of a color between 0.0 and 1.0



22
23
24
# File 'lib/luminosity_contrast.rb', line 22

def relative_luminance(r, g, b)
  (0.2126 * f(r)) + (0.7152 * f(g)) + (0.0722 * f(b))
end