Class: Abachrome::Converters::OklabToOklch

Inherits:
Base
  • Object
show all
Defined in:
lib/abachrome/converters/oklab_to_oklch.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 OKLCH color space. The method performs a mathematical transformation from the rectangular coordinates (L, a, b) to cylindrical coordinates (L, C, h), where:

  • L (lightness) remains the same
  • C (chroma) is calculated as the Euclidean distance from the origin in the a-b plane
  • h (hue) is calculated as the angle in the a-b plane

Parameters:

Returns:

  • (Abachrome::Color)

    The equivalent color in OKLCH color space with the same alpha value

Raises:

  • (ArgumentError)

    If the provided color is not in OKLAB color space



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/abachrome/converters/oklab_to_oklch.rb', line 37

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

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

  c = ((a * a) + (b * b)).sqrt
  h = (AbcDecimal.atan2(b, a) * 180) / Math::PI
  h += 360 if h.negative?

  Color.new(
    ColorSpace.find(:oklch),
    [l, c, h],
    oklab_color.alpha
  )
end