]>
git.saurik.com Git - wxWidgets.git/blob - tests/graphics/colour.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/colour.cpp
3 // Purpose: wxColour unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
19 #include "wx/colour.h"
20 #include "asserthelper.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // Check the colour components, with and without alpha.
28 // NB: These are macros and not functions to have correct line numbers in case
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() )
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() )
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 class ColourTestCase
: public CppUnit::TestCase
49 CPPUNIT_TEST_SUITE( ColourTestCase
);
50 CPPUNIT_TEST( GetSetRGB
);
51 CPPUNIT_TEST( FromString
);
52 CPPUNIT_TEST_SUITE_END();
57 DECLARE_NO_COPY_CLASS(ColourTestCase
)
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( ColourTestCase
);
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ColourTestCase
, "ColourTestCase" );
66 void ColourTestCase::GetSetRGB()
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() );
76 CPPUNIT_ASSERT_EQUAL( wxColour(0x123456), c
);
77 CPPUNIT_ASSERT_EQUAL( 0x123456, c
.GetRGB() );
79 c
.SetRGBA(0xaabbccdd);
81 CPPUNIT_ASSERT_EQUAL( 0xdd, (int)c
.Red() );
82 CPPUNIT_ASSERT_EQUAL( 0xcc, (int)c
.Green() );
83 CPPUNIT_ASSERT_EQUAL( 0xbb, (int)c
.Blue() );
85 // wxX11 doesn't support alpha at all currently.
87 CPPUNIT_ASSERT_EQUAL( 0xaa, (int)c
.Alpha() );
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() );
95 CPPUNIT_ASSERT_EQUAL( 0xaabbccdd, c
.GetRGBA() );
99 void ColourTestCase::FromString()
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 );
105 ASSERT_EQUAL_RGB( wxColour("#aabbcc"), 0xaa, 0xbb, 0xcc );
107 ASSERT_EQUAL_RGB( wxColour("red"), 0xff, 0, 0 );
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
) );