Class: Rszr::Color::RGBA

Inherits:
Object
  • Object
show all
Defined in:
lib/rszr/color.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue, alpha = 255) ⇒ RGBA

Returns a new instance of RGBA.



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

def initialize(red, green, blue, alpha = 255)
  if red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255 || alpha < 0 || alpha > 255
    raise ArgumentError, 'color out of range'
  end
  @red, @green, @blue, @alpha = red, green, blue, alpha
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



5
6
7
# File 'lib/rszr/color.rb', line 5

def alpha
  @alpha
end

#blueObject (readonly)

Returns the value of attribute blue.



5
6
7
# File 'lib/rszr/color.rb', line 5

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



5
6
7
# File 'lib/rszr/color.rb', line 5

def green
  @green
end

#redObject (readonly)

Returns the value of attribute red.



5
6
7
# File 'lib/rszr/color.rb', line 5

def red
  @red
end

Instance Method Details

#to_hex(rgb: false) ⇒ Object



19
20
21
# File 'lib/rszr/color.rb', line 19

def to_hex(rgb: false)
  "%0#{rgb ? 6 : 8}x" % to_i(rgb: rgb)
end

#to_i(rgb: false) ⇒ Object



14
15
16
17
# File 'lib/rszr/color.rb', line 14

def to_i(rgb: false)
  i = red.to_i << 24 | green.to_i << 16 | blue.to_i << 8 | alpha.to_i
  rgb ? i >> 8 : i
end