Module: ColorDecomposition::Comparator

Extended by:
Comparator
Included in:
Comparator, Node
Defined in:
lib/color_decomposition/color/comparator.rb

Instance Method Summary collapse

Instance Method Details

#ciede2000(lab1, lab2) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/color_decomposition/color/comparator.rb', line 5

def ciede2000(lab1, lab2)
  lch1, lch2 = to_lch(lab1, lab2)

  l_bar = (lch1[:l] + lch2[:l]) / 2
  c_bar = (lch1[:c] + lch2[:c]) / 2
  h_bar = mean_hue(lch1, lch2)

  l_delta = lch2[:l] - lch1[:l]
  c_delta = lch2[:c] - lch1[:c]
  h_delta = hue_angle(lch1, lch2)
  o_delta = 30 * Math.exp(-((h_bar - 275) / 25)**2)

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

  rc = 2 * Math.sqrt(c_bar**7 / (c_bar**7 + 25**7))
  sl = 1 + 0.015 * (l_bar - 50)**2 / Math.sqrt(20 + (l_bar - 50)**2)
  sc = 1 + 0.045 * c_bar
  sh = 1 + 0.015 * c_bar * t
  rt = -Math.sin(radians(2 * o_delta)) * rc

  Math.sqrt((l_delta / sl)**2 + (c_delta / sc)**2 + (h_delta / sh)**2 +
            rt * (c_delta / sc) * (h_delta / sh)).clamp(0, 100)
end