Class: ColorDiff::Color::Lab

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

Constant Summary collapse

PRECISION =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(l = 0, a = 0, b = 0) ⇒ Lab

Returns a new instance of Lab.



8
9
10
11
12
# File 'lib/color_diff/color/lab.rb', line 8

def initialize(l = 0, a = 0, b = 0)
  @l = l
  @a = a
  @b = b
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



4
5
6
# File 'lib/color_diff/color/lab.rb', line 4

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



4
5
6
# File 'lib/color_diff/color/lab.rb', line 4

def b
  @b
end

#lObject (readonly)

Returns the value of attribute l.



4
5
6
# File 'lib/color_diff/color/lab.rb', line 4

def l
  @l
end

Class Method Details

.from_xyz(xyz) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/color_diff/color/lab.rb', line 14

def self.from_xyz(xyz)
  ref_x = 95.047
  ref_y = 100.000
  ref_z = 108.883
  x = xyz.x / ref_x
  y = xyz.y / ref_y
  z = xyz.z / ref_z

  x = normalize_xyz_component(x)
  y = normalize_xyz_component(y)
  z = normalize_xyz_component(z)

  l = (116 * y) - 16
  a = 500 * (x - y)
  b = 200 * (y - z)

  new(l, a, b)
end

.normalize_xyz_component(c) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/color_diff/color/lab.rb', line 33

def self.normalize_xyz_component(c)
  if c > 0.008856
    c ** (1 / 3.0)
  else
    (c * 7.787) + (16 / 116.0)
  end
end

Instance Method Details

#to_sObject



41
42
43
# File 'lib/color_diff/color/lab.rb', line 41

def to_s
  "L#{@l.round(PRECISION)}A#{@a.round(PRECISION)}B#{@b.round(PRECISION)}"
end