Class: ColorConverters::CielabConverter

Inherits:
BaseConverter show all
Defined in:
lib/color_converters/converters/cielab_converter.rb

Constant Summary

Constants inherited from BaseConverter

BaseConverter::IMPORT_DP, BaseConverter::OUTPUT_DP

Instance Attribute Summary

Attributes inherited from BaseConverter

#original_value, #rgba

Class Method Summary collapse

Methods inherited from BaseConverter

#alpha, #cielab, #cielch, #cmyk, factory, #hex, #hsb, #hsl, #hsv, inherited, #initialize, #name, #oklab, #oklch, #rgb, #xyz

Constructor Details

This class inherits a constructor from ColorConverters::BaseConverter

Class Method Details

.boundsObject



11
12
13
# File 'lib/color_converters/converters/cielab_converter.rb', line 11

def self.bounds
  { l: [0.0, 100.0], a: [-128.0, 127.0], b: [-128.0, 127.0] }
end

.cielab_to_xyz(colour_input) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/color_converters/converters/cielab_converter.rb', line 34

def self.cielab_to_xyz(colour_input)
  l = colour_input[:l].to_d
  a = colour_input[:a].to_d
  b = colour_input[:b].to_d

  yy = (l + 16.0.to_d) / 116.0.to_d
  xx = (a / 500.0.to_d) + yy
  zz = yy - (b / 200.0.to_d)

  e = 216.0.to_d / 24_389.0.to_d

  x, y, z = [xx, yy, zz].map do
    if _1**3.to_d <= e
      (3.0.to_d * (6.0.to_d / 29.0.to_d) * (6.0.to_d / 29.0.to_d) * (_1 - (4.0.to_d / 29.0.to_d)))
    else
      _1**3.to_d
    end
  end

  x *= 95.047.to_d
  y *= 100.0.to_d
  z *= 108.883.to_d

  [x, y, z]
end

.matches?(colour_input) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
# File 'lib/color_converters/converters/cielab_converter.rb', line 5

def self.matches?(colour_input)
  return false unless colour_input.is_a?(Hash)

  colour_input.keys - [:l, :a, :b, :space] == [] && colour_input[:space].to_s == 'cie'
end

.xyz_to_cielab(xyz_array) ⇒ Object



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
# File 'lib/color_converters/converters/cielab_converter.rb', line 60

def self.xyz_to_cielab(xyz_array)
  x, y, z = xyz_array.map(&:to_d)

  # https://www.w3.org/TR/css-color-4/#color-conversion-code
  # # The D50 & D65 standard illuminant white point
  # wp_rel = [0.3457 / 0.3585, 1.0, (1.0 - 0.3457 - 0.3585) / 0.3585]
  wp_rel = [0.3127.to_d / 0.3290.to_d, 1.0.to_d, (1.0.to_d - 0.3127.to_d - 0.3290.to_d) / 0.3290.to_d].map { _1 * 100.0.to_d }

  xr, yr, zr = wp_rel

  # # Calculate the ratio of the XYZ values to the reference white.
  # # http://www.brucelindbloom.com/index.html?Equations.html
  rel = [x / xr, y / yr, z / zr]

  e = 216.0.to_d / 24_389.0.to_d
  k = 841.0.to_d / 108.0.to_d

  # And now transform
  # http:#en.wikipedia.org/wiki/Lab_color_space#Forward_transformation
  # There is a brief explanation there as far as the nature of the calculations,
  # as well as a much nicer looking modeling of the algebra.
  xx, yy, zz = rel.map do
    if _1 > e
      _1**(1.0.to_d / 3.0.to_d)
    else
      (k * _1) + (4.0.to_d / 29.0.to_d)
      # The 4/29 here is for when t = 0 (black). 4/29 * 116 = 16, and 16 -
      # 16 = 0, which is the correct value for L* with black.
      # ((1.0/3)*((29.0/6)**2) * t) + (4.0/29)
    end
  end

  l = ((116.0.to_d * yy) - 16.0.to_d)
  a = (500.0.to_d * (xx - yy))
  b = (200.0.to_d * (yy - zz))

  [l, a, b]
end