Color example
This tutorial explains how to use the Color
class from Ignition Math library.
Compile the code
Go to ign-math/examples
and use cmake
to compile the code:
git clone https://github.com/ignitionrobotics/ign-math/ -b ign-math6
cd ign-math/examples
mkdir build
cd build
cmake ..
make
When the code is compiled, run:
./color_example
The ouput of the program:
The alpha value of a should be 1: 1
The RGBA value of a: 0.6 0.7 0.8 1
Check if a is Blue: 1
The RGB value of a should be (0, 0, 1): 0, 0, 1
The HSV value of a: 240 1 1
The RGBA value of a should be (0, 0, 1, 1): 0 0 1 1
Code
Create a color with the following RGBA value. The the alpha value is set default to 1.0:
gz::math::Color a = gz::math::Color(0.3, 0.4, 0.5);
Change the value of a color via Set()
. All values are set default to 1.0 if not specify.
a.gz::math::Color::Set(0.6, 0.7, 0.8, 1.0);
The ABGR, ARGB, BGRA, RGBA are 4 datatypes that allow you to set color from a 32-bit int. Take BGRA as an example:
// 0xFF0000FF is blue in BGRA. Convert it to RGBA.
gz::math::Color::BGRA blue = 0xFF0000FF;
a.gz::math::Color::SetFromBGRA(blue);
Color class overloads math operators including +
, -
, *
, /
, []
, and ==
.
<< a[1] << ", "
<< a[2] << std::endl;
You can also set or read a color in HSV.
// Initialize with HSV. (240, 1.0, 1.0) is blue in HSV.
a.gz::math::Color::SetFromHSV(240.0, 1.0, 1.0);
There are more functions in Color
. Take a look at the API
T endl(T... args)