Class: Emf::Model::Geometry::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/emf/model/geometry/color.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Color.



7
8
9
10
11
12
# File 'lib/emf/model/geometry/color.rb', line 7

def initialize(red:, green:, blue:, alpha: 255)
  @red = coerce(red)
  @green = coerce(green)
  @blue = coerce(blue)
  @alpha = coerce(alpha)
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



14
15
16
# File 'lib/emf/model/geometry/color.rb', line 14

def alpha
  @alpha
end

#blueObject (readonly)

Returns the value of attribute blue.



14
15
16
# File 'lib/emf/model/geometry/color.rb', line 14

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



14
15
16
# File 'lib/emf/model/geometry/color.rb', line 14

def green
  @green
end

#redObject (readonly)

Returns the value of attribute red.



14
15
16
# File 'lib/emf/model/geometry/color.rb', line 14

def red
  @red
end

Class Method Details

.blackObject



32
33
34
# File 'lib/emf/model/geometry/color.rb', line 32

def self.black
  new(red: 0, green: 0, blue: 0)
end

.from_wire(wire) ⇒ Object



16
17
18
# File 'lib/emf/model/geometry/color.rb', line 16

def self.from_wire(wire)
  new(red: wire.red, green: wire.green, blue: wire.blue, alpha: wire.reserved.zero? ? 255 : wire.reserved)
end

.transparentObject



40
41
42
# File 'lib/emf/model/geometry/color.rb', line 40

def self.transparent
  new(red: 0, green: 0, blue: 0, alpha: 0)
end

.whiteObject



36
37
38
# File 'lib/emf/model/geometry/color.rb', line 36

def self.white
  new(red: 255, green: 255, blue: 255)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



44
45
46
47
48
# File 'lib/emf/model/geometry/color.rb', line 44

def ==(other)
  other.is_a?(self.class) &&
    red == other.red && green == other.green &&
    blue == other.blue && alpha == other.alpha
end

#hashObject



51
52
53
# File 'lib/emf/model/geometry/color.rb', line 51

def hash
  [self.class, red, green, blue, alpha].hash
end

#inspectObject



55
56
57
# File 'lib/emf/model/geometry/color.rb', line 55

def inspect
  "#<#{self.class.name} #{to_hex_rgba}>"
end

#to_hexObject



24
25
26
# File 'lib/emf/model/geometry/color.rb', line 24

def to_hex
  alpha == 255 ? format("#%02X%02X%02X", red, green, blue) : to_hex_rgba
end

#to_hex_rgbaObject



28
29
30
# File 'lib/emf/model/geometry/color.rb', line 28

def to_hex_rgba
  format("#%02X%02X%02X%02X", red, green, blue, alpha)
end

#to_wireObject



20
21
22
# File 'lib/emf/model/geometry/color.rb', line 20

def to_wire
  Binary::Types::ColorRef.new(red: red, green: green, blue: blue, reserved: alpha)
end