Class: Chroma::RgbGenerator::FromString

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Bounders

#bound01, #bound_alpha, #clamp01, #to_percentage

Constructor Details

#initialize(format, input) ⇒ FromString

Returns a new instance of FromString

Parameters:

  • format (Symbol)

    unused

  • input (String)

    input to parse


38
39
40
# File 'lib/chroma/rgb_generator/from_string.rb', line 38

def initialize(format, input)
  @input = normalize_input(input)
end

Class Method Details

.matchersHash<Symbol, Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the regex matchers and rgb generation classes for various string color formats.

Returns:

  • (Hash<Symbol, Hash>)

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chroma/rgb_generator/from_string.rb', line 9

def self.matchers
  @matchers ||= begin
    # TinyColor.js matchers
    css_int = '[-\\+]?\\d+%?'
    css_num = '[-\\+]?\\d*\\.\\d+%?'
    css_unit = "(?:#{css_num})|(?:#{css_int})"
    permissive_prefix = '[\\s|\\(]+('
    permissive_delim = ')[,|\\s]+('
    permissive_suffix = ')\\s*\\)?'
    permissive_match3 = "#{permissive_prefix}#{[css_unit] * 3 * permissive_delim}#{permissive_suffix}"
    permissive_match4 = "#{permissive_prefix}#{[css_unit] * 4 * permissive_delim}#{permissive_suffix}"
    hex_match = '[0-9a-fA-F]'

    {
      rgb:  { regex: /rgb#{permissive_match3}/,        class_name: :FromRgbValues },
      rgba: { regex: /rgba#{permissive_match4}/,       class_name: :FromRgbValues },
      hsl:  { regex: /hsl#{permissive_match3}/,        class_name: :FromHslValues },
      hsla: { regex: /hsla#{permissive_match4}/,       class_name: :FromHslValues },
      hsv:  { regex: /hsv#{permissive_match3}/,        class_name: :FromHsvValues },
      hsva: { regex: /hsva#{permissive_match4}/,       class_name: :FromHsvValues },
      hex3: { regex: /^#?#{"(#{hex_match}{1})" * 3}$/, class_name: :FromHexStringValues, builder: :from_hex3 },
      hex6: { regex: /^#?#{"(#{hex_match}{2})" * 3}$/, class_name: :FromHexStringValues, builder: :from_hex6 },
      hex8: { regex: /^#?#{"(#{hex_match}{2})" * 4}$/, class_name: :FromHexStringValues, builder: :from_hex8 }
    }.freeze
  end
end

Instance Method Details

#generateColorModes::Rgb

Generates a ColorModes::Rgb.

Returns:


44
45
46
# File 'lib/chroma/rgb_generator/from_string.rb', line 44

def generate
  get_generator.generate
end