Module: ColorDifference

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

Defined Under Namespace

Classes: Color

Constant Summary collapse

CIRCLE =
360
HALF_CIRCLE =
180
VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.cie2000(color1, color2) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/color_difference.rb', line 7

def self.cie2000(color1, color2)
  color1 = ColorDifference::Color.new(color1[:r], color1[:g], color1[:b])
  color2 = ColorDifference::Color.new(color2[:r], color2[:g], color2[:b])

  color_avg = color_avg(color1, color2)
  g = 0.5 * (1 - convert(color_avg))

  a1 = (1.0 + g) * color1.a
  a2 = (1.0 + g) * color2.a
  c1 = Math.sqrt((a1**2) + (color1.b**2))
  c2 = Math.sqrt((a2**2) + (color2.b**2))
  c_avg = (c1 + c2) / 2.0

  hue1 = hue(color1, a1)
  hue2 = hue(color2, a2)
  hue_avg = hue_avg(hue1, hue2)
  hue_delta = hue_delta(hue1, hue2)

  t = 1 - 0.17 * Math.cos(radians(hue_avg - 30)) +
          0.24 * Math.cos(radians(2 * hue_avg)) +
          0.32 * Math.cos(radians(3 * hue_avg + 6)) -
          0.2 * Math.cos(radians(4 * hue_avg - 63))

  delta_l = color2.l - color1.l
  delta_c = c2 - c1
  hue_delta = 2 * Math.sqrt(c2 * c1) * Math.sin(radians(hue_delta) / 2.0)

  l_avg = (color1.l + color2.l) / 2.0
  s_l = 1 + ((0.015 * ((l_avg - 50)**2)) / Math.sqrt(20 + ((l_avg - 50)**2.0)))
  s_c = 1 + 0.045 * c_avg
  s_h = 1 + 0.015 * c_avg * t

  delta_ro = 30 * Math.exp(-((((hue_avg - 275) / 25)**2.0)))
  r_t = -2 * convert(c_avg) * Math.sin(2 * radians(delta_ro))

  difference = Math.sqrt(((delta_l / (s_l))**2) +
                         ((delta_c / (s_c))**2) +
                         ((hue_delta / (s_h))**2) +
                         r_t * (delta_c / (s_c)) * (hue_delta / (s_h)))

  scale(difference)
end