Class: SFML::Color

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

Overview

32-bit RGBA color, immutable.

SFML::Color.new(255, 100, 50)        # opaque
SFML::Color.new(255, 100, 50, 128)   # half-transparent
SFML::Color.rgb(255, 100, 50)        # alias
SFML::Color.rgba(255, 100, 50, 128)
SFML::Color["#ff6432"]               # hex (RGB or RRGGBB or RRGGBBAA)
SFML::Color.cornflower_blue          # named

Constant Summary collapse

BLACK =

Standard SFML colors.

new(0,   0,   0)
WHITE =
new(255, 255, 255)
RED =
new(255, 0,   0)
GREEN =
new(0,   255, 0)
BLUE =
new(0,   0,   255)
YELLOW =
new(255, 255, 0)
MAGENTA =
new(255, 0,   255)
CYAN =
new(0,   255, 255)
TRANSPARENT =
new(0,   0,   0,   0)
CORNFLOWER_BLUE =

A nicer default than pure black for empty windows.

new(100, 149, 237)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, g, b, a = 255) ⇒ Color

Returns a new instance of Color.



13
14
15
16
17
18
19
# File 'lib/sfml/graphics/color.rb', line 13

def initialize(r, g, b, a = 255)
  @r = Integer(r)
  @g = Integer(g)
  @b = Integer(b)
  @a = Integer(a)
  freeze
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



11
12
13
# File 'lib/sfml/graphics/color.rb', line 11

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



11
12
13
# File 'lib/sfml/graphics/color.rb', line 11

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g.



11
12
13
# File 'lib/sfml/graphics/color.rb', line 11

def g
  @g
end

#rObject (readonly)

Returns the value of attribute r.



11
12
13
# File 'lib/sfml/graphics/color.rb', line 11

def r
  @r
end

Class Method Details

.[](hex) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sfml/graphics/color.rb', line 24

def self.[](hex)
  str = hex.to_s.delete_prefix("#")
  case str.length
  when 3
    new(str[0].hex * 17, str[1].hex * 17, str[2].hex * 17, 255)
  when 6
    new(str[0..1].to_i(16), str[2..3].to_i(16), str[4..5].to_i(16), 255)
  when 8
    new(str[0..1].to_i(16), str[2..3].to_i(16), str[4..5].to_i(16), str[6..7].to_i(16))
  else
    raise ArgumentError, "Color hex must be #RGB, #RRGGBB, or #RRGGBBAA, got #{hex.inspect}"
  end
end

.blackObject



77
# File 'lib/sfml/graphics/color.rb', line 77

def black           = BLACK

.blueObject



81
# File 'lib/sfml/graphics/color.rb', line 81

def blue            = BLUE

.cornflower_blueObject



86
# File 'lib/sfml/graphics/color.rb', line 86

def cornflower_blue = CORNFLOWER_BLUE

.cyanObject



84
# File 'lib/sfml/graphics/color.rb', line 84

def cyan            = CYAN

.from_native(struct) ⇒ Object

:nodoc:



58
59
60
# File 'lib/sfml/graphics/color.rb', line 58

def self.from_native(struct) # :nodoc:
  new(struct[:r], struct[:g], struct[:b], struct[:a])
end

.greenObject



80
# File 'lib/sfml/graphics/color.rb', line 80

def green           = GREEN

.magentaObject



83
# File 'lib/sfml/graphics/color.rb', line 83

def magenta         = MAGENTA

.redObject



79
# File 'lib/sfml/graphics/color.rb', line 79

def red             = RED

.rgb(r, g, b) ⇒ Object



21
# File 'lib/sfml/graphics/color.rb', line 21

def self.rgb(r, g, b)        = new(r, g, b, 255)

.rgba(r, g, b, a) ⇒ Object



22
# File 'lib/sfml/graphics/color.rb', line 22

def self.rgba(r, g, b, a)    = new(r, g, b, a)

.transparentObject



85
# File 'lib/sfml/graphics/color.rb', line 85

def transparent     = TRANSPARENT

.whiteObject



78
# File 'lib/sfml/graphics/color.rb', line 78

def white           = WHITE

.yellowObject



82
# File 'lib/sfml/graphics/color.rb', line 82

def yellow          = YELLOW

Instance Method Details

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



38
39
40
# File 'lib/sfml/graphics/color.rb', line 38

def ==(other)
  other.is_a?(Color) && @r == other.r && @g == other.g && @b == other.b && @a == other.a
end

#deconstructObject



46
# File 'lib/sfml/graphics/color.rb', line 46

def deconstruct = to_a

#deconstruct_keys(_keys) ⇒ Object



47
# File 'lib/sfml/graphics/color.rb', line 47

def deconstruct_keys(_keys) = to_h

#hashObject



42
# File 'lib/sfml/graphics/color.rb', line 42

def hash = [@r, @g, @b, @a].hash

#to_aObject



44
# File 'lib/sfml/graphics/color.rb', line 44

def to_a = [@r, @g, @b, @a]

#to_hObject



45
# File 'lib/sfml/graphics/color.rb', line 45

def to_h = { r: @r, g: @g, b: @b, a: @a }

#to_nativeObject

:nodoc:



52
53
54
55
56
# File 'lib/sfml/graphics/color.rb', line 52

def to_native # :nodoc:
  C::Graphics::Color.new.tap do |c|
    c[:r] = @r; c[:g] = @g; c[:b] = @b; c[:a] = @a
  end
end

#to_sObject Also known as: inspect



49
# File 'lib/sfml/graphics/color.rb', line 49

def to_s = "Color(#{@r}, #{@g}, #{@b}, #{@a})"