Method: String#validate_color

Defined in:
lib/na/colors.rb

#validate_colorString

Extract the longest valid %color name from a string.

Allows %colors to bleed into other text and still be recognized, e.g. %greensomething still finds %green.

Returns:

  • (String)

    a valid color name



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/na/colors.rb', line 112

def validate_color
  valid_color = nil
  compiled = ''
  normalize_color.chars.each do |char|
    compiled += char
    if Color.attributes.include?(compiled.to_sym) || compiled =~ /^([fb]g?)?#([a-f0-9]{6})$/i
      valid_color = compiled
    end
  end

  valid_color
end