colour.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #include "wx/colour.h"
  16. #include "asserthelper.h"
  17. // ----------------------------------------------------------------------------
  18. // helpers
  19. // ----------------------------------------------------------------------------
  20. // Check the colour components, with and without alpha.
  21. //
  22. // NB: These are macros and not functions to have correct line numbers in case
  23. // of failure.
  24. #define ASSERT_EQUAL_RGB(c, r, g, b) \
  25. CPPUNIT_ASSERT_EQUAL( r, (int)c.Red() ); \
  26. CPPUNIT_ASSERT_EQUAL( g, (int)c.Green() ); \
  27. CPPUNIT_ASSERT_EQUAL( b, (int)c.Blue() )
  28. #define ASSERT_EQUAL_RGBA(c, r, g, b, a) \
  29. ASSERT_EQUAL_RGB(c, r, g, b); \
  30. CPPUNIT_ASSERT_EQUAL( a, (int)c.Alpha() )
  31. // ----------------------------------------------------------------------------
  32. // test class
  33. // ----------------------------------------------------------------------------
  34. class ColourTestCase : public CppUnit::TestCase
  35. {
  36. public:
  37. ColourTestCase() { }
  38. private:
  39. CPPUNIT_TEST_SUITE( ColourTestCase );
  40. CPPUNIT_TEST( GetSetRGB );
  41. CPPUNIT_TEST( FromString );
  42. CPPUNIT_TEST_SUITE_END();
  43. void GetSetRGB();
  44. void FromString();
  45. DECLARE_NO_COPY_CLASS(ColourTestCase)
  46. };
  47. // register in the unnamed registry so that these tests are run by default
  48. CPPUNIT_TEST_SUITE_REGISTRATION( ColourTestCase );
  49. // also include in its own registry so that these tests can be run alone
  50. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ColourTestCase, "ColourTestCase" );
  51. void ColourTestCase::GetSetRGB()
  52. {
  53. wxColour c;
  54. c.SetRGB(0x123456);
  55. CPPUNIT_ASSERT_EQUAL( 0x56, (int)c.Red() );
  56. CPPUNIT_ASSERT_EQUAL( 0x34, (int)c.Green() );
  57. CPPUNIT_ASSERT_EQUAL( 0x12, (int)c.Blue() );
  58. CPPUNIT_ASSERT_EQUAL( wxALPHA_OPAQUE, c.Alpha() );
  59. CPPUNIT_ASSERT_EQUAL( wxColour(0x123456), c );
  60. CPPUNIT_ASSERT_EQUAL( 0x123456, c.GetRGB() );
  61. c.SetRGBA(0xaabbccdd);
  62. CPPUNIT_ASSERT_EQUAL( 0xdd, (int)c.Red() );
  63. CPPUNIT_ASSERT_EQUAL( 0xcc, (int)c.Green() );
  64. CPPUNIT_ASSERT_EQUAL( 0xbb, (int)c.Blue() );
  65. // wxX11 doesn't support alpha at all currently.
  66. #ifndef __WXX11__
  67. CPPUNIT_ASSERT_EQUAL( 0xaa, (int)c.Alpha() );
  68. #endif // __WXX11__
  69. // FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
  70. // into account: bug or feature?
  71. //CPPUNIT_ASSERT_EQUAL( wxColour(0xaabbccdd), c );
  72. CPPUNIT_ASSERT_EQUAL( 0xbbccdd, c.GetRGB() );
  73. #ifndef __WXX11__
  74. CPPUNIT_ASSERT_EQUAL( 0xaabbccdd, c.GetRGBA() );
  75. #endif // __WXX11__
  76. }
  77. void ColourTestCase::FromString()
  78. {
  79. ASSERT_EQUAL_RGB( wxColour("rgb(11, 22, 33)"), 11, 22, 33 );
  80. ASSERT_EQUAL_RGBA( wxColour("rgba(11, 22, 33, 0.5)"), 11, 22, 33, 128 );
  81. ASSERT_EQUAL_RGBA( wxColour("rgba( 11, 22, 33, 0.5 )"), 11, 22, 33, 128 );
  82. ASSERT_EQUAL_RGB( wxColour("#aabbcc"), 0xaa, 0xbb, 0xcc );
  83. ASSERT_EQUAL_RGB( wxColour("red"), 0xff, 0, 0 );
  84. wxColour col;
  85. CPPUNIT_ASSERT( !wxFromString("rgb(1, 2)", &col) );
  86. CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456)", &col) );
  87. CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456, foo)", &col) );
  88. }