Class: Archimate::DataModel::Color

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#aInt, NilClass (readonly)

Returns optional alpha component, value 0-100.

Returns:

  • (Int, NilClass)

    optional alpha component, value 0-100



23
# File 'lib/archimate/data_model/color.rb', line 23

model_attr :a

#bInt (readonly)

Returns blue component, value 0-255.

Returns:

  • (Int)

    blue component, value 0-255



20
# File 'lib/archimate/data_model/color.rb', line 20

model_attr :b

#gInt (readonly)

Returns green component, value 0-255.

Returns:

  • (Int)

    green component, value 0-255



17
# File 'lib/archimate/data_model/color.rb', line 17

model_attr :g

#rInt (readonly)

Returns red component, value 0-255.

Returns:

  • (Int)

    red component, value 0-255



14
# File 'lib/archimate/data_model/color.rb', line 14

model_attr :r

Class Method Details

.blackColor

Results in a Color instance for the color black

Returns:



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

Parameters:

  • str (String)

    CSS color string

Returns:



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_rgbaObject



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_sObject



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