Skip to content

Commit

Permalink
add color tutorial
Browse files Browse the repository at this point in the history
Signed-off-by: youhy <[email protected]>
  • Loading branch information
AzulRadio committed Dec 11, 2021
1 parent f21a0ab commit 190c1e3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
10 changes: 8 additions & 2 deletions examples/color_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ int main(int argc, char **argv)
//! [Create a color]
ignition::math::Color a = ignition::math::Color(0.3, 0.4, 0.5);
//! [Create a color]

// 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;


//! [Set a new color value]
a.ignition::math::Color::Set(0.6, 0.7, 0.8, 1.0);
//! [Set a new color value]
Expand All @@ -38,14 +38,20 @@ int main(int argc, char **argv)
ignition::math::Color::BGRA blue = 0xFF0000FF;
a.ignition::math::Color::SetFromBGRA(blue);
//! [Set value from BGRA]

//! [Math operator]
std::cout << "Check if a is Blue: " << (a == ignition::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;
//! [Math operator]

//! [Set value from HSV]
// Initialize with HSV. (240, 1.0, 1.0) is blue in HSV.
a.ignition::math::Color::SetFromHSV(240.0, 1.0, 1.0);
//! [Set value from HSV]
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;
//! [Set value from HSV]

}
//! [complete]
1 change: 1 addition & 0 deletions tutorials.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Ignition @IGN_DESIGNATION_CAP@ library and how to use the library effectively.
4. \subpage angle "Angle example"
5. \subpage triangle "Triangle example"
6. \subpage rotation "Rotation example"
7. \subpage color "Color example"

## License

Expand Down
58 changes: 58 additions & 0 deletions tutorials/color.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
\page color 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:

```{.sh}
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:

```{.sh}
./color_example
```

The ouput of the program:

```{.sh}
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: 4 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:

\snippet examples/color_example.cc Create a color

Change the value of a color via `Set()`. All values are set default to 1.0 if not specify.

\snippet examples/color_example.cc Set a new color value

The ABGR, ARGB, BGRA, RGBA are 4 datatypes that allow you to set color from a 32-bit int. Take BGRA as an example:

\snippet examples/color_example.cc Set value from BGRA

Color class overloads math operators including `+`, `-`, `*`, `/`, `[]`, and `==`.

\snippet examples/color_example.cc Math operator

You can also set or read a color in HSV.

\snippet examples/color_example.cc Set value from HSV

There are more functions in `Color`. Take a look at the [API](https://ignitionrobotics.org/api/math/6.9/classignition_1_1math_1_1Color.html)

0 comments on commit 190c1e3

Please sign in to comment.