Sha256: acf4cba51709968899433c788a6c3e8fa6b00a6e7dc25fe9e1a6663c24d80615

Contents?: true

Size: 605 Bytes

Versions: 1

Compression:

Stored size: 605 Bytes

Contents

# frozen_string_literal: true

module TNS
  module Color
    # Hex color representation
    class Hex < Base
      attr_reader :red, :green, :blue

      def initialize(rgb)
        @red, @green, @blue = [rgb.red, rgb.green, rgb.blue].map { |color| normalize(color) }

        super()
      end

      def to_css
        "##{self}"
      end

      def to_s
        "#{red}#{green}#{blue}"
      end

      private

      # Set color value between allowed range and set to hex representation.
      def normalize(value)
        value.round.clamp(0, 255).to_s(16).rjust(2, "0")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tints-n-shades-0.1.0 lib/tns/color/hex.rb