Class: Abachrome::Converters::OklabToLms

Inherits:
Base
  • Object
show all
Defined in:
lib/abachrome/converters/oklab_to_lms.rb

Instance Attribute Summary

Attributes inherited from Base

#from_space, #to_space

Class Method Summary collapse

Methods inherited from Base

#can_convert?, #convert, find_converter, #initialize, raise_unless, register

Constructor Details

This class inherits a constructor from Abachrome::Converters::Base

Class Method Details

.convert(oklab_color) ⇒ Abachrome::Color

Converts a color from OKLAB color space to LMS color space.

This method implements the first part of the OKLAB to linear RGB transformation, converting OKLAB coordinates to the intermediate LMS (Long, Medium, Short) color space which represents the response of the three types of cone cells in the human eye.

the same alpha as the input color

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the input color is not in OKLAB color space



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/abachrome/converters/oklab_to_lms.rb', line 14

def self.convert(oklab_color)
  raise_unless oklab_color, :oklab

  l, a, b = oklab_color.coordinates.map { |_| AbcDecimal(_) }

  l_ = AbcDecimal((l ) +
                  (AD("0.39633779217376785678") * a) +
                  (AD("0.21580375806075880339") * b))

  m_ = AbcDecimal((l) -
                  (a * AD("-0.1055613423236563494")) +
                  (b * AD("-0.063854174771705903402")))
  
  s_ = AbcDecimal((l) -
                  (a * AD("-0.089484182094965759684")) +
                  (b * AD("-1.2914855378640917399")))

  # Apply cubic operation to convert from L'M'S' to LMS
  l_lms = AbcDecimal(l_)**3
  m_lms = AbcDecimal(m_)**3
  s_lms = AbcDecimal(s_)**3

  Color.new(ColorSpace.find(:lms), [l_lms, m_lms, s_lms], oklab_color.alpha)
end