class Dato::Local::FieldType::Color

Attributes

blue[R]
green[R]
red[R]

Public Class Methods

new(red, green, blue, alpha) click to toggle source
# File lib/dato/local/field_type/color.rb, line 18
def initialize(red, green, blue, alpha)
  @red = red
  @green = green
  @blue = blue
  @alpha = alpha
end
parse(value, _repo) click to toggle source
# File lib/dato/local/field_type/color.rb, line 9
def self.parse(value, _repo)
  value && new(
    value[:red],
    value[:green],
    value[:blue],
    value[:alpha],
  )
end

Public Instance Methods

alpha() click to toggle source
# File lib/dato/local/field_type/color.rb, line 33
def alpha
  @alpha / 255.0
end
hex() click to toggle source
# File lib/dato/local/field_type/color.rb, line 37
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
rgb() click to toggle source
# File lib/dato/local/field_type/color.rb, line 25
def rgb
  if @alpha == 255
    "rgb(#{red}, #{green}, #{blue})"
  else
    "rgba(#{red}, #{green}, #{blue}, #{alpha})"
  end
end
to_hash(*_args) click to toggle source
# File lib/dato/local/field_type/color.rb, line 55
def to_hash(*_args)
  {
    red: red,
    green: green,
    blue: blue,
    rgb: rgb,
    hex: hex,
  }
end