struct SF::Color
- SF::Color
- Struct
- Value
- Object
Overview
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 *.
Defined in:
graphics/graphics.crgraphics/obj.cr
Constant Summary
-
Black =
new(0, 0, 0)
-
Black predefined color
-
Blue =
new(0, 0, 255)
-
Blue predefined color
-
Cyan =
new(0, 255, 255)
-
Cyan predefined color
-
Green =
new(0, 255, 0)
-
Green predefined color
-
Magenta =
new(255, 0, 255)
-
Magenta predefined color
-
Red =
new(255, 0, 0)
-
Red predefined color
-
Transparent =
new(0, 0, 0, 0)
-
Transparent (black) predefined color
-
White =
new(255, 255, 255)
-
White predefined color
-
Yellow =
new(255, 255, 0)
-
Yellow predefined color
Constructors
-
.new(red : Int, green : Int, blue : Int, alpha : Int = 255)
Construct the color from its 4 RGBA components
-
.new(color : Int)
Construct the color from 32-bit unsigned integer
-
.new
Default constructor
Instance Method Summary
-
#!=(right : Color) : Bool
Overload of the != operator
-
#*(right : Color) : Color
Overload of the binary * operator
-
#+(right : Color) : Color
Overload of the binary + operator
-
#-(right : Color) : Color
Overload of the binary - operator
-
#==(right : Color) : Bool
Overload of the == operator
-
#a : UInt8
Alpha (opacity) component
- #a=(a : Int)
-
#b : UInt8
Blue component
- #b=(b : Int)
-
#dup : Color
Returns a shallow copy of this object.
-
#g : UInt8
Green component
- #g=(g : Int)
- #inspect(io)
-
#r : UInt8
Red component
- #r=(r : Int)
-
#to_integer : UInt32
Retrieve the color as a 32-bit unsigned integer
Constructor Detail
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 Method Detail
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