SF::Color
Inherits Struct / Value / Object
Utility struct for manipulating RGBA colors
SF::Color is a simple color struct composed of 4 components:
- Red
- Green
- Blue
- Alpha (opacity)
Each component is a public member, an unsigned integer in
the range 0..255. Thus, colors can be constructed and
manipulated very easily:
color = SF::Color.new(255, 0, 0) # red
color.r = 0 # make it black
color.b = 128 # make it dark blue
The fourth component of colors, named "alpha", represents the opacity of the color. A color with an alpha value of 255 will be fully opaque, while an alpha value of 0 will make a color fully transparent, whatever the value of the other components is.
The most common colors are already defined as static variables:
black = SF::Color::Black
white = SF::Color::White
red = SF::Color::Red
green = SF::Color::Green
blue = SF::Color::Blue
yellow = SF::Color::Yellow
magenta = SF::Color::Magenta
cyan = SF::Color::Cyan
transparent = SF::Color::Transparent
Colors can also be added and modulated (multiplied) using the overloaded operators + and *.
Constants
Black predefined color
Blue predefined color
Cyan predefined color
Green predefined color
Magenta predefined color
Red predefined color
Transparent (black) predefined color
White predefined color
Yellow predefined color
Constructors
Construct the color from its 4 RGBA components
- red - Red component (in the range
0..255) - green - Green component (in the range
0..255) - blue - Blue component (in the range
0..255) - alpha - Alpha (opacity) component (in the range
0..255)
Construct the color from 32-bit unsigned integer
- color - Number containing the RGBA components (in that order)
Default constructor
Constructs an opaque black color. It is equivalent to
SF::Color.new(0, 0, 0, 255).
Instance methods
Overload of the != operator
This operator compares two colors and check if they are different.
- left - Left operand
- right - Right operand
Returns: True if colors are different, false if they are equal
Overload of the binary * operator
This operator returns the component-wise multiplication
(also called "modulation") of two colors.
Components are then divided by 255 so that the result is
still in the range 0 .. 255.
- left - Left operand
- right - Right operand
Returns: Result of left * right
Overload of the binary + operator
This operator returns the component-wise sum of two colors. Components that exceed 255 are clamped to 255.
- left - Left operand
- right - Right operand
Returns: Result of left + right
Overload of the binary - operator
This operator returns the component-wise subtraction of two colors. Components below 0 are clamped to 0.
- left - Left operand
- right - Right operand
Returns: Result of left - right
Overload of the == operator
This operator compares two colors and check if they are equal.
- left - Left operand
- right - Right operand
Returns: True if colors are equal, false if they are different
Returns a shallow copy of this object.
Because Value is a value type, this method returns self,
which already involves a shallow copy of this object because
value types are passed by value.
Retrieve the color as a 32-bit unsigned integer
Returns: Color represented as a 32-bit unsigned integer