]>
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
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
20 #include "wx/colour.h"
21 #include "asserthelper.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 // Check the colour components, with and without alpha.
29 // NB: These are macros and not functions to have correct line numbers in case
31 #define ASSERT_EQUAL_RGB(c, r, g, b) \
32 CPPUNIT_ASSERT_EQUAL( r, (int)c.Red() ); \
33 CPPUNIT_ASSERT_EQUAL( g, (int)c.Green() ); \
34 CPPUNIT_ASSERT_EQUAL( b, (int)c.Blue() )
36 #define ASSERT_EQUAL_RGBA(c, r, g, b, a) \
37 ASSERT_EQUAL_RGB(c, r, g, b); \
38 CPPUNIT_ASSERT_EQUAL( a, (int)c.Alpha() )
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 class ColourTestCase
: public CppUnit::TestCase
50 CPPUNIT_TEST_SUITE( ColourTestCase
);
51 CPPUNIT_TEST( GetSetRGB
);
52 CPPUNIT_TEST( FromString
);
53 CPPUNIT_TEST_SUITE_END();
58 DECLARE_NO_COPY_CLASS(ColourTestCase
)
61 // register in the unnamed registry so that these tests are run by default
62 CPPUNIT_TEST_SUITE_REGISTRATION( ColourTestCase
);
64 // also include in it's own registry so that these tests can be run alone
65 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ColourTestCase
, "ColourTestCase" );
67 void ColourTestCase::GetSetRGB()
72 CPPUNIT_ASSERT_EQUAL( 0x56, (int)c
.Red() );
73 CPPUNIT_ASSERT_EQUAL( 0x34, (int)c
.Green() );
74 CPPUNIT_ASSERT_EQUAL( 0x12, (int)c
.Blue() );
75 CPPUNIT_ASSERT_EQUAL( wxALPHA_OPAQUE
, c
.Alpha() );
77 CPPUNIT_ASSERT_EQUAL( wxColour(0x123456), c
);
78 CPPUNIT_ASSERT_EQUAL( 0x123456, c
.GetRGB() );
80 c
.SetRGBA(0xaabbccdd);
82 CPPUNIT_ASSERT_EQUAL( 0xdd, (int)c
.Red() );
83 CPPUNIT_ASSERT_EQUAL( 0xcc, (int)c
.Green() );
84 CPPUNIT_ASSERT_EQUAL( 0xbb, (int)c
.Blue() );
86 // wxX11 doesn't support alpha at all currently.
88 CPPUNIT_ASSERT_EQUAL( 0xaa, (int)c
.Alpha() );
91 // FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
92 // into account: bug or feature?
93 //CPPUNIT_ASSERT_EQUAL( wxColour(0xaabbccdd), c );
94 CPPUNIT_ASSERT_EQUAL( 0xbbccdd, c
.GetRGB() );
96 CPPUNIT_ASSERT_EQUAL( 0xaabbccdd, c
.GetRGBA() );
100 void ColourTestCase::FromString()
102 ASSERT_EQUAL_RGB( wxColour("rgb(11, 22, 33)"), 11, 22, 33 );
103 ASSERT_EQUAL_RGBA( wxColour("rgba(11, 22, 33, 0.5)"), 11, 22, 33, 128 );
104 ASSERT_EQUAL_RGBA( wxColour("rgba( 11, 22, 33, 0.5 )"), 11, 22, 33, 128 );
106 ASSERT_EQUAL_RGB( wxColour("#aabbcc"), 0xaa, 0xbb, 0xcc );
108 ASSERT_EQUAL_RGB( wxColour("red"), 0xff, 0, 0 );
111 CPPUNIT_ASSERT( !wxFromString("rgb(1, 2)", &col
) );
112 CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456)", &col
) );
113 CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456, foo)", &col
) );