Class: Abachrome::ColorModels::Oklch

Inherits:
Object
  • Object
show all
Defined in:
lib/abachrome/color_models/oklch.rb

Class Method Summary collapse

Class Method Details

.from_oklab(l, a, b) ⇒ Array<Numeric>

Converts OKLab color coordinates to OKLCH color coordinates.

  • l is the lightness component (unchanged from OKLab)

  • c is the chroma component (calculated from a and b)

  • h is the hue angle in degrees (0-360)

Parameters:

  • l (Numeric)

    The lightness component from OKLab.

  • a (Numeric)

    The green-red component from OKLab.

  • b (Numeric)

    The blue-yellow component from OKLab.

Returns:

  • (Array<Numeric>)

    An array containing the OKLCH values [l, c, h] where:



70
71
72
73
74
75
76
# File 'lib/abachrome/color_models/oklch.rb', line 70

def self.from_oklab(l, a, b)
  # Convert OKLab to OKLCH
  c = Math.sqrt((a * a) + (b * b))
  h = Math.atan2(b, a) * 180 / Math::PI
  h += 360 if h.negative?
  [l, c, h]
end

.normalize(l, c, h) ⇒ Array<AbcDecimal>

Normalizes OKLCH color values to their standard ranges.

Parameters:

  • l (Numeric)

    The lightness component, will be clamped to range 0-1

  • c (Numeric)

    The chroma component, will be clamped to range 0-1

  • h (Numeric)

    The hue component in degrees, will be normalized to range 0-360

Returns:

  • (Array<AbcDecimal>)

    Array containing the normalized [lightness, chroma, hue] values



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/abachrome/color_models/oklch.rb', line 31

def self.normalize(l, c, h)
  l = AbcDecimal(l)
  c = AbcDecimal(c)
  h = AbcDecimal(h)

  # Normalize hue to 0-360 range
  h -= 360 while h >= 360
  h += 360 while h.negative?

  # Normalize lightness and chroma to 0-1 range
  l = l.clamp(0, 1)
  c = c.clamp(0, 1)

  [l, c, h]
end

.to_oklab(l, c, h) ⇒ Array<Numeric>

Converts OKLCH color coordinates to OKLab color coordinates.

Parameters:

  • l (Numeric)

    The lightness value in the OKLCH color space

  • c (Numeric)

    The chroma value in the OKLCH color space

  • h (Numeric)

    The hue value in degrees in the OKLCH color space

Returns:

  • (Array<Numeric>)

    An array containing the OKLab coordinates [l, a, b]



53
54
55
56
57
58
59
# File 'lib/abachrome/color_models/oklch.rb', line 53

def self.to_oklab(l, c, h)
  # Convert OKLCH to OKLab
  h_rad = h * Math::PI / 180
  a = c * Math.cos(h_rad)
  b = c * Math.sin(h_rad)
  [l, a, b]
end