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 =

Pure white, opaque.

new(255, 255, 255)
RED =

Pure red, opaque.

new(255, 0,   0)
GREEN =

Pure green, opaque.

new(0,   255, 0)
BLUE =

Pure blue, opaque.

new(0,   0,   255)
YELLOW =

Pure yellow, opaque.

new(255, 255, 0)
MAGENTA =

Pure magenta, opaque.

new(255, 0,   255)
CYAN =

Pure cyan, opaque.

new(0,   255, 255)
TRANSPARENT =

Fully transparent (alpha = 0).

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

Build a Color from individual channel values. Each must be an Integer in [0, 255]; alpha defaults to 255 (opaque).



16
17
18
19
20
21
22
# File 'lib/sfml/graphics/color.rb', line 16

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

Instance Attribute Details

#a ⇒ Object (readonly)

The Red channel (0–255), Green channel (0–255), Blue channel (0–255), Alpha channel (0–255) components.



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

def a
  @a
end

#b ⇒ Object (readonly)

The Red channel (0–255), Green channel (0–255), Blue channel (0–255), Alpha channel (0–255) components.



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

def b
  @b
end

#g ⇒ Object (readonly)

The Red channel (0–255), Green channel (0–255), Blue channel (0–255), Alpha channel (0–255) components.



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

def g
  @g
end

#r ⇒ Object (readonly)

The Red channel (0–255), Green channel (0–255), Blue channel (0–255), Alpha channel (0–255) components.



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

def r
  @r
end

Class Method Details

.[](hex) ⇒ Object

Parse a hex color string. Accepts #RGB (each digit doubled), #RRGGBB (opaque), or #RRGGBBAA (with alpha). The leading # is optional.

SFML::Color["#ff6432"]     #=> Color(255, 100, 50, 255)
SFML::Color["#ff643280"]   #=> Color(255, 100, 50, 128)
SFML::Color["#abc"]        #=> Color(170, 187, 204, 255)


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sfml/graphics/color.rb', line 36

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

.black ⇒ Object

Returns the BLACK singleton.



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

def black           = BLACK
# Returns the WHITE singleton.

.blue ⇒ Object

Returns the BLUE singleton.



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

def blue            = BLUE
# Returns the YELLOW singleton.

.cornflower_blue ⇒ Object

Returns the CORNFLOWER_BLUE singleton — handy default for empty windows.



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

def cornflower_blue = CORNFLOWER_BLUE

.cyan ⇒ Object

Returns the CYAN singleton.



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

def cyan            = CYAN
# Returns the TRANSPARENT singleton (alpha = 0).

.from_integer(value) ⇒ Object

Build from a packed 0xRRGGBBAA integer.



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

def self.from_integer(value)
  from_native(C::Graphics.sfColor_fromInteger(Integer(value)))
end

.from_native(struct) ⇒ Object

Returns the self.



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

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

.green ⇒ Object

Returns the GREEN singleton.



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

def green           = GREEN
# Returns the BLUE singleton.

.magenta ⇒ Object

Returns the MAGENTA singleton.



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

def magenta         = MAGENTA
# Returns the CYAN singleton.

.red ⇒ Object

Returns the RED singleton.



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

def red             = RED
# Returns the GREEN singleton.

.rgb(r, g, b) ⇒ Object

Opaque RGB constructor — alias for .new(r, g, b).



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

def self.rgb(r, g, b)        = new(r, g, b, 255)
# RGBA constructor — alias for `.new(r, g, b, a)`.

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

RGBA constructor — alias for .new(r, g, b, a).



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

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

.transparent ⇒ Object

Returns the TRANSPARENT singleton (alpha = 0).



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

def transparent     = TRANSPARENT
# Returns the CORNFLOWER_BLUE singleton — handy default for empty windows.

.white ⇒ Object

Returns the WHITE singleton.



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

def white           = WHITE
# Returns the RED singleton.

.yellow ⇒ Object

Returns the YELLOW singleton.



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

def yellow          = YELLOW
# Returns the MAGENTA singleton.

Instance Method Details

#*(other) ⇒ Object Also known as: modulate

Channel-wise multiply, e.g. for tinting (white modulates to input; black modulates to black). Uses CSFML's normalised formula (a * b) / 255.

Raises:

  • (ArgumentError)


108
109
110
111
# File 'lib/sfml/graphics/color.rb', line 108

def *(other)
  raise ArgumentError, "Color *: expected SFML::Color" unless other.is_a?(Color)
  self.class.from_native(C::Graphics.sfColor_modulate(to_native, other.to_native))
end

#+(other) ⇒ Object

Component-wise channel arithmetic (saturating in CSFML — values clamp to [0, 255]).

Raises:

  • (ArgumentError)


94
95
96
97
# File 'lib/sfml/graphics/color.rb', line 94

def +(other)
  raise ArgumentError, "Color +: expected SFML::Color" unless other.is_a?(Color)
  self.class.from_native(C::Graphics.sfColor_add(to_native, other.to_native))
end

#-(other) ⇒ Object

Saturating channel-wise subtraction — values clamp at 0.

Raises:

  • (ArgumentError)


100
101
102
103
# File 'lib/sfml/graphics/color.rb', line 100

def -(other)
  raise ArgumentError, "Color -: expected SFML::Color" unless other.is_a?(Color)
  self.class.from_native(C::Graphics.sfColor_subtract(to_native, other.to_native))
end

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

Value equality — all four channels must match.



51
52
53
# File 'lib/sfml/graphics/color.rb', line 51

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

#deconstruct ⇒ Object

Pattern-match support for in [r, g, b, a].



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

def deconstruct = to_a
# Pattern-match support for `in {r:, g:, b:, a:}`.

#deconstruct_keys(_keys) ⇒ Object

Pattern-match support for in {r:, g:, b:, a:}.



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

def deconstruct_keys(_keys) = to_h

#hash ⇒ Object

Hash key support.



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

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

#to_a ⇒ Object

[r, g, b, a].



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

def to_a = [@r, @g, @b, @a]
# `{r:, g:, b:, a:}`.

#to_h ⇒ Object

{r:, g:, b:, a:}.



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

def to_h = { r: @r, g: @g, b: @b, a: @a }
# Pattern-match support for `in [r, g, b, a]`.

#to_integer ⇒ Object

Pack this color into a single 0xRRGGBBAA integer.



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

def to_integer = C::Graphics.sfColor_toInteger(to_native)

#to_native ⇒ Object

Returns the to native.



72
73
74
75
76
# File 'lib/sfml/graphics/color.rb', line 72

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_s ⇒ Object Also known as: inspect

String representation for debugging.



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

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