Class: Abachrome::Converters::LrgbToSrgb

Inherits:
Base
  • Object
show all
Defined in:
lib/abachrome/converters/lrgb_to_srgb.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(lrgb_color) ⇒ Abachrome::Color

Converts a color from linear RGB to sRGB color space.

Parameters:

  • lrgb_color (Abachrome::Color)

    The color in linear RGB color space to convert

Returns:

  • (Abachrome::Color)

    A new Color object in sRGB color space with the converted coordinates

Raises:

  • (TypeError)

    If the provided color is not in linear RGB color space



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

def self.convert(lrgb_color)
  raise_unless lrgb_color, :lrgb
  r, g, b = lrgb_color.coordinates.map { |c| to_srgb(AbcDecimal(c)) }

  output_coords = [r, g, b]

  Color.new(
    ColorSpace.find(:srgb),
    output_coords,
    lrgb_color.alpha
  )
end

.to_srgb(v) ⇒ AbcDecimal

Converts a linear RGB value to standard RGB color space (sRGB) value.

This method implements the standard linearization function used in the sRGB color space. For small values (≤ 0.0031308), a simple linear transformation is applied. For larger values, a power function with gamma correction is used.

Parameters:

  • v (AbcDecimal)

    The linear RGB value to convert

Returns:

  • (AbcDecimal)

    The corresponding sRGB value, preserving the sign of the input



44
45
46
47
48
49
50
51
52
# File 'lib/abachrome/converters/lrgb_to_srgb.rb', line 44

def self.to_srgb(v)
  v_abs = v.abs
  v_sign = v.negative? ? -1 : 1
  if v_abs <= AD("0.0031308")
    v * AD("12.92")
  else
    v_sign * ((AD("1.055") * (v_abs**Rational(1.0, 2.4))) - AD("0.055"))
  end
end