Sha256: e56fdf0ca8bae8dc2cb93479add683288d4a3204c93b30bae9e489fabeab4b91

Contents?: true

Size: 1.26 KB

Versions: 37

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
#
module Dato
  module Local
    module FieldType
      class Color
        attr_reader :red, :green, :blue, :alpha

        def self.parse(value, _repo)
          value && new(
            value[:red],
            value[:green],
            value[:blue],
            value[:alpha]
          )
        end

        def initialize(red, green, blue, alpha)
          @red = red
          @green = green
          @blue = blue
          @alpha = alpha / 255.0
        end

        def rgb
          if alpha == 1.0
            "rgb(#{red}, #{green}, #{blue})"
          else #
            "rgba(#{red}, #{green}, #{blue}, #{alpha})"
          end
        end

        def hex
          r = red.to_s(16)
          g = green.to_s(16)
          b = blue.to_s(16)
          a = (alpha * 255).to_i.to_s(16)

          r = "0#{r}" if r.length == 1
          g = "0#{g}" if g.length == 1
          b = "0#{b}" if b.length == 1
          a = "0#{a}" if a.length == 1

          hex = '#' + r + g + b

          hex += a if a != 'ff'

          hex
        end

        def to_hash(*_args)
          {
            red: red,
            green: green,
            blue: blue,
            rgb: rgb,
            hex: hex
          }
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
dato-0.6.9 lib/dato/local/field_type/color.rb
dato-0.6.8 lib/dato/local/field_type/color.rb
dato-0.6.7 lib/dato/local/field_type/color.rb
dato-0.6.6 lib/dato/local/field_type/color.rb
dato-0.6.5 lib/dato/local/field_type/color.rb
dato-0.6.3 lib/dato/local/field_type/color.rb
dato-0.6.3.beta lib/dato/local/field_type/color.rb
dato-0.6.2 lib/dato/local/field_type/color.rb
dato-0.6.1 lib/dato/local/field_type/color.rb
dato-0.6.0 lib/dato/local/field_type/color.rb
dato-0.5.1 lib/dato/local/field_type/color.rb
dato-0.5.0 lib/dato/local/field_type/color.rb
dato-0.4.3 lib/dato/local/field_type/color.rb
dato-0.4.2 lib/dato/local/field_type/color.rb
dato-0.4.1 lib/dato/local/field_type/color.rb
dato-0.4.0 lib/dato/local/field_type/color.rb
dato-0.3.31 lib/dato/local/field_type/color.rb
dato-0.4.0.pre lib/dato/local/field_type/color.rb
dato-0.3.30 lib/dato/local/field_type/color.rb
dato-0.3.29 lib/dato/local/field_type/color.rb