KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_color4d.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2018-2023, 2023 KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <boost/test/unit_test.hpp>
25
26#include "color4d_test_utils.h"
27
29
30#include <gal/color4d.h>
31
32#include <wx/colour.h>
33
34// All these tests are of a class in KIGFX
35using namespace KIGFX;
36
37
41BOOST_AUTO_TEST_SUITE( Color4D )
42
43
44
48{
49 const auto c = COLOR4D{ 0.4, 0.5, 0.6, 0.7 };
50
51 BOOST_CHECK_EQUAL( c.r, 0.4 );
52 BOOST_CHECK_EQUAL( c.g, 0.5 );
53 BOOST_CHECK_EQUAL( c.b, 0.6 );
54 BOOST_CHECK_EQUAL( c.a, 0.7 );
55
56 const auto copied = c;
57
58 // Test equality
59 BOOST_CHECK_EQUAL( c, copied );
60
61 const auto c2 = COLOR4D{ 0.1, 0.2, 0.3, 0.4 };
62
63 // Test inequality
64 BOOST_CHECK_NE( c, c2 );
65}
66
67
73{
75 double factor;
77};
78
79
84{
85 // Inverts RGB, A is the same
86 static const std::vector<COLOR_SCALAR_CASE> cases = {
87 { { 0.0, 0.25, 1.0, 1.0 }, 0.0, { 1.0, 0.75, 0.0, 1.0 } },
88 };
89
90 for( const auto& c : cases )
91 {
92 auto col = c.start;
93
94 const auto inverted = col.Inverted();
95 BOOST_CHECK_EQUAL( inverted, c.expected );
96
97 // Test in-place function
98 col.Invert();
99 BOOST_CHECK_EQUAL( col, c.expected );
100 }
101}
102
103
108{
109 static const std::vector<COLOR_SCALAR_CASE> cases = {
110 { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.5, 0.5, 0.5, 1.0 } },
111 { { 0.0, 0.5, 1.0, 1.0 }, 0.5, { 0.5, 0.75, 1.0, 1.0 } },
112 };
113
114 for( const auto& c : cases )
115 {
116 auto col = c.start;
117
118 const auto brightened = col.Brightened( c.factor );
119 BOOST_CHECK_EQUAL( brightened, c.expected );
120
121 // Test in-place function
122 col.Brighten( c.factor );
123 BOOST_CHECK_EQUAL( col, c.expected );
124 }
125}
126
127
132{
133 static const std::vector<COLOR_SCALAR_CASE> cases = {
134 { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.0, 0.0, 0.0, 1.0 } },
135 { { 1.0, 1.0, 1.0, 1.0 }, 0.5, { 0.5, 0.5, 0.5, 1.0 } },
136 };
137
138 for( const auto& c : cases )
139 {
140 auto col = c.start;
141
142 const auto brightened = col.Darkened( c.factor );
143 BOOST_CHECK_EQUAL( brightened, c.expected );
144
145 // Test in-place function
146 col.Darken( c.factor );
147 BOOST_CHECK_EQUAL( col, c.expected );
148 }
149}
150
151
156{
157 static const std::vector<COLOR_SCALAR_CASE> cases = {
158 { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.0, 0.0, 0.0, 0.5 } },
159 { { 0.0, 0.5, 1.0, 1.0 }, 0.5, { 0.0, 0.5, 1.0, 0.5 } },
160 };
161
162 for( const auto& c : cases )
163 {
164 auto& col = c.start;
165
166 const auto with_alpha = col.WithAlpha( c.factor );
167 BOOST_CHECK_EQUAL( with_alpha, c.expected );
168 }
169
170 // Note: If COLOR4D::WithAlpha raised an exception, we could check
171 // the bounds-checking with BOOST_REQUIRE_THROW,
172 // but it assert()s, so we can't.
173}
174
176{
177 double h;
178 double s;
179 double v;
180 unsigned char r;
181 unsigned char g;
182 unsigned char b;
183};
184
185
190{
191 static const std::vector<FROM_HSV_TO_HEX_CASE> cases = {
192 { 10, 0.71, 0.66, 168, 69, 49 },
193 { 15, 0.96, 0.34, 87, 24, 3 },
194 { 120, 0.50, 0.50, 64, 128, 64 },
195 { 190, 0.32, 0.97, 168, 234, 247 },
196 { 240, 0.15, 0.75, 163, 163, 191 },
197 { 240, 0.90, 0.75, 19, 19, 191 },
198 { 310, 0.71, 0.66, 168, 49, 148 },
199 { 331, 0.15, 0.85, 217, 184, 200 },
200 };
201
202 for( const auto& c : cases )
203 {
204 auto col = COLOR4D{};
205 col.FromHSV( c.h, c.s, c.v );
206 double new_h, new_s, new_v;
207 col.ToHSV( new_h, new_s, new_v );
208 const unsigned char alpha = 0xFF;
209
210 BOOST_CHECK_PREDICATE( KI_TEST::IsColorNearHex, ( col )( c.r )( c.g )( c.b )( alpha ) );
211 BOOST_CHECK_CLOSE( c.h, new_h, 0.0001 );
212 BOOST_CHECK_CLOSE( c.s, new_s, 0.0001 );
213 BOOST_CHECK_CLOSE( c.v, new_v, 0.0001 );
214 }
215}
216
218{
219 double h;
220 double s;
221 double l;
222 unsigned char r;
223 unsigned char g;
224 unsigned char b;
225};
226
227
232{
233 static const std::vector<FROM_HSL_TO_HEX_CASE> cases = {
234 { 10, 0.71, 0.66, 230, 127, 107 },
235 { 15, 0.96, 0.34, 170, 45, 3 },
236 { 120, 0.5, 0.5, 64, 191, 64 },
237 { 190, 0.32, 0.97, 245, 249, 250 },
238 { 240, 0.15, 0.75, 182, 182, 201 },
239 { 240, 0.90, 0.75, 134, 134, 249 },
240 { 310, 0.71, 0.66, 230, 107, 209 },
241 { 331, 0.15, 0.85, 222, 211, 217 },
242 };
243
244 for( const auto& c : cases )
245 {
246 auto col = COLOR4D{};
247 col.FromHSL( c.h, c.s, c.l );
248 double new_h, new_s, new_l;
249 col.ToHSL( new_h, new_s, new_l );
250 const unsigned char alpha = 0xFF;
251
252 BOOST_CHECK_PREDICATE( KI_TEST::IsColorNearHex, ( col )( c.r )( c.g )( c.b )( alpha ) );
253 BOOST_CHECK_CLOSE( c.h, new_h, 0.0001 );
254 BOOST_CHECK_CLOSE( c.s, new_s, 0.0001 );
255 BOOST_CHECK_CLOSE( c.l, new_l, 0.0001 );
256 }
257}
258
259
261{
262 wxColour wx;
264};
265
266
267static std::vector<WX_CONV_CASE> wx_conv_cases = {
268 { { 0x00, 0x00, 0x00, 0x00 }, { 0.0, 0.0, 0.0, 0.0 } },
269 { { 0x66, 0x80, 0x99, 0xB3 }, { 0.4, 0.5, 0.6, 0.7 } },
270 { { 0xFF, 0xFF, 0xFF, 0xFF }, { 1.0, 1.0, 1.0, 1.0 } },
271 { { 0xFF, 0x00, 0x00, 0xFF }, { 0.999, 0.001, 0.0, 1.0 } },
272};
273
274
279{
280 for( const auto& c : wx_conv_cases )
281 {
282 wxColour wx_col = c.c4d.ToColour();
283
284 // A hack, but avoids having to define a custom operator<<
285 BOOST_CHECK_EQUAL( wx_col.Red(), c.wx.Red() );
286 BOOST_CHECK_EQUAL( wx_col.Green(), c.wx.Green() );
287 BOOST_CHECK_EQUAL( wx_col.Blue(), c.wx.Blue() );
288 BOOST_CHECK_EQUAL( wx_col.Alpha(), c.wx.Alpha() );
289 }
290}
291
292
297{
298 const double tol = 0.5 / 255.0; // One bit of quantised error
299
300 for( const auto& c : wx_conv_cases )
301 {
302 const auto col = COLOR4D{ c.wx };
303
304 BOOST_CHECK_PREDICATE( KI_TEST::IsColorNear, ( col )( c.c4d )( tol ) );
305 }
306}
307
308
313{
314 std::hash<COLOR4D> colorHasher;
315
316 COLOR4D a( 0.5, 0.5, 0.5, 0.5 );
317 COLOR4D b( 0.5, 0.5, 0.5, 0.5 );
318
319 BOOST_CHECK_EQUAL( a.Compare( b ), 0 );
320 BOOST_CHECK_EQUAL( colorHasher( a ), colorHasher( b ) );
321
322 b.r = 0.25;
323 BOOST_CHECK_GT( a.Compare( b ), 0 );
324 BOOST_CHECK_NE( colorHasher( a ), colorHasher( b ) );
325
326 b.r = 0.75;
327 BOOST_CHECK_LT( a.Compare( b ), 0 );
328
329 b.r = 0.5;
330 b.g = 0.25;
331 BOOST_CHECK_GT( a.Compare( b ), 0 );
332
333 b.g = 0.75;
334 BOOST_CHECK_LT( a.Compare( b ), 0 );
335
336 b.g = 0.5;
337 b.b = 0.25;
338 BOOST_CHECK_GT( a.Compare( b ), 0 );
339
340 b.b = 0.75;
341 BOOST_CHECK_LT( a.Compare( b ), 0 );
342
343 b.b = 0.5;
344 b.a = 0.25;
345 BOOST_CHECK_GT( a.Compare( b ), 0 );
346
347 b.a = 0.75;
348 BOOST_CHECK_LT( a.Compare( b ), 0 );
349}
350
351
352BOOST_AUTO_TEST_SUITE_END()
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
double r
Red component.
Definition: color4d.h:392
double g
Green component.
Definition: color4d.h:393
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:422
int Compare(const COLOR4D &aRhs) const
Definition: color4d.cpp:588
double a
Alpha component.
Definition: color4d.h:395
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:327
double b
Blue component.
Definition: color4d.h:394
Test utilities for COLOUR4D objects.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
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.
bool IsColorNear(const KIGFX::COLOR4D &aCol, const KIGFX::COLOR4D aOther, double aTol)
Checks if a COLOR4D is close enough to another.
Test case data for a test that takes a colour and a scalar factor and returns a result.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(BasicOps)
Declares a struct as the Boost test fixture.
static std::vector< WX_CONV_CASE > wx_conv_cases