Sha256: 51771dac7d1b01a2ca7195c282c3f3eeb2103af73c7139c57be6724c86d25a02

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module Inker
  class Color
    # This module implements the methods which can be used to serialize
    # a color as string in different formats.
    module Serializers

      # Convert color to a HEX color string.
      #
      # @param force_alpha [Boolean] indicates if alpha channel should be included in HEX color string
      #        when alpha component wasn't specified
      # @return [String] a HEX color string
      def hex(force_alpha: false)
        result = hex6
        result += (alpha * 255).to_i.to_s(16).rjust(2, "0") if alpha < 1 or force_alpha

        return result
      end


      # Convert color to a HEX color string without alpha channel.
      #
      # @return [String] a HEX color string without alpha channel
      def hex6
        result = "#"
        result += red.to_s(16).rjust(2, "0")
        result += green.to_s(16).rjust(2, "0")
        result += blue.to_s(16).rjust(2, "0")

        return result
      end


      # Convert color to RGB color string.
      #
      # @return [String] a RGB color string
      def rgb
        return "rgb(#{red}, #{green}, #{blue})"
      end


      # Convert color to RGBA color string.
      #
      # @param alpha_precision [Integer] indicates the precision of alpha value
      #
      # @return [String] a RGBA color string
      def rgba(alpha_precision: 2)
        return "rgba(#{red}, #{green}, #{blue}, #{alpha.round(alpha_precision)})"
      end


      # Convert color to HSL color string.
      #
      # @return [String] a HSL color string
      def hsl
        return "hsl(#{hue}, #{(saturation * 100).round}%, #{(lightness * 100).round}%)"
      end


      # Convert color to HSL color string.
      #
      # @param alpha_precision [Integer] indicates the precision of alpha value
      #
      # @return [String] a HSL color string
      def hsla(alpha_precision: 2)
        return "hsl(#{hue}, #{(saturation * 100).round}%, #{(lightness * 100).round}%, #{alpha.round(alpha_precision)})"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inker-0.1.0 lib/inker/color/serializers.rb