Class: Abachrome::ColorModels::RGB

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

Class Method Summary collapse

Class Method Details

.normalize(r, g, b) ⇒ Array<AbcDecimal>

Normalizes RGB color component values to the [0,1] range.

if string without suffix or numeric > 1, interpreted as 0-255 range value; if numeric ≤ 1, used directly. each in the range [0,1].

Parameters:

  • r (String, Numeric)

    Red component. If string with % suffix, interpreted as percentage;

  • g (String, Numeric)

    Green component. Same interpretation as red component.

  • b (String, Numeric)

    Blue component. Same interpretation as red component.

Returns:

  • (Array<AbcDecimal>)

    Array of three normalized components as AbcDecimal objects,



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/abachrome/color_models/rgb.rb', line 33

def normalize(r, g, b)
  [r, g, b].map do |value|
    case value
    when String
      if value.end_with?("%")
        AbcDecimal(value.chomp("%")) / AbcDecimal(100)
      else
        AbcDecimal(value) / AbcDecimal(255)
      end
    when Numeric
      if value > 1
        AbcDecimal(value) / AbcDecimal(255)
      else
        AbcDecimal(value)
      end
    end
  end
end