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



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

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.



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

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