Class: Archimate::DataModel::Color
- Inherits:
-
Object
- Object
- Archimate::DataModel::Color
- Includes:
- Comparison
- Defined in:
- lib/archimate/data_model/color.rb
Overview
RGBColorType in the XSD RGB Color type. The r, g, b attributes range from 0 - 255. The a (alpha) transparency attribute is optional. 0 = full transparency, 100 = opaque.
Instance Attribute Summary collapse
-
#a ⇒ Int, NilClass
readonly
Optional alpha component, value 0-100.
-
#b ⇒ Int
readonly
Blue component, value 0-255.
-
#g ⇒ Int
readonly
Green component, value 0-255.
-
#r ⇒ Int
readonly
Red component, value 0-255.
Class Method Summary collapse
-
.black ⇒ Color
Results in a Color instance for the color black.
-
.rgba(str) ⇒ Color
Parses a CSS style color string into a color object.
Instance Method Summary collapse
-
#initialize(r:, g:, b:, a: nil) ⇒ Color
constructor
A new instance of Color.
- #to_rgba ⇒ Object
- #to_s ⇒ Object
Methods included from Comparison
#==, #[], #dig, #each, #hash, included, #inspect, #pretty_print, #to_h
Constructor Details
#initialize(r:, g:, b:, a: nil) ⇒ Color
Returns a new instance of Color.
46 47 48 49 50 51 52 53 54 |
# File 'lib/archimate/data_model/color.rb', line 46 def initialize(r:, g:, b:, a: nil) raise "r, g, b cannot be nil" if [r, g, b].any?(&:nil?) raise "r, g, b must be between 0 and 255" if [r, g, b].any? { |v| v.negative? || v > 255 } raise "a must be between 0 and 100" if a && (a.negative? || a > 100) @r = r @g = g @b = b @a = a end |
Instance Attribute Details
#a ⇒ Int, NilClass (readonly)
Returns optional alpha component, value 0-100.
23 |
# File 'lib/archimate/data_model/color.rb', line 23 model_attr :a |
#b ⇒ Int (readonly)
Returns blue component, value 0-255.
20 |
# File 'lib/archimate/data_model/color.rb', line 20 model_attr :b |
#g ⇒ Int (readonly)
Returns green component, value 0-255.
17 |
# File 'lib/archimate/data_model/color.rb', line 17 model_attr :g |
#r ⇒ Int (readonly)
Returns red component, value 0-255.
14 |
# File 'lib/archimate/data_model/color.rb', line 14 model_attr :r |
Class Method Details
.black ⇒ Color
Results in a Color instance for the color black
42 43 44 |
# File 'lib/archimate/data_model/color.rb', line 42 def self.black new(r: 0, g: 0, b: 0, a: 100) end |
.rgba(str) ⇒ Color
Parses a CSS style color string into a color object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/archimate/data_model/color.rb', line 28 def self.rgba(str) return nil if str.nil? md = str.match(/#([\da-f]{2})([\da-f]{2})([\da-f]{2})([\da-f]{2})?/) return nil unless md new( r: md[1].to_i(16), g: md[2].to_i(16), b: md[3].to_i(16), a: md[4].nil? ? 100 : (md[4].to_i(16) / 256.0 * 100.0).to_i ) end |
Instance Method Details
#to_rgba ⇒ Object
60 61 62 |
# File 'lib/archimate/data_model/color.rb', line 60 def to_rgba a == 100 ? format("#%02x%02x%02x", r, g, b) : format("#%02x%02x%02x%02x", r, g, b, scaled_alpha) end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/archimate/data_model/color.rb', line 56 def to_s "Color(r: #{r}, g: #{g}, b: #{b}, a: #{a})" end |