Skip to content

struct SF::Color
inherits Struct #

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 = 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

  • 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)
View source

.new(color : Int)#

Construct the color from 32-bit unsigned integer

  • color - Number containing the RGBA components (in that order)
View source

.new#

Default constructor

Constructs an opaque black color. It is equivalent to SF::Color.new(0, 0, 0, 255).

View source

Methods#

#!=(right : Color) : Bool#

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

View source

#*(right : Color) : Color#

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

View source

#+(right : Color) : Color#

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

View source

#-(right : Color) : Color#

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

View source

#==(right : Color) : Bool#

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

View source

#a : UInt8#

Alpha (opacity) component

View source

#a=(a : Int)#

View source

#b : UInt8#

Blue component

View source

#b=(b : Int)#

View source

#dup : Color#

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.

View source

#g : UInt8#

Green component

View source

#g=(g : Int)#

View source

#inspect(io)#

View source

#r : UInt8#

Red component

View source

#r=(r : Int)#

View source

#to_integer : UInt32#

Retrieve the color as a 32-bit unsigned integer

Returns: Color represented as a 32-bit unsigned integer

View source