Module: ColorsHelper

Defined in:
app/helpers/colors_helper.rb

Constant Summary collapse

HEX_COLOR_PATTERN =
/\A\#(?:[0-9A-Fa-f]{3}){1,2}\Z/

Instance Method Summary collapse

Instance Method Details

#hex_color_to_rgb_array(hex_color) ⇒ Object



6
7
8
9
10
11
12
# File 'app/helpers/colors_helper.rb', line 6

def hex_color_to_rgb_array(hex_color)
  unless hex_color.is_a?(String) && HEX_COLOR_PATTERN.match?(hex_color)
    raise ArgumentError, "invalid hex color `#{hex_color}`"
  end

  hex_color.length == 7 ? hex_color[1, 7].scan(/.{2}/).map(&:hex) : hex_color[1, 4].scan(/./).map { |v| (v * 2).hex }
end

#rgb_array_to_hex_color(rgb_array) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
# File 'app/helpers/colors_helper.rb', line 14

def rgb_array_to_hex_color(rgb_array)
  raise ArgumentError, "invalid RGB array `#{rgb_array}`" unless rgb_array_valid?(rgb_array)

  "##{rgb_array.map{ "%02x" % _1 }.join}"
end