Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / tests / graphics / colour.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/colour.cpp
3 // Purpose: wxColour unit test
4 // Author: Vadim Zeitlin
5 // Created: 2009-09-19
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/colour.h"
20 #include "asserthelper.h"
21
22 // ----------------------------------------------------------------------------
23 // helpers
24 // ----------------------------------------------------------------------------
25
26 // Check the colour components, with and without alpha.
27 //
28 // NB: These are macros and not functions to have correct line numbers in case
29 // of failure.
30 #define ASSERT_EQUAL_RGB(c, r, g, b) \
31 CPPUNIT_ASSERT_EQUAL( r, (int)c.Red() ); \
32 CPPUNIT_ASSERT_EQUAL( g, (int)c.Green() ); \
33 CPPUNIT_ASSERT_EQUAL( b, (int)c.Blue() )
34
35 #define ASSERT_EQUAL_RGBA(c, r, g, b, a) \
36 ASSERT_EQUAL_RGB(c, r, g, b); \
37 CPPUNIT_ASSERT_EQUAL( a, (int)c.Alpha() )
38
39 // ----------------------------------------------------------------------------
40 // test class
41 // ----------------------------------------------------------------------------
42
43 class ColourTestCase : public CppUnit::TestCase
44 {
45 public:
46 ColourTestCase() { }
47
48 private:
49 CPPUNIT_TEST_SUITE( ColourTestCase );
50 CPPUNIT_TEST( GetSetRGB );
51 CPPUNIT_TEST( FromString );
52 CPPUNIT_TEST_SUITE_END();
53
54 void GetSetRGB();
55 void FromString();
56
57 DECLARE_NO_COPY_CLASS(ColourTestCase)
58 };
59
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( ColourTestCase );
62
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ColourTestCase, "ColourTestCase" );
65
66 void ColourTestCase::GetSetRGB()
67 {
68 wxColour c;
69 c.SetRGB(0x123456);
70
71 CPPUNIT_ASSERT_EQUAL( 0x56, (int)c.Red() );
72 CPPUNIT_ASSERT_EQUAL( 0x34, (int)c.Green() );
73 CPPUNIT_ASSERT_EQUAL( 0x12, (int)c.Blue() );
74 CPPUNIT_ASSERT_EQUAL( wxALPHA_OPAQUE, c.Alpha() );
75
76 CPPUNIT_ASSERT_EQUAL( wxColour(0x123456), c );
77 CPPUNIT_ASSERT_EQUAL( 0x123456, c.GetRGB() );
78
79 c.SetRGBA(0xaabbccdd);
80
81 CPPUNIT_ASSERT_EQUAL( 0xdd, (int)c.Red() );
82 CPPUNIT_ASSERT_EQUAL( 0xcc, (int)c.Green() );
83 CPPUNIT_ASSERT_EQUAL( 0xbb, (int)c.Blue() );
84
85 // wxX11 doesn't support alpha at all currently.
86 #ifndef __WXX11__
87 CPPUNIT_ASSERT_EQUAL( 0xaa, (int)c.Alpha() );
88 #endif // __WXX11__
89
90 // FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
91 // into account: bug or feature?
92 //CPPUNIT_ASSERT_EQUAL( wxColour(0xaabbccdd), c );
93 CPPUNIT_ASSERT_EQUAL( 0xbbccdd, c.GetRGB() );
94 #ifndef __WXX11__
95 CPPUNIT_ASSERT_EQUAL( 0xaabbccdd, c.GetRGBA() );
96 #endif // __WXX11__
97 }
98
99 void ColourTestCase::FromString()
100 {
101 ASSERT_EQUAL_RGB( wxColour("rgb(11, 22, 33)"), 11, 22, 33 );
102 ASSERT_EQUAL_RGBA( wxColour("rgba(11, 22, 33, 0.5)"), 11, 22, 33, 128 );
103 ASSERT_EQUAL_RGBA( wxColour("rgba( 11, 22, 33, 0.5 )"), 11, 22, 33, 128 );
104
105 ASSERT_EQUAL_RGB( wxColour("#aabbcc"), 0xaa, 0xbb, 0xcc );
106
107 ASSERT_EQUAL_RGB( wxColour("red"), 0xff, 0, 0 );
108
109 wxColour col;
110 CPPUNIT_ASSERT( !wxFromString("rgb(1, 2)", &col) );
111 CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456)", &col) );
112 CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456, foo)", &col) );
113 }
114