Class: SFML::Color
- Inherits:
-
Object
- Object
- SFML::Color
- Defined in:
- lib/sfml/graphics/color.rb
Overview
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
-
#a ⇒ Object
readonly
Returns the value of attribute a.
-
#b ⇒ Object
readonly
Returns the value of attribute b.
-
#g ⇒ Object
readonly
Returns the value of attribute g.
-
#r ⇒ Object
readonly
Returns the value of attribute r.
Class Method Summary collapse
- .[](hex) ⇒ Object
- .black ⇒ Object
- .blue ⇒ Object
- .cornflower_blue ⇒ Object
- .cyan ⇒ Object
-
.from_integer(value) ⇒ Object
Build from a packed 0xRRGGBBAA integer.
-
.from_native(struct) ⇒ Object
:nodoc:.
- .green ⇒ Object
- .magenta ⇒ Object
- .red ⇒ Object
- .rgb(r, g, b) ⇒ Object
- .rgba(r, g, b, a) ⇒ Object
- .transparent ⇒ Object
- .white ⇒ Object
- .yellow ⇒ Object
Instance Method Summary collapse
-
#*(other) ⇒ Object
(also: #modulate)
Channel-wise multiply, e.g.
-
#+(other) ⇒ Object
Component-wise channel arithmetic (saturating in CSFML — values clamp to [0, 255]).
- #-(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #deconstruct ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #hash ⇒ Object
-
#initialize(r, g, b, a = 255) ⇒ Color
constructor
A new instance of Color.
- #to_a ⇒ Object
- #to_h ⇒ Object
-
#to_integer ⇒ Object
Pack this color into a single 0xRRGGBBAA integer.
-
#to_native ⇒ Object
:nodoc:.
- #to_s ⇒ Object (also: #inspect)
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
#a ⇒ Object (readonly)
Returns the value of attribute a.
11 12 13 |
# File 'lib/sfml/graphics/color.rb', line 11 def a @a end |
#b ⇒ Object (readonly)
Returns the value of attribute b.
11 12 13 |
# File 'lib/sfml/graphics/color.rb', line 11 def b @b end |
#g ⇒ Object (readonly)
Returns the value of attribute g.
11 12 13 |
# File 'lib/sfml/graphics/color.rb', line 11 def g @g end |
#r ⇒ Object (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 |
.black ⇒ Object
107 |
# File 'lib/sfml/graphics/color.rb', line 107 def black = BLACK |
.blue ⇒ Object
111 |
# File 'lib/sfml/graphics/color.rb', line 111 def blue = BLUE |
.cornflower_blue ⇒ Object
116 |
# File 'lib/sfml/graphics/color.rb', line 116 def cornflower_blue = CORNFLOWER_BLUE |
.cyan ⇒ Object
114 |
# File 'lib/sfml/graphics/color.rb', line 114 def cyan = CYAN |
.from_integer(value) ⇒ Object
Build from a packed 0xRRGGBBAA integer.
63 64 65 |
# File 'lib/sfml/graphics/color.rb', line 63 def self.from_integer(value) from_native(C::Graphics.sfColor_fromInteger(Integer(value))) end |
.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 |
.green ⇒ Object
110 |
# File 'lib/sfml/graphics/color.rb', line 110 def green = GREEN |
.magenta ⇒ Object
113 |
# File 'lib/sfml/graphics/color.rb', line 113 def magenta = MAGENTA |
.red ⇒ Object
109 |
# File 'lib/sfml/graphics/color.rb', line 109 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) |
.transparent ⇒ Object
115 |
# File 'lib/sfml/graphics/color.rb', line 115 def transparent = TRANSPARENT |
.white ⇒ Object
108 |
# File 'lib/sfml/graphics/color.rb', line 108 def white = WHITE |
.yellow ⇒ Object
112 |
# File 'lib/sfml/graphics/color.rb', line 112 def yellow = YELLOW |
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`.
86 87 88 89 |
# File 'lib/sfml/graphics/color.rb', line 86 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]).
73 74 75 76 |
# File 'lib/sfml/graphics/color.rb', line 73 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
78 79 80 81 |
# File 'lib/sfml/graphics/color.rb', line 78 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?
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 |
#deconstruct ⇒ Object
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 |
#hash ⇒ Object
42 |
# File 'lib/sfml/graphics/color.rb', line 42 def hash = [@r, @g, @b, @a].hash |
#to_a ⇒ Object
44 |
# File 'lib/sfml/graphics/color.rb', line 44 def to_a = [@r, @g, @b, @a] |
#to_h ⇒ Object
45 |
# File 'lib/sfml/graphics/color.rb', line 45 def to_h = { r: @r, g: @g, b: @b, a: @a } |
#to_integer ⇒ Object
Pack this color into a single 0xRRGGBBAA integer.
68 |
# File 'lib/sfml/graphics/color.rb', line 68 def to_integer = C::Graphics.sfColor_toInteger(to_native) |
#to_native ⇒ Object
: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_s ⇒ Object Also known as: inspect
49 |
# File 'lib/sfml/graphics/color.rb', line 49 def to_s = "Color(#{@r}, #{@g}, #{@b}, #{@a})" |