Gazebo Math

API Reference

8.0.0

Defines a color using a red (R), green (G), blue (B), and alpha (A) component. Each color component is in the range [0..1]. More...

#include <gz/math/Color.hh>

Public Types

typedef unsigned int ABGR
 A ABGR packed value as an unsigned int Each 8 bits corresponds to a channel.
 
typedef unsigned int ARGB
 A ARGB packed value as an unsigned int Each 8 bits corresponds to a channel.
 
typedef unsigned int BGRA
 A BGRA packed value as an unsigned int Each 8 bits corresponds to a channel.
 
typedef unsigned int RGBA
 A RGBA packed value as an unsigned int Each 8 bits corresponds to a channel.
 

Public Member Functions

 Color ()=default
 Constructor.
 
 Color (const float _r, const float _g, const float _b, const float _a=1.0)
 Constructor.
 
floatA ()
 Get a mutable reference to the alpha value.
 
float A () const
 Get the alpha value.
 
void A (const float _a)
 Set the alpha value.
 
ABGR AsABGR () const
 Get as uint32 ABGR packed value.
 
ARGB AsARGB () const
 Get as uint32 ARGB packed value.
 
BGRA AsBGRA () const
 Get as uint32 BGRA packed value.
 
RGBA AsRGBA () const
 Get as uint32 RGBA packed value.
 
floatB ()
 Get a mutable reference to the blue value.
 
float B () const
 Get the blue value.
 
void B (const float _b)
 Set the blue value.
 
floatG ()
 Get a mutable reference to the green value.
 
float G () const
 Get the green value.
 
void G (const float _g)
 Set the green value.
 
Vector3f HSV () const
 Get the color in HSV colorspace.
 
bool operator!= (const Color &_pt) const
 Inequality operator.
 
const Color operator* (const Color &_pt) const
 Multiplication operator.
 
const Color operator* (const float &_v) const
 Multiply all color components by _v.
 
const Coloroperator*= (const Color &_pt)
 Multiplication equal operator.
 
Color operator+ (const Color &_pt) const
 Addition operator (this + _pt)
 
Color operator+ (const float &_v) const
 Add _v to all color components.
 
const Coloroperator+= (const Color &_pt)
 Addition equal operator.
 
Color operator- (const Color &_pt) const
 Subtraction operator.
 
Color operator- (const float &_v) const
 Subtract _v from all color components.
 
const Coloroperator-= (const Color &_pt)
 Subtraction equal operator.
 
const Color operator/ (const Color &_pt) const
 Division operator.
 
const Color operator/ (const float &_v) const
 Divide all color component by _v.
 
const Coloroperator/= (const Color &_pt)
 Division equal operator.
 
bool operator== (const Color &_pt) const
 Equality operator.
 
float operator[] (const unsigned int _index)
 Array index operator.
 
float operator[] (const unsigned int _index) const
 Array index operator, const version.
 
floatR ()
 Get a mutable reference to the red value.
 
float R () const
 Get the red value.
 
void R (const float _r)
 Set the red value.
 
void Reset ()
 Reset the color to default values to red=0, green=0, blue=0, alpha=1.
 
void Set (const float _r=1, const float _g=1, const float _b=1, const float _a=1)
 Set the contents of the vector.
 
void SetFromABGR (const ABGR _v)
 Set from uint32 ABGR packed value.
 
void SetFromARGB (const ARGB _v)
 Set from uint32 ARGB packed value.
 
void SetFromBGRA (const BGRA _v)
 Set from uint32 BGRA packed value.
 
void SetFromHSV (const float _h, const float _s, const float _v)
 Set a color based on HSV values.
 
void SetFromRGBA (const RGBA _v)
 Set from uint32 RGBA packed value.
 
void SetFromYUV (const float _y, const float _u, const float _v)
 Set from yuv.
 
Vector3f YUV () const
 Get the color in YUV colorspace.
 

Static Public Member Functions

static constexpr Color UnclampedColor (const float _r, const float _g, const float _b, const float _a)
 Static function to create colors that are not clamped.
 

Static Public Attributes

static const ColorBlack
 (0, 0, 0)
 
static const ColorBlue
 (0, 0, 1)
 
static const ColorCyan
 (0, 1, 1)
 
static const ColorGreen
 (0, 1, 0)
 
static const ColorMagenta
 (1, 0, 1)
 
static const ColorRed
 (1, 0, 0)
 
static const ColorWhite
 (1, 1, 1)
 
static const ColorYellow
 (1, 1, 0)
 

Detailed Description

Defines a color using a red (R), green (G), blue (B), and alpha (A) component. Each color component is in the range [0..1].

Example

#include <iostream>
#include <gz/math/Color.hh>
int main(int /*argc*/, char **/*argv*/)
{
gz::math::Color a = gz::math::Color(0.3f, 0.4f, 0.5f);
// The channel order is R, G, B, A and the default alpha value of a should be 1.0
std::cout << "The alpha value of a should be 1: " << a.A() << std::endl;
a.gz::math::Color::Set(0.6f, 0.7f, 0.8f, 1.0f);
std::cout << "The RGBA value of a: " << a << std::endl;
// 0xFF0000FF is blue in BGRA. Convert it to RGBA.
a.gz::math::Color::SetFromBGRA(blue);
std::cout << "Check if a is Blue: " << (a == gz::math::Color::Blue) << std::endl;
std::cout << "The RGB value of a should be (0, 0, 1): " << a[0] << ", "
<< a[1] << ", "
<< a[2] << std::endl;
// Initialize with HSV. (240, 1.0, 1.0) is blue in HSV.
a.gz::math::Color::SetFromHSV(240.0, 1.0, 1.0);
std::cout << "The HSV value of a: " << a.HSV() << std::endl;
std::cout << "The RGBA value of a should be (0, 0, 1, 1): " << a << std::endl;
}

Member Typedef Documentation

◆ ABGR

A ABGR packed value as an unsigned int Each 8 bits corresponds to a channel.

ABGR a = 0xFF0000FF; // (1, 0, 0, 1) for RGBA, i.e. red.

◆ ARGB

A ARGB packed value as an unsigned int Each 8 bits corresponds to a channel.

ARGB a = 0xFF0000FF; // (0, 0, 1, 1) for RGBA, i.e. blue.

◆ BGRA

A BGRA packed value as an unsigned int Each 8 bits corresponds to a channel.

BGRA a = 0xFF0000FF; // (0, 0, 1, 1) for RGBA, i.e. blue.

◆ RGBA

A RGBA packed value as an unsigned int Each 8 bits corresponds to a channel.

RGBA a = 0xFF0000FF; // (1, 0, 0, 1) for RGBA, i.e. red.

Constructor & Destructor Documentation

◆ Color() [1/2]

Color ( )
default

Constructor.

Referenced by Color::UnclampedColor().

◆ Color() [2/2]

Color ( const float  _r,
const float  _g,
const float  _b,
const float  _a = 1.0 
)
inline

Constructor.

Parameters
[in]_rRed value (range 0 to 1)
[in]_gGreen value (range 0 to 1)
[in]_bBlue value (range 0 to 1)
[in]_aAlpha value (0=transparent, 1=opaque)
Note
If there are values outside the range [0, 1], they will be clamped and an error message will be printed

Member Function Documentation

◆ A() [1/3]

float & A ( )

Get a mutable reference to the alpha value.

Returns
The alpha value

◆ A() [2/3]

float A ( ) const

Get the alpha value.

Returns
The alpha value

◆ A() [3/3]

void A ( const float  _a)

Set the alpha value.

Parameters
[in]_aNew alpha value

◆ AsABGR()

ABGR AsABGR ( ) const

Get as uint32 ABGR packed value.

Returns
the color

◆ AsARGB()

ARGB AsARGB ( ) const

Get as uint32 ARGB packed value.

Returns
the color

◆ AsBGRA()

BGRA AsBGRA ( ) const

Get as uint32 BGRA packed value.

Returns
the color

◆ AsRGBA()

RGBA AsRGBA ( ) const

Get as uint32 RGBA packed value.

Returns
the color

◆ B() [1/3]

float & B ( )

Get a mutable reference to the blue value.

Returns
The blue value

◆ B() [2/3]

float B ( ) const

Get the blue value.

Returns
The blue value

◆ B() [3/3]

void B ( const float  _b)

Set the blue value.

Parameters
[in]_bNew blue value

◆ G() [1/3]

float & G ( )

Get a mutable reference to the green value.

Returns
The green value

◆ G() [2/3]

float G ( ) const

Get the green value.

Returns
The green value

◆ G() [3/3]

void G ( const float  _g)

Set the green value.

Parameters
[in]_gNew green value

◆ HSV()

Vector3f HSV ( ) const

Get the color in HSV colorspace.

Returns
HSV values in a Vector3f format. A vector3f containing {NAN_F, NAN_F, NAN_F} is returned on error.

◆ operator!=()

bool operator!= ( const Color _pt) const

Inequality operator.

Parameters
[in]_ptThe color to check for inequality
Returns
True if the this color does not equal _pt

◆ operator*() [1/2]

const Color operator* ( const Color _pt) const

Multiplication operator.

Parameters
[in]_ptThe color to muliply by
Returns
The resulting color

◆ operator*() [2/2]

const Color operator* ( const float _v) const

Multiply all color components by _v.

Parameters
[in]_vThe value to multiply by
Returns
The resulting color

◆ operator*=()

const Color & operator*= ( const Color _pt)

Multiplication equal operator.

Parameters
[in]_ptThe color to muliply by
Returns
The resulting color

◆ operator+() [1/2]

Color operator+ ( const Color _pt) const

Addition operator (this + _pt)

Parameters
[in]_ptColor to add
Returns
The resulting color

◆ operator+() [2/2]

Color operator+ ( const float _v) const

Add _v to all color components.

Parameters
[in]_vValue to add to each color component
Returns
The resulting color

◆ operator+=()

const Color & operator+= ( const Color _pt)

Addition equal operator.

Parameters
[in]_ptColor to add
Returns
The resulting color

◆ operator-() [1/2]

Color operator- ( const Color _pt) const

Subtraction operator.

Parameters
[in]_ptThe color to substract
Returns
The resulting color

◆ operator-() [2/2]

Color operator- ( const float _v) const

Subtract _v from all color components.

Parameters
[in]_vValue to subtract
Returns
The resulting color

◆ operator-=()

const Color & operator-= ( const Color _pt)

Subtraction equal operator.

Parameters
[in]_ptColor to subtract
Returns
The resulting color

◆ operator/() [1/2]

const Color operator/ ( const Color _pt) const

Division operator.

Parameters
[in]_ptColor to divide by
Returns
The resulting color

◆ operator/() [2/2]

const Color operator/ ( const float _v) const

Divide all color component by _v.

Parameters
[in]_vThe value to divide by
Returns
The resulting color

◆ operator/=()

const Color & operator/= ( const Color _pt)

Division equal operator.

Parameters
[in]_ptColor to divide by
Returns
The resulting color

◆ operator==()

bool operator== ( const Color _pt) const

Equality operator.

Parameters
[in]_ptThe color to check for equality
Returns
True if the this color equals _pt

◆ operator[]() [1/2]

float operator[] ( const unsigned int  _index)

Array index operator.

Parameters
[in]_indexColor component index(0=red, 1=green, 2=blue, 3=alpha)
Returns
r, g, b, or a when _index is 0, 1, 2 or 3. A NAN_F value is returned if the _index is invalid

◆ operator[]() [2/2]

float operator[] ( const unsigned int  _index) const

Array index operator, const version.

Parameters
[in]_indexColor component index(0=red, 1=green, 2=blue, 3=alpha)
Returns
r, g, b, or a when _index is 0, 1, 2 or 3. A NAN_F value is returned if the _index is invalid

◆ R() [1/3]

float & R ( )

Get a mutable reference to the red value.

Returns
The red value

◆ R() [2/3]

float R ( ) const

Get the red value.

Returns
The red value

◆ R() [3/3]

void R ( const float  _r)

Set the red value.

Parameters
[in]_rNew red value

◆ Reset()

void Reset ( )

Reset the color to default values to red=0, green=0, blue=0, alpha=1.

◆ Set()

void Set ( const float  _r = 1,
const float  _g = 1,
const float  _b = 1,
const float  _a = 1 
)

Set the contents of the vector.

Parameters
[in]_rRed value (range 0 to 1)
[in]_gGreen value (range 0 to 1)
[in]_bBlue value (range 0 to 1)
[in]_aAlpha value (0=transparent, 1=opaque)

◆ SetFromABGR()

void SetFromABGR ( const ABGR  _v)

Set from uint32 ABGR packed value.

Parameters
[in]_vthe new color

◆ SetFromARGB()

void SetFromARGB ( const ARGB  _v)

Set from uint32 ARGB packed value.

Parameters
[in]_vthe new color

◆ SetFromBGRA()

void SetFromBGRA ( const BGRA  _v)

Set from uint32 BGRA packed value.

Parameters
[in]_vthe new color

◆ SetFromHSV()

void SetFromHSV ( const float  _h,
const float  _s,
const float  _v 
)

Set a color based on HSV values.

Parameters
[in]_hHue(0..360)
[in]_sSaturation(0..1)
[in]_vValue(0..1)

◆ SetFromRGBA()

void SetFromRGBA ( const RGBA  _v)

Set from uint32 RGBA packed value.

Parameters
[in]_vthe new color

◆ SetFromYUV()

void SetFromYUV ( const float  _y,
const float  _u,
const float  _v 
)

Set from yuv.

Parameters
[in]_yvalue
[in]_uvalue
[in]_vvalue

◆ UnclampedColor()

static constexpr Color UnclampedColor ( const float  _r,
const float  _g,
const float  _b,
const float  _a 
)
inlinestaticconstexpr

Static function to create colors that are not clamped.

Parameters
[in]_rRed value (range 0 to 1)
[in]_gGreen value (range 0 to 1)
[in]_bBlue value (range 0 to 1)
[in]_aAlpha value (0=transparent, 1=opaque)
Note
This is mainly intended to initialize constexpr values such as Color::gRed. The Color::Color constructor is recommended for regular use.

References Color::Color().

◆ YUV()

Vector3f YUV ( ) const

Get the color in YUV colorspace.

Returns
the YUV color

Member Data Documentation

◆ Black

const Color& Black
static

(0, 0, 0)

◆ Blue

const Color& Blue
static

(0, 0, 1)

◆ Cyan

const Color& Cyan
static

(0, 1, 1)

◆ Green

const Color& Green
static

(0, 1, 0)

◆ Magenta

const Color& Magenta
static

(1, 0, 1)

◆ Red

const Color& Red
static

(1, 0, 0)

◆ White

const Color& White
static

(1, 1, 1)

◆ Yellow

const Color& Yellow
static

(1, 1, 0)


The documentation for this class was generated from the following file: