Class: Dato::Local::FieldType::Color
- Inherits:
-
Object
- Object
- Dato::Local::FieldType::Color
- Defined in:
- lib/dato/local/field_type/color.rb
Instance Attribute Summary collapse
-
#blue ⇒ Object
readonly
Returns the value of attribute blue.
-
#green ⇒ Object
readonly
Returns the value of attribute green.
-
#red ⇒ Object
readonly
Returns the value of attribute red.
Class Method Summary collapse
Instance Method Summary collapse
- #alpha ⇒ Object
- #hex ⇒ Object
-
#initialize(red, green, blue, alpha) ⇒ Color
constructor
A new instance of Color.
- #rgb ⇒ Object
- #to_hash(*_args) ⇒ Object
Constructor Details
#initialize(red, green, blue, alpha) ⇒ Color
Returns a new instance of Color.
18 19 20 21 22 23 |
# 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 |
Instance Attribute Details
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
7 8 9 |
# File 'lib/dato/local/field_type/color.rb', line 7 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
7 8 9 |
# File 'lib/dato/local/field_type/color.rb', line 7 def green @green end |
#red ⇒ Object (readonly)
Returns the value of attribute red.
7 8 9 |
# File 'lib/dato/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/dato/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
#alpha ⇒ Object
33 34 35 |
# File 'lib/dato/local/field_type/color.rb', line 33 def alpha @alpha / 255.0 end |
#hex ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# 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 ⇒ Object
25 26 27 28 29 30 31 |
# 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) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# 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 |