Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
color.cpp
1#include <Core/Utils/Color.hpp>
2#include <Eigen/Core>
3#include <catch2/catch_test_macros.hpp>
4
5TEST_CASE( "Core/Utils/Color", "[unittests][Core][Core/Utils][Color]" ) {
6 using namespace Ra::Core::Utils;
7
8 Scalar alpha = 0.5;
9
10 Color color1 = Color::Red(), color2 = Color::Green();
11 Color white =
12 Color::fromRGB( color1.rgb() + color2.rgb() + Color::Blue().rgb() ); // white = 1,1,1,1
13 Color grey = ( alpha * Color::Black() + ( 1 - alpha ) * white );
14
15 SECTION( "Test color presets" ) {
16 REQUIRE( white.rgb().isApprox( Color::White().rgb() ) );
17 REQUIRE( white.hasValidAlpha() );
18
19 Color white2 = color1 + color2 + Color::Blue(); // white = 1,1,1,3
20 REQUIRE( !white2.hasValidAlpha() );
21
22 REQUIRE( grey.isApprox( Color::Grey( 0.5 ) ) );
23 }
24
25 SECTION( "Test color presets and interpolation" ) {
26
27 // interpolate rgba
28 REQUIRE( grey.rgb().isApprox( Color::Grey( 0.5 ).rgb() ) );
29
30 // interpolate rgb
31 grey = Color::fromRGB( alpha * Color::Black().rgb() + ( 1 - alpha ) * white.rgb() );
32 REQUIRE( grey.isApprox( Color::Grey( 0.5 ) ) );
33
34 grey = Color::fromRGB( alpha * Color::Black().rgb() + ( 1 - alpha ) * white.rgb(),
35 Scalar( 1 ) );
36 REQUIRE( grey.isApprox( Color::Grey( 0.5 ) ) );
37 }
38 SECTION( "Test color conversion" ) {
39 Color grey2 = Color::fromRGB( Eigen::Matrix<Scalar, 3, 1>::Constant( 0.5 ) );
40
41 // Conversion from RBG failed
42 REQUIRE( grey.isApprox( grey2 ) );
43
44 Color grey3;
45 grey3 << .5, .5, .5, 1.;
46 // Conversion from comma operator failed
47 REQUIRE( grey.isApprox( grey3 ) );
48
49 uint32_t half = uint32_t( 0.5 * 255 );
50 uint32_t one = 255;
51 auto rgba = ( half << 24 ) | ( half << 16 ) | ( half << 8 ) | ( one << 0 );
52 auto argb = ( one << 24 ) | ( half << 16 ) | ( half << 8 ) | ( half << 0 );
53
54 // Conversion to RGBA32 failed
55 REQUIRE( grey3.toRGBA32() == rgba );
56 // Conversion to ARGB32 failed
57 REQUIRE( grey3.toARGB32() == argb );
58 }
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() );
63
64 REQUIRE( Color::linearRGBTosRGB( red ).isApprox( Color::Red() ) );
65 REQUIRE( Color::linearRGBTosRGB( green ).isApprox( Color::Green() ) );
66 REQUIRE( Color::linearRGBTosRGB( blue ).isApprox( Color::Blue() ) );
67 }
68 SECTION( "Test named" ) {
69 Color teal = Color::getNamedColor( "teal" );
70 Color dummy = Color::getNamedColor( "dummy" );
71
72 REQUIRE( dummy.isApprox( Color::Black() ) );
73 REQUIRE( teal.isApprox( Color( 0_ra, 128_ra / 255_ra, 128_ra / 255_ra ) ) );
74 }
75}