Sha256: 515b0e44882a038c3d9dc0262a48842e7b2d0cc004d252e2adb6ad7cb5c945ae

Contents?: true

Size: 845 Bytes

Versions: 1

Compression:

Stored size: 845 Bytes

Contents

require "hex_to_rgba/version"

module HexToRgba
  class Converter
    def self.convert color, opacity
      if color.to_s.include? '#'
        hexadecimal_color = color.delete '#'
      else
        hexadecimal_color = color
      end

      if (/([a-fA-F]|[0-9]){3,6}/ =~ color).nil?
        'Wrong color format'
      else
        if hexadecimal_color.length == 3
          red = (hexadecimal_color[0]*2).to_i 16
          green = (hexadecimal_color[1]*2).to_i 16
          blue = (hexadecimal_color[2]*2).to_i 16
        elsif hexadecimal_color.length == 6
          red = hexadecimal_color[0..1].to_i 16
          green = hexadecimal_color[3..4].to_i 16
          blue = hexadecimal_color[5..6].to_i 16
        else
          'Wrong color format'
        end
        "rgba(#{red}, #{green}, #{blue}, #{opacity})"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hex_to_rgba-0.0.2 lib/hex_to_rgba.rb