Class: Chroma::RgbGenerator::FromHexStringValues

Inherits:
Base
  • Object
show all
Defined in:
lib/chroma/rgb_generator/from_hex_string_values.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Bounders

#bound01, #bound_alpha, #clamp01, #to_percentage

Constructor Details

#initialize(format, r, g, b, a = 'ff') ⇒ FromHexStringValues

Returns a new instance of FromHexStringValues

Parameters:

  • format (Symbol)

    color format

  • r (String)

    red value

  • g (String)

    green value

  • b (String)

    blue value

  • a (String) (defaults to: 'ff')

    alpha value


9
10
11
12
# File 'lib/chroma/rgb_generator/from_hex_string_values.rb', line 9

def initialize(format, r, g, b, a = 'ff')
  @format = format || :hex
  @r, @g, @b, @a = r, g, b, a
end

Class Method Details

.from_hex3(format, r, g, b) ⇒ ColorModes::Rgb

Generates a ColorModes::Rgb from 3-character hexadecimal.

Parameters:

  • format (Symbol)

    color format

  • r (String)

    red value

  • g (String)

    green value

  • b (String)

    blue value

Returns:


30
31
32
# File 'lib/chroma/rgb_generator/from_hex_string_values.rb', line 30

def from_hex3(format, r, g, b)
  new(format || :hex3, r * 2, g * 2, b * 2)
end

.from_hex6(format, r, g, b) ⇒ ColorModes::Rgb

Generates a ColorModes::Rgb from 6-character hexadecimal.

Parameters:

  • format (Symbol)

    color format

  • r (String)

    red value

  • g (String)

    green value

  • b (String)

    blue value

Returns:


41
42
43
# File 'lib/chroma/rgb_generator/from_hex_string_values.rb', line 41

def from_hex6(format, r, g, b)
  new(format, r, g, b)
end

.from_hex8(format, a, r, g, b) ⇒ ColorModes::Rgb

Generates a ColorModes::Rgb from 8-character hexadecimal.

Parameters:

  • format (Symbol)

    color format

  • r (String)

    red value

  • g (String)

    green value

  • b (String)

    blue value

  • a (String)

    alpha value

Returns:


53
54
55
# File 'lib/chroma/rgb_generator/from_hex_string_values.rb', line 53

def from_hex8(format, a, r, g, b)
  new(format || :hex8, r, g, b, a)
end

Instance Method Details

#generateColorModes::Rgb

Generates a ColorModes::Rgb.

Returns:


16
17
18
19
20
# File 'lib/chroma/rgb_generator/from_hex_string_values.rb', line 16

def generate
  r, g, b = [@r, @g, @b].map { |n| n.to_i(16) }
  a = @a.to_i(16) / 255.0
  [ColorModes::Rgb.new(r, g, b, a), @format]
end