Add wxColour::{Set,Get}RGB[A]().
[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 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/colour.h"
21
22 // ----------------------------------------------------------------------------
23 // helper functions
24 // ----------------------------------------------------------------------------
25
26 namespace
27 {
28 // by default colour components values are output incorrectly because they
29 // are unsigned chars, define a small helper struct which formats them in
30 // a more useful way
31 struct ColourChannel
32 {
33 ColourChannel(unsigned char value) : m_value(value) { }
34
35 unsigned char m_value;
36 };
37
38 std::ostream& operator<<(std::ostream& os, const ColourChannel& cc)
39 {
40 os.width(2);
41 os.fill('0');
42 os << static_cast<int>(cc.m_value);
43 return os;
44 }
45 } // anonymous namespace
46
47 // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxColour objects
48 std::ostream& operator<<(std::ostream& os, const wxColour& c)
49 {
50 os << std::hex << std::noshowbase
51 << "("
52 << ColourChannel(c.Red()) << ", "
53 << ColourChannel(c.Green()) << ", "
54 << ColourChannel(c.Blue());
55
56 if ( const unsigned char a = c.Alpha() )
57 {
58 os << ", " << ColourChannel(c.Alpha());
59 }
60
61 os << ")";
62
63 return os;
64 }
65
66 // ----------------------------------------------------------------------------
67 // test class
68 // ----------------------------------------------------------------------------
69
70 class ColourTestCase : public CppUnit::TestCase
71 {
72 public:
73 ColourTestCase() { }
74
75 private:
76 CPPUNIT_TEST_SUITE( ColourTestCase );
77 CPPUNIT_TEST( GetSetRGB );
78 CPPUNIT_TEST_SUITE_END();
79
80 void GetSetRGB();
81
82 DECLARE_NO_COPY_CLASS(ColourTestCase)
83 };
84
85 // register in the unnamed registry so that these tests are run by default
86 CPPUNIT_TEST_SUITE_REGISTRATION( ColourTestCase );
87
88 // also include in it's own registry so that these tests can be run alone
89 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ColourTestCase, "ColourTestCase" );
90
91 void ColourTestCase::GetSetRGB()
92 {
93 wxColour c;
94 c.SetRGB(0x123456);
95
96 CPPUNIT_ASSERT_EQUAL( 0x56, (int)c.Red() );
97 CPPUNIT_ASSERT_EQUAL( 0x34, (int)c.Green() );
98 CPPUNIT_ASSERT_EQUAL( 0x12, (int)c.Blue() );
99 CPPUNIT_ASSERT_EQUAL( wxALPHA_OPAQUE, c.Alpha() );
100
101 CPPUNIT_ASSERT_EQUAL( wxColour(0x123456), c );
102 CPPUNIT_ASSERT_EQUAL( 0x123456, c.GetRGB() );
103
104 c.SetRGBA(0xaabbccdd);
105
106 CPPUNIT_ASSERT_EQUAL( 0xdd, (int)c.Red() );
107 CPPUNIT_ASSERT_EQUAL( 0xcc, (int)c.Green() );
108 CPPUNIT_ASSERT_EQUAL( 0xbb, (int)c.Blue() );
109 CPPUNIT_ASSERT_EQUAL( 0xaa, (int)c.Alpha() );
110
111 // FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
112 // into account: bug or feature?
113 //CPPUNIT_ASSERT_EQUAL( wxColour(0xaabbccdd), c );
114 CPPUNIT_ASSERT_EQUAL( 0xbbccdd, c.GetRGB() );
115 CPPUNIT_ASSERT_EQUAL( 0xaabbccdd, c.GetRGBA() );
116 }
117