Class: Agave::Local::FieldType::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/agave/local/field_type/color.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue, alpha) ⇒ Color

Returns a new instance of Color.



18
19
20
21
22
23
# File 'lib/agave/local/field_type/color.rb', line 18

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

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



7
8
9
# File 'lib/agave/local/field_type/color.rb', line 7

def alpha
  @alpha
end

#blueObject (readonly)

Returns the value of attribute blue.



7
8
9
# File 'lib/agave/local/field_type/color.rb', line 7

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



7
8
9
# File 'lib/agave/local/field_type/color.rb', line 7

def green
  @green
end

#redObject (readonly)

Returns the value of attribute red.



7
8
9
# File 'lib/agave/local/field_type/color.rb', line 7

def red
  @red
end

Class Method Details

.parse(value, _repo) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/agave/local/field_type/color.rb', line 9

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

Instance Method Details

#hexObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/agave/local/field_type/color.rb', line 33

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

#rgbObject



25
26
27
28
29
30
31
# File 'lib/agave/local/field_type/color.rb', line 25

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

#to_hash(*_args) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/agave/local/field_type/color.rb', line 51

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