Class: ColorConverters::RgbConverter

Inherits:
BaseConverter show all
Defined in:
lib/color_converters/converters/rgb_converter.rb

Constant Summary

Constants inherited from BaseConverter

BaseConverter::IMPORT_DP, BaseConverter::OUTPUT_DP

Instance Attribute Summary

Attributes inherited from BaseConverter

#original_value, #rgba

Class Method Summary collapse

Methods inherited from BaseConverter

#alpha, #cielab, #cielch, #cmyk, factory, #hex, #hsb, #hsl, #hsv, inherited, #initialize, #name, #oklab, #oklch, #rgb, #xyz

Constructor Details

This class inherits a constructor from ColorConverters::BaseConverter

Class Method Details

.boundsObject



11
12
13
# File 'lib/color_converters/converters/rgb_converter.rb', line 11

def self.bounds
  { r: [0.0, 255.0], g: [0.0, 255.0], b: [0.0, 255.0], a: [0.0, 1.0] }
end

.lrgb_to_rgb(lrgb_array) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/color_converters/converters/rgb_converter.rb', line 69

def self.lrgb_to_rgb(lrgb_array)
  rr, gg, bb = lrgb_array

  # Apply sRGB Companding (gamma correction) to convert from Linear RGB to non-linear sRGB.

  # This is defined by the sRGB specification (IEC 61966-2-1).

  # The exponent for the non-linear segment is 1/2.4 (approximately 0.41666...).

  # Assumption that rr, gg, bb are always positive

  r, g, b = [rr, gg, bb].map do
    if _1.to_d <= 0.0031308.to_d
      # Linear portion of the sRGB curve

      _1.to_d * 12.92.to_d
    else
      # Non-linear (gamma-corrected) portion of the sRGB curve

      # The sRGB specification uses an exponent of 1/2.4.

      #

      (1.055.to_d * (_1.to_d**(1.0.to_d / 2.4.to_d))) - 0.055.to_d

      # IMPORTANT NUMERICAL NOTE:

      # On this specific system (and confirmed by Wolfram Alpha for direct calculation),

      # the inverse power function for val**2.4 yields a result that deviates from the value expected by widely-used colour science libraries (like Bruce Lindbloom's).

      #

      # To compensate for this numerical discrepancy and ensure the final CIELAB values match standard online calculators and specifications,

      # an empirically determined exponent of 2.5 has been found to produce the correct linearized sRGB values on this environment.

      #

      # Choose 1/2.4 for strict adherence to the standard's definition (knowing your results may slightly deviate from common calculators),

      # or choose 1/2.5 to ensure your calculated linear RGB values (and thus CIELAB) match authoritative external tools on this system.

      #

      # (1.055 * (_1**(1.0 / 2.5))) - 0.055

    end
  end

  # Scale the 0-1 sRGB value to the 0-255 range for 8-bit colour components.

  r *= 255.0.to_d
  g *= 255.0.to_d
  b *= 255.0.to_d

  # Clamping RGB values to prevent out-of-gamut issues and numerical errors and ensures these values stay within the valid and expected range.

  r = r.clamp(0.0..255.0)
  g = g.clamp(0.0..255.0)
  b = b.clamp(0.0..255.0)

  [r, g, b]
end

.matches?(colour_input) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
# File 'lib/color_converters/converters/rgb_converter.rb', line 5

def self.matches?(colour_input)
  return false unless colour_input.is_a?(Hash)

  colour_input.keys - [:r, :g, :b] == [] || colour_input.keys - [:r, :g, :b, :a] == []
end

.rgb_to_lrgb(rgb_array_frac) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/color_converters/converters/rgb_converter.rb', line 36

def self.rgb_to_lrgb(rgb_array_frac)
  # [0, 1]

  r, g, b = rgb_array_frac

  # Inverse sRGB companding. Linearizes RGB channels with respect to energy.

  # Assumption that r, g, b are always positive

  rr, gg, bb = [r, g, b].map do
    if _1.to_d <= 0.04045.to_d
      _1.to_d / 12.92.to_d
    else
      # sRGB Inverse Companding (Non-linear to Linear RGB)

      # The sRGB specification (IEC 61966-2-1) defines the exponent as 2.4.

      #

      (((_1.to_d + 0.055.to_d) / 1.055.to_d)**2.4.to_d)

      # IMPORTANT NUMERICAL NOTE:

      # On this specific system (and confirmed by Wolfram Alpha for direct calculation),

      # the power function for val**2.4 yields a result that deviates from the value expected by widely-used colour science libraries (like Bruce Lindbloom's).

      #

      # To compensate for this numerical discrepancy and ensure the final CIELAB values match standard online calculators and specifications,

      # an empirically determined exponent of 2.5 has been found to produce the correct linearized sRGB values on this environment.

      #

      # Choose 2.4 for strict adherence to the standard's definition (knowing your results may slightly deviate from common calculators),

      # or choose 2.5 to ensure your calculated linear RGB values (and thus CIELAB) match authoritative external tools on this system.

      #

      # ((_1 + 0.055) / 1.055)**2.5

    end
  end

  # [0, 1]

  [rr, gg, bb]
end