KiCad PCB EDA Suite
test_color4d.cpp File Reference
#include <boost/test/unit_test.hpp>
#include "color4d_test_utils.h"
#include <qa_utils/wx_utils/unit_test_utils.h>
#include <gal/color4d.h>

Go to the source code of this file.

Classes

struct  COLOR_SCALAR_CASE
 Test case data for a test that takes a colour and a scalar factor and returns a result. More...
 
struct  FROM_HSV_TO_HEX_CASE
 
struct  FROM_HSL_TO_HEX_CASE
 

Functions

 BOOST_AUTO_TEST_CASE (BasicOps)
 Declares a struct as the Boost test fixture. More...
 
 BOOST_AUTO_TEST_CASE (Invert)
 Check inversion. More...
 
 BOOST_AUTO_TEST_CASE (Brighten)
 Check inversion. More...
 
 BOOST_AUTO_TEST_CASE (Darken)
 Check darken. More...
 
 BOOST_AUTO_TEST_CASE (WithAlpha)
 Check alpha setting. More...
 
 BOOST_AUTO_TEST_CASE (FromHsv)
 Check FromHSV. More...
 
 BOOST_AUTO_TEST_CASE (FromHsl)
 Check FromHSL. More...
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/7]

BOOST_AUTO_TEST_CASE ( BasicOps  )

Declares a struct as the Boost test fixture.

Check basic setting and getting of values

Definition at line 49 of file test_color4d.cpp.

50 {
51  const auto c = COLOR4D{ 0.4, 0.5, 0.6, 0.7 };
52 
53  BOOST_CHECK_EQUAL( c.r, 0.4 );
54  BOOST_CHECK_EQUAL( c.g, 0.5 );
55  BOOST_CHECK_EQUAL( c.b, 0.6 );
56  BOOST_CHECK_EQUAL( c.a, 0.7 );
57 
58  const auto copied = c;
59 
60  // Test equality
61  BOOST_CHECK_EQUAL( c, copied );
62 
63  const auto c2 = COLOR4D{ 0.1, 0.2, 0.3, 0.4 };
64 
65  // Test inequality
66  BOOST_CHECK_NE( c, c2 );
67 }
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103

◆ BOOST_AUTO_TEST_CASE() [2/7]

BOOST_AUTO_TEST_CASE ( Invert  )

Check inversion.

Definition at line 85 of file test_color4d.cpp.

86 {
87  // Inverts RGB, A is the same
88  static const std::vector<COLOR_SCALAR_CASE> cases = {
89  { { 0.0, 0.25, 1.0, 1.0 }, 0.0, { 1.0, 0.75, 0.0, 1.0 } },
90  };
91 
92  for( const auto& c : cases )
93  {
94  auto col = c.start;
95 
96  const auto inverted = col.Inverted();
97  BOOST_CHECK_EQUAL( inverted, c.expected );
98 
99  // Test in-place function
100  col.Invert();
101  BOOST_CHECK_EQUAL( col, c.expected );
102  }
103 }

◆ BOOST_AUTO_TEST_CASE() [3/7]

BOOST_AUTO_TEST_CASE ( Brighten  )

Check inversion.

Definition at line 109 of file test_color4d.cpp.

110 {
111  static const std::vector<COLOR_SCALAR_CASE> cases = {
112  { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.5, 0.5, 0.5, 1.0 } },
113  { { 0.0, 0.5, 1.0, 1.0 }, 0.5, { 0.5, 0.75, 1.0, 1.0 } },
114  };
115 
116  for( const auto& c : cases )
117  {
118  auto col = c.start;
119 
120  const auto brightened = col.Brightened( c.factor );
121  BOOST_CHECK_EQUAL( brightened, c.expected );
122 
123  // Test in-place function
124  col.Brighten( c.factor );
125  BOOST_CHECK_EQUAL( col, c.expected );
126  }
127 }

◆ BOOST_AUTO_TEST_CASE() [4/7]

BOOST_AUTO_TEST_CASE ( Darken  )

Check darken.

Definition at line 133 of file test_color4d.cpp.

134 {
135  static const std::vector<COLOR_SCALAR_CASE> cases = {
136  { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.0, 0.0, 0.0, 1.0 } },
137  { { 1.0, 1.0, 1.0, 1.0 }, 0.5, { 0.5, 0.5, 0.5, 1.0 } },
138  };
139 
140  for( const auto& c : cases )
141  {
142  auto col = c.start;
143 
144  const auto brightened = col.Darkened( c.factor );
145  BOOST_CHECK_EQUAL( brightened, c.expected );
146 
147  // Test in-place function
148  col.Darken( c.factor );
149  BOOST_CHECK_EQUAL( col, c.expected );
150  }
151 }

◆ BOOST_AUTO_TEST_CASE() [5/7]

BOOST_AUTO_TEST_CASE ( WithAlpha  )

Check alpha setting.

Definition at line 157 of file test_color4d.cpp.

158 {
159  static const std::vector<COLOR_SCALAR_CASE> cases = {
160  { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.0, 0.0, 0.0, 0.5 } },
161  { { 0.0, 0.5, 1.0, 1.0 }, 0.5, { 0.0, 0.5, 1.0, 0.5 } },
162  };
163 
164  for( const auto& c : cases )
165  {
166  auto col = c.start;
167 
168  const auto with_alpha = col.WithAlpha( c.factor );
169  BOOST_CHECK_EQUAL( with_alpha, c.expected );
170  }
171 
172  // Note: If COLOR4D::WithAlpha raised an exception, we could check
173  // the bounds-checking with BOOST_REQUIRE_THROW,
174  // but it assert()s, so we can't.
175 }

◆ BOOST_AUTO_TEST_CASE() [6/7]

BOOST_AUTO_TEST_CASE ( FromHsv  )

Check FromHSV.

Definition at line 191 of file test_color4d.cpp.

192 {
193  static const std::vector<FROM_HSV_TO_HEX_CASE> cases = {
194  { 10, 0.71, 0.66, 168, 69, 49 },
195  { 15, 0.96, 0.34, 87, 24, 3 },
196  { 120, 0.50, 0.50, 64, 128, 64 },
197  { 190, 0.32, 0.97, 168, 234, 247 },
198  { 240, 0.15, 0.75, 163, 163, 191 },
199  { 240, 0.90, 0.75, 19, 19, 191 },
200  { 310, 0.71, 0.66, 168, 49, 148 },
201  { 331, 0.15, 0.85, 217, 184, 200 },
202  };
203 
204  for( const auto& c : cases )
205  {
206  auto col = COLOR4D{};
207  col.FromHSV( c.h, c.s, c.v );
208  double new_h, new_s, new_v;
209  col.ToHSV( new_h, new_s, new_v );
210  const unsigned char alpha = 0xFF;
211 
212  BOOST_CHECK_PREDICATE( KI_TEST::IsColorNearHex, ( col )( c.r )( c.g )( c.b )( alpha ) );
213  BOOST_CHECK_CLOSE( c.h, new_h, 0.0001 );
214  BOOST_CHECK_CLOSE( c.s, new_s, 0.0001 );
215  BOOST_CHECK_CLOSE( c.v, new_v, 0.0001 );
216  }
217 }
bool IsColorNearHex(const KIGFX::COLOR4D &aCol, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
Checks if a COLOR4D is close enough to a given RGB char value.
void FromHSV(double aInH, double aInS, double aInV)
Changes currently used color to the one given by hue, saturation and value parameters.
Definition: color4d.cpp:418
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103

References KIGFX::COLOR4D::FromHSV(), and KI_TEST::IsColorNearHex().

◆ BOOST_AUTO_TEST_CASE() [7/7]

BOOST_AUTO_TEST_CASE ( FromHsl  )

Check FromHSL.

Definition at line 233 of file test_color4d.cpp.

234 {
235  static const std::vector<FROM_HSL_TO_HEX_CASE> cases = {
236  { 10, 0.71, 0.66, 230, 127, 107 },
237  { 15, 0.96, 0.34, 170, 45, 3 },
238  { 120, 0.5, 0.5, 64, 191, 64 },
239  { 190, 0.32, 0.97, 245, 249, 250 },
240  { 240, 0.15, 0.75, 182, 182, 201 },
241  { 240, 0.90, 0.75, 134, 134, 249 },
242  { 310, 0.71, 0.66, 230, 107, 209 },
243  { 331, 0.15, 0.85, 222, 211, 217 },
244  };
245 
246  for( const auto& c : cases )
247  {
248  auto col = COLOR4D{};
249  col.FromHSL( c.h, c.s, c.l );
250  double new_h, new_s, new_l;
251  col.ToHSL( new_h, new_s, new_l );
252  const unsigned char alpha = 0xFF;
253 
254  BOOST_CHECK_PREDICATE( KI_TEST::IsColorNearHex, ( col )( c.r )( c.g )( c.b )( alpha ) );
255  BOOST_CHECK_CLOSE( c.h, new_h, 0.0001 );
256  BOOST_CHECK_CLOSE( c.s, new_s, 0.0001 );
257  BOOST_CHECK_CLOSE( c.l, new_l, 0.0001 );
258  }
259 }
bool IsColorNearHex(const KIGFX::COLOR4D &aCol, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
Checks if a COLOR4D is close enough to a given RGB char value.
void FromHSL(double aInHue, double aInSaturation, double aInLightness)
Change currently used color to the one given by hue, saturation and lightness parameters.
Definition: color4d.cpp:323
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103

References KIGFX::COLOR4D::FromHSL(), and KI_TEST::IsColorNearHex().