Class: Color::Lab

Inherits:
Base
  • Object
show all
Defined in:
lib/quadtone/color/lab.rb

Constant Summary collapse

DeltaEMethods =
[
  :density,
  :cie76,
  :cie94,
  :cmclc
]

Instance Attribute Summary

Attributes inherited from Base

#components

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, average, colorspace_name, #eql?, from_cgats, #hash, #initialize, num_components, #to_cgats, #to_s, #to_str

Methods included from Math

#deg2rad, #rad2deg

Constructor Details

This class inherits a constructor from Color::Base

Class Method Details

.cgats_fieldsObject



16
17
18
# File 'lib/quadtone/color/lab.rb', line 16

def self.cgats_fields
  %w{LAB_L LAB_A LAB_B}
end

.component_namesObject



12
13
14
# File 'lib/quadtone/color/lab.rb', line 12

def self.component_names
  [:l, :a, :b]
end

Instance Method Details

#aObject



24
25
26
# File 'lib/quadtone/color/lab.rb', line 24

def a
  @components[1]
end

#bObject



28
29
30
# File 'lib/quadtone/color/lab.rb', line 28

def b
  @components[2]
end

#chromaObject



36
37
38
39
# File 'lib/quadtone/color/lab.rb', line 36

def chroma
  # http://www.brucelindbloom.com/Eqn_Lab_to_LCH.html
  sqrt((a * a) + (b * b))
end

#delta_e(other, method = :cmclc) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/quadtone/color/lab.rb', line 50

def delta_e(other, method=:cmclc)
  # http://en.wikipedia.org/wiki/Color_difference
  # http://www.brucelindbloom.com/iPhone/ColorDiff.html
  l1, a1, b1 = self.l, self.a, self.b
  l2, a2, b2 = other.l, other.a, other.b
  dl = l2 - l1
  da = a1 - a2
  db = b1 - b2
  if method == :cie94 || method == :cmclc
    c1, c2 = self.chroma, other.chroma
    h1, h2 = self.hue, other.hue
    dc = c1 - c2
    dh2 = da**2 + db**2 - dc**2
    return Float::NAN if dh2 < 0
    dh = sqrt(dh2)
  end
  case method
  when :density
    dl.abs
  when :cie76
    sqrt(dl**2 + da**2 + db**2)
  when :cie94
    kl, k1, k2 = 1, 0.045, 0.015
    sqrt(
      (dl / kl)**2 +
      (dc / (1 + k1*c1))**2 +
      (dh / (1 + k2*c2)**2)
    )
  when :cmclc
    l, c = 2, 1
    sl = (l1 < 16) ?
      0.511 :
      0.040975 * l1 / (1 + 0.01765 * l1)
    sc = 0.0638 * c1 / (1 + 0.0131 * c1) + 0.638
    f = sqrt(
      (c1 ** 4) / ((c1 ** 4) + 1900)
    )
    t = (h1 >= 164 && h1 <= 345) ?
      0.56 + (0.2 * cos(deg2rad(h1 + 168))).abs :
      0.36 + (0.4 * cos(deg2rad(h1 + 35))).abs
    sh = sc * ((f * t) + 1 - f)
    sqrt(
      (dl / (l * sl)) ** 2 +
      (dc / (c * sc)) ** 2 +
      (dh / sh) ** 2
    )
  else
    raise "Unknown deltaE method: #{method.inspect}"
  end
end

#hueObject



41
42
43
44
45
46
47
48
# File 'lib/quadtone/color/lab.rb', line 41

def hue
  # http://www.brucelindbloom.com/Eqn_Lab_to_LCH.html
  if a == 0 && b == 0
    0
  else
    rad2deg(atan2(b, a)) % 360
  end
end

#lObject



20
21
22
# File 'lib/quadtone/color/lab.rb', line 20

def l
  @components[0]
end

#to_rgbObject



124
125
126
# File 'lib/quadtone/color/lab.rb', line 124

def to_rgb
  to_xyz.to_rgb
end

#to_xyzObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/quadtone/color/lab.rb', line 101

def to_xyz
  # after http://www.easyrgb.com/index.php?X=MATH&H=08#text8

  y = (l + 16) / 116.0
  x = (a / 500.0) + y
  z = y - (b / 200.0)

  x, y, z = [x, y, z].map do |n|
    if (n3 = n**3) > 0.008856
      n3
    else
      (n - 16 / 116.0) / 7.787
    end
  end

  ref = Color::XYZ.standard_reference
  x *= ref.x
  y *= ref.y
  z *= ref.z

  Color::XYZ.new([x, y, z])
end

#valueObject



32
33
34
# File 'lib/quadtone/color/lab.rb', line 32

def value
  1 - (l / 100.0)
end