1#include <Core/Utils/Color.hpp>
3#include <catch2/catch_test_macros.hpp>
5TEST_CASE(
"Core/Utils/Color",
"[unittests][Core][Core/Utils][Color]" ) {
6 using namespace Ra::Core::Utils;
10 Color color1 = Color::Red(), color2 = Color::Green();
12 Color::fromRGB( color1.rgb() + color2.rgb() + Color::Blue().rgb() );
13 Color grey = ( alpha * Color::Black() + ( 1 - alpha ) * white );
15 SECTION(
"Test color presets" ) {
16 REQUIRE( white.rgb().isApprox( Color::White().rgb() ) );
17 REQUIRE( white.hasValidAlpha() );
19 Color white2 = color1 + color2 + Color::Blue();
20 REQUIRE( !white2.hasValidAlpha() );
22 REQUIRE( grey.isApprox( Color::Grey( 0.5 ) ) );
25 SECTION(
"Test color presets and interpolation" ) {
28 REQUIRE( grey.rgb().isApprox( Color::Grey( 0.5 ).rgb() ) );
31 grey = Color::fromRGB( alpha * Color::Black().rgb() + ( 1 - alpha ) * white.rgb() );
32 REQUIRE( grey.isApprox( Color::Grey( 0.5 ) ) );
34 grey = Color::fromRGB( alpha * Color::Black().rgb() + ( 1 - alpha ) * white.rgb(),
36 REQUIRE( grey.isApprox( Color::Grey( 0.5 ) ) );
38 SECTION(
"Test color conversion" ) {
39 Color grey2 = Color::fromRGB( Eigen::Matrix<Scalar, 3, 1>::Constant( 0.5 ) );
42 REQUIRE( grey.isApprox( grey2 ) );
45 grey3 << .5, .5, .5, 1.;
47 REQUIRE( grey.isApprox( grey3 ) );
49 uint32_t half = uint32_t( 0.5 * 255 );
51 auto rgba = ( half << 24 ) | ( half << 16 ) | ( half << 8 ) | ( one << 0 );
52 auto argb = ( one << 24 ) | ( half << 16 ) | ( half << 8 ) | ( half << 0 );
55 REQUIRE( grey3.toRGBA32() == rgba );
57 REQUIRE( grey3.toARGB32() == argb );
59 SECTION(
"Test srgb" ) {
60 Color red = Color::sRGBToLinearRGB( Color::Red() );
61 Color green = Color::sRGBToLinearRGB( Color::Green() );
62 Color blue = Color::sRGBToLinearRGB( Color::Blue() );
64 REQUIRE( Color::linearRGBTosRGB( red ).isApprox( Color::Red() ) );
65 REQUIRE( Color::linearRGBTosRGB( green ).isApprox( Color::Green() ) );
66 REQUIRE( Color::linearRGBTosRGB( blue ).isApprox( Color::Blue() ) );
68 SECTION(
"Test named" ) {
69 Color teal = Color::getNamedColor(
"teal" );
70 Color dummy = Color::getNamedColor(
"dummy" );
72 REQUIRE( dummy.isApprox( Color::Black() ) );
73 REQUIRE( teal.isApprox(
Color( 0_ra, 128_ra / 255_ra, 128_ra / 255_ra ) ) );