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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include "color4d_test_utils.h"
23
25
26#include <gal/color4d.h>
27
28#include <wx/colour.h>
29
30// All these tests are of a class in KIGFX
31using namespace KIGFX;
32
33
37BOOST_AUTO_TEST_SUITE( Color4D )
38
39
40
44{
45 const auto c = COLOR4D{ 0.4, 0.5, 0.6, 0.7 };
46
47 BOOST_CHECK_EQUAL( c.r, 0.4 );
48 BOOST_CHECK_EQUAL( c.g, 0.5 );
49 BOOST_CHECK_EQUAL( c.b, 0.6 );
50 BOOST_CHECK_EQUAL( c.a, 0.7 );
51
52 const auto copied = c;
53
54 // Test equality
56
57 const auto c2 = COLOR4D{ 0.1, 0.2, 0.3, 0.4 };
58
59 // Test inequality
60 BOOST_CHECK_NE( c, c2 );
61}
62
63
74
75
80{
81 // Inverts RGB, A is the same
82 static const std::vector<COLOR_SCALAR_CASE> cases = {
83 { { 0.0, 0.25, 1.0, 1.0 }, 0.0, { 1.0, 0.75, 0.0, 1.0 } },
84 };
85
86 for( const auto& c : cases )
87 {
88 auto col = c.start;
89
90 const auto inverted = col.Inverted();
91 BOOST_CHECK_EQUAL( inverted, c.expected );
92
93 // Test in-place function
94 col.Invert();
95 BOOST_CHECK_EQUAL( col, c.expected );
96 }
97}
98
99
104{
105 static const std::vector<COLOR_SCALAR_CASE> cases = {
106 { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.5, 0.5, 0.5, 1.0 } },
107 { { 0.0, 0.5, 1.0, 1.0 }, 0.5, { 0.5, 0.75, 1.0, 1.0 } },
108 };
109
110 for( const auto& c : cases )
111 {
112 auto col = c.start;
113
114 const auto brightened = col.Brightened( c.factor );
115 BOOST_CHECK_EQUAL( brightened, c.expected );
116
117 // Test in-place function
118 col.Brighten( c.factor );
119 BOOST_CHECK_EQUAL( col, c.expected );
120 }
121}
122
123
128{
129 static const std::vector<COLOR_SCALAR_CASE> cases = {
130 { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.0, 0.0, 0.0, 1.0 } },
131 { { 1.0, 1.0, 1.0, 1.0 }, 0.5, { 0.5, 0.5, 0.5, 1.0 } },
132 };
133
134 for( const auto& c : cases )
135 {
136 auto col = c.start;
137
138 const auto brightened = col.Darkened( c.factor );
139 BOOST_CHECK_EQUAL( brightened, c.expected );
140
141 // Test in-place function
142 col.Darken( c.factor );
143 BOOST_CHECK_EQUAL( col, c.expected );
144 }
145}
146
147
152{
153 static const std::vector<COLOR_SCALAR_CASE> cases = {
154 { { 0.0, 0.0, 0.0, 1.0 }, 0.5, { 0.0, 0.0, 0.0, 0.5 } },
155 { { 0.0, 0.5, 1.0, 1.0 }, 0.5, { 0.0, 0.5, 1.0, 0.5 } },
156 };
157
158 for( const auto& c : cases )
159 {
160 auto& col = c.start;
161
162 const auto with_alpha = col.WithAlpha( c.factor );
163 BOOST_CHECK_EQUAL( with_alpha, c.expected );
164 }
165
166 // Note: If COLOR4D::WithAlpha raised an exception, we could check
167 // the bounds-checking with BOOST_REQUIRE_THROW,
168 // but it assert()s, so we can't.
169}
170
172{
173 double h;
174 double s;
175 double v;
176 unsigned char r;
177 unsigned char g;
178 unsigned char b;
179};
180
181
186{
187 static const std::vector<FROM_HSV_TO_HEX_CASE> cases = {
188 { 10, 0.71, 0.66, 168, 69, 49 },
189 { 15, 0.96, 0.34, 87, 24, 3 },
190 { 120, 0.50, 0.50, 64, 128, 64 },
191 { 190, 0.32, 0.97, 168, 234, 247 },
192 { 240, 0.15, 0.75, 163, 163, 191 },
193 { 240, 0.90, 0.75, 19, 19, 191 },
194 { 310, 0.71, 0.66, 168, 49, 148 },
195 { 331, 0.15, 0.85, 217, 184, 200 },
196 };
197
198 for( const auto& c : cases )
199 {
200 auto col = COLOR4D{};
201 col.FromHSV( c.h, c.s, c.v );
202 double new_h, new_s, new_v;
203 col.ToHSV( new_h, new_s, new_v );
204 const unsigned char alpha = 0xFF;
205
206 BOOST_CHECK_PREDICATE( KI_TEST::IsColorNearHex, ( col )( c.r )( c.g )( c.b )( alpha ) );
207 BOOST_CHECK_CLOSE( c.h, new_h, 0.0001 );
208 BOOST_CHECK_CLOSE( c.s, new_s, 0.0001 );
209 BOOST_CHECK_CLOSE( c.v, new_v, 0.0001 );
210 }
211}
212
214{
215 double h;
216 double s;
217 double l;
218 unsigned char r;
219 unsigned char g;
220 unsigned char b;
221};
222
223
228{
229 static const std::vector<FROM_HSL_TO_HEX_CASE> cases = {
230 { 10, 0.71, 0.66, 230, 127, 107 },
231 { 15, 0.96, 0.34, 170, 45, 3 },
232 { 120, 0.5, 0.5, 64, 191, 64 },
233 { 190, 0.32, 0.97, 245, 249, 250 },
234 { 240, 0.15, 0.75, 182, 182, 201 },
235 { 240, 0.90, 0.75, 134, 134, 249 },
236 { 310, 0.71, 0.66, 230, 107, 209 },
237 { 331, 0.15, 0.85, 222, 211, 217 },
238 };
239
240 for( const auto& c : cases )
241 {
242 auto col = COLOR4D{};
243 col.FromHSL( c.h, c.s, c.l );
244 double new_h, new_s, new_l;
245 col.ToHSL( new_h, new_s, new_l );
246 const unsigned char alpha = 0xFF;
247
248 BOOST_CHECK_PREDICATE( KI_TEST::IsColorNearHex, ( col )( c.r )( c.g )( c.b )( alpha ) );
249 BOOST_CHECK_CLOSE( c.h, new_h, 0.0001 );
250 BOOST_CHECK_CLOSE( c.s, new_s, 0.0001 );
251 BOOST_CHECK_CLOSE( c.l, new_l, 0.0001 );
252 }
253}
254
255
257{
258 wxColour wx;
260};
261
262
263static std::vector<WX_CONV_CASE> wx_conv_cases = {
264 { { 0x00, 0x00, 0x00, 0x00 }, { 0.0, 0.0, 0.0, 0.0 } },
265 { { 0x66, 0x80, 0x99, 0xB3 }, { 0.4, 0.5, 0.6, 0.7 } },
266 { { 0xFF, 0xFF, 0xFF, 0xFF }, { 1.0, 1.0, 1.0, 1.0 } },
267 { { 0xFF, 0x00, 0x00, 0xFF }, { 0.999, 0.001, 0.0, 1.0 } },
268};
269
270
275{
276 for( const auto& c : wx_conv_cases )
277 {
278 wxColour wx_col = c.c4d.ToColour();
279
280 // A hack, but avoids having to define a custom operator<<
281 BOOST_CHECK_EQUAL( wx_col.Red(), c.wx.Red() );
282 BOOST_CHECK_EQUAL( wx_col.Green(), c.wx.Green() );
283 BOOST_CHECK_EQUAL( wx_col.Blue(), c.wx.Blue() );
284 BOOST_CHECK_EQUAL( wx_col.Alpha(), c.wx.Alpha() );
285 }
286}
287
288
293{
294 const double tol = 0.5 / 255.0; // One bit of quantised error
295
296 for( const auto& c : wx_conv_cases )
297 {
298 const auto col = COLOR4D{ c.wx };
299
300 BOOST_CHECK_PREDICATE( KI_TEST::IsColorNear, ( col )( c.c4d )( tol ) );
301 }
302}
303
304
309{
310 std::hash<COLOR4D> colorHasher;
311
312 COLOR4D a( 0.5, 0.5, 0.5, 0.5 );
313 COLOR4D b( 0.5, 0.5, 0.5, 0.5 );
314
315 BOOST_CHECK_EQUAL( a.Compare( b ), 0 );
316 BOOST_CHECK_EQUAL( colorHasher( a ), colorHasher( b ) );
317
318 b.r = 0.25;
319 BOOST_CHECK_GT( a.Compare( b ), 0 );
320 BOOST_CHECK_NE( colorHasher( a ), colorHasher( b ) );
321
322 b.r = 0.75;
323 BOOST_CHECK_LT( a.Compare( b ), 0 );
324
325 b.r = 0.5;
326 b.g = 0.25;
327 BOOST_CHECK_GT( a.Compare( b ), 0 );
328
329 b.g = 0.75;
330 BOOST_CHECK_LT( a.Compare( b ), 0 );
331
332 b.g = 0.5;
333 b.b = 0.25;
334 BOOST_CHECK_GT( a.Compare( b ), 0 );
335
336 b.b = 0.75;
337 BOOST_CHECK_LT( a.Compare( b ), 0 );
338
339 b.b = 0.5;
340 b.a = 0.25;
341 BOOST_CHECK_GT( a.Compare( b ), 0 );
342
343 b.a = 0.75;
344 BOOST_CHECK_LT( a.Compare( b ), 0 );
345}
346
347
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
double r
Red component.
Definition color4d.h:389
double g
Green component.
Definition color4d.h:390
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:437
int Compare(const COLOR4D &aRhs) const
Definition color4d.cpp:606
double a
Alpha component.
Definition color4d.h:392
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:340
double b
Blue component.
Definition color4d.h:391
Test utilities for COLOUR4D objects.
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
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_CASE(HorizontalAlignment)
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
BOOST_AUTO_TEST_SUITE_END()
bool copied
BOOST_CHECK_PREDICATE(ArePolylineEndPointsNearCircle,(chain)(c.m_geom.m_center_point)(radius)(accuracy+epsilon))
BOOST_CHECK_EQUAL(result, "25.4")