Class: Abachrome::Converters::SrgbToLrgb

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

Class Method Summary collapse

Class Method Details

.convert(srgb_color) ⇒ Abachrome::Color

Converts a color from sRGB color space to linear RGB color space. This method performs gamma correction by linearizing each sRGB coordinate.

with the same alpha value as the input color

Parameters:

Returns:

  • (Abachrome::Color)

    A new color object in the linear RGB (LRGB) color space



26
27
28
29
30
31
32
33
34
# File 'lib/abachrome/converters/srgb_to_lrgb.rb', line 26

def self.convert(srgb_color)
  r, g, b = srgb_color.coordinates.map { |c| to_linear(AbcDecimal(c)) }

  Color.new(
    ColorSpace.find(:lrgb),
    [r, g, b],
    srgb_color.alpha
  )
end

.to_linear(v) ⇒ AbcDecimal

Converts a sRGB component to its linear RGB equivalent. This conversion applies the appropriate gamma correction to transform an sRGB value into a linear RGB value.

Parameters:

  • v (AbcDecimal, Numeric)

    The sRGB component value to convert (typically in range 0-1)

Returns:

  • (AbcDecimal)

    The corresponding linear RGB component value



42
43
44
45
46
47
48
49
50
# File 'lib/abachrome/converters/srgb_to_lrgb.rb', line 42

def self.to_linear(v)
  v_abs = v.abs
  v_sign = v.negative? ? -1 : 1
  if v_abs <= AD("0.04045")
    v / AD("12.92")
  else
    v_sign * (((v_abs + AD("0.055")) / AD("1.055"))**AD("2.4"))
  end
end