Sha256: d244025e1b7bb4d256e343578d2d4477d2103e989ece6913435313774bef85bf

Contents?: true

Size: 852 Bytes

Versions: 8

Compression:

Stored size: 852 Bytes

Contents

module Vedeu
  class Translator
    def self.translate(html_colour = '')
      new(html_colour).translate
    end

    def initialize(html_colour = '')
      @html_colour = html_colour
    end

    def translate
      return '' unless valid?

      [16, red, green, blue].inject(:+)
    end

    private

    attr_reader :html_colour

    def red
      (html_colour[1..2].to_i(16) / 51) * 36
    end

    def green
      (html_colour[3..4].to_i(16) / 51) * 6
    end

    def blue
      (html_colour[5..6].to_i(16) / 51) * 1
    end

    def valid?
      present? && valid_type? && valid_format?
    end

    def present?
      return true unless html_colour.nil? || html_colour.empty?

      false
    end

    def valid_type?
      html_colour.is_a?(String)
    end

    def valid_format?
      html_colour =~ /^#([A-Fa-f0-9]{6})$/
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vedeu-0.1.3 lib/vedeu/support/translator.rb
vedeu-0.1.2 lib/vedeu/support/translator.rb
vedeu-0.1.1 lib/vedeu/support/translator.rb
vedeu-0.1.0 lib/vedeu/support/translator.rb
vedeu-0.0.42 lib/vedeu/support/translator.rb
vedeu-0.0.41 lib/vedeu/support/translator.rb
vedeu-0.0.40 lib/vedeu/support/translator.rb
vedeu-0.0.39 lib/vedeu/support/translator.rb