class RGBUtils::WebSafeToHexConverter

Constants

DEFAULT_COLOR

Attributes

color[R]

Public Class Methods

new(color) click to toggle source
# File lib/rgb_utils/converters/web_safe_to_hex_converter.rb, line 5
def initialize(color)
  @color = color
end

Public Instance Methods

convert() click to toggle source
# File lib/rgb_utils/converters/web_safe_to_hex_converter.rb, line 9
def convert
  return DEFAULT_COLOR unless valid?

  result = color_elements
  result.map! { |c| c * 2 } if web_safe?

  "##{result.join}"
end

Private Instance Methods

color_elements() click to toggle source
# File lib/rgb_utils/converters/web_safe_to_hex_converter.rb, line 30
def color_elements
  @color_elements ||= color.scan(/\h/)
end
valid?() click to toggle source
# File lib/rgb_utils/converters/web_safe_to_hex_converter.rb, line 22
def valid?
  [3, 6].include?(color_elements.count)
end
web_safe?() click to toggle source
# File lib/rgb_utils/converters/web_safe_to_hex_converter.rb, line 26
def web_safe?
  color_elements.count == 3
end