Sha256: 777032fd0dbf75cc52cc85dc96439dbc68a2039b93a018fb1f82dee660bd376f

Contents?: true

Size: 1003 Bytes

Versions: 4

Compression:

Stored size: 1003 Bytes

Contents

module Chroma
  # Main module to generate an instance of {ColorModes::Rgb} from several
  # possible inputs.
  module RgbGenerator
    class << self
      # Generates an instance of {ColorModes::Rgb} as well as color format
      #   symbol.
      #
      # @param input [String, ColorModes::Rgb, ColorModes::Hsl, ColorModes::Hsv]
      # @return      [[ColorModes::Rgb, Symbol]]
      def generate_rgb_and_format(input)
        get_generator(input).generate.tap do |(rgb)|
          rgb.r = round(rgb.r)
          rgb.g = round(rgb.g)
          rgb.b = round(rgb.b)
        end
      end

      private

      def get_generator(input)
        klass = case input
                when String          then FromString
                when ColorModes::Hsl then FromHsl
                when ColorModes::Hsv then FromHsv
                when ColorModes::Rgb then FromRgb
                end

        klass.new(nil, input)
      end

      def round(n)
        n < 1 ? n.round : n
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chroma-0.2.0 lib/chroma/rgb_generator.rb
chroma-0.1.0 lib/chroma/rgb_generator.rb
chroma-0.0.1 lib/chroma/rgb_generator.rb
chroma-0.0.1.alpha.3 lib/chroma/rgb_generator.rb