From b0edecea489b62db69eb74ca8f7623132cceab54 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 19 Sep 2009 16:29:50 +0000 Subject: [PATCH] Add wxColour::{Set,Get}RGB[A](). These methods allow to operate with all 3 or 4 colour channels at once. Add their implementation, documentation and a unit test for wxColour exercising them. Closes #9918. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61976 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/colour.h | 23 ++++++- interface/wx/colour.h | 36 ++++++++++ tests/Makefile.in | 4 ++ tests/graphics/colour.cpp | 117 +++++++++++++++++++++++++++++++++ tests/makefile.bcc | 4 ++ tests/makefile.gcc | 4 ++ tests/makefile.vc | 4 ++ tests/makefile.wat | 4 ++ tests/test.bkl | 1 + tests/test_test_gui.dsp | 4 ++ tests/test_vc7_test_gui.vcproj | 3 + tests/test_vc8_test_gui.vcproj | 4 ++ tests/test_vc9_test_gui.vcproj | 4 ++ 14 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 tests/graphics/colour.cpp diff --git a/docs/changes.txt b/docs/changes.txt index f9055f7267..52e2f71b57 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -426,6 +426,7 @@ All (GUI): and wxPG_EX_TOOLBAR_SEPARATOR styles for finer control over borders. Borders around property grid are now native for consistency. - Added wxXmlResource::LoadObjectRecursively(). +- Added wxColour::Set/GetRGB() and Set/GetRGBA() methods (Marcel Haß). GTK: diff --git a/include/wx/colour.h b/include/wx/colour.h index 68f6845787..6c731ef546 100644 --- a/include/wx/colour.h +++ b/include/wx/colour.h @@ -90,7 +90,7 @@ public: ChannelType green, ChannelType blue, ChannelType alpha = wxALPHA_OPAQUE) - { InitRGBA(red,green,blue, alpha); } + { InitRGBA(red, green, blue, alpha); } // implemented in colourcmn.cpp bool Set(const wxString &str) @@ -119,6 +119,27 @@ public: // implemented in colourcmn.cpp virtual wxString GetAsString(long flags = wxC2S_NAME | wxC2S_CSS_SYNTAX) const; + void SetRGB(wxUint32 colRGB) + { + Set((ChannelType)(0xFF & colRGB), + (ChannelType)(0xFF & (colRGB >> 8)), + (ChannelType)(0xFF & (colRGB >> 16))); + } + + void SetRGBA(wxUint32 colRGBA) + { + Set((ChannelType)(0xFF & colRGBA), + (ChannelType)(0xFF & (colRGBA >> 8)), + (ChannelType)(0xFF & (colRGBA >> 16)), + (ChannelType)(0xFF & (colRGBA >> 24))); + } + + wxUint32 GetRGB() const + { return Red() | (Green() << 8) | (Blue() << 16); } + + wxUint32 GetRGBA() const + { return Red() | (Green() << 8) | (Blue() << 16) | (Alpha() << 24); } + #if !wxCOLOUR_IS_GDIOBJECT virtual bool IsOk() const= 0; diff --git a/interface/wx/colour.h b/interface/wx/colour.h index 36cbfb025b..d6f3fa5f82 100644 --- a/interface/wx/colour.h +++ b/interface/wx/colour.h @@ -112,6 +112,42 @@ public: */ virtual wxString GetAsString(long flags = wxC2S_NAME | wxC2S_CSS_SYNTAX) const; + //@{ + /** + Sets the RGB or RGBA colour values from a single 32 bit value. + + The arguments @a colRGB and @a colRGBA should be of the form 0x00BBGGRR + and 0xAABBGGRR respectively where @c 0xRR, @c 0xGG, @c 0xBB and @c 0xAA + are the values of the red, blue, green and alpha components. + + Notice the right-to-left order of components! + + @see GetRGB(), GetRGBA() + + @since 2.9.1 + */ + void SetRGB(wxUint32 colRGB); + void SetRGBA(wxUint32 colRGBA); + //@} + + //@{ + /** + Gets the RGB or RGBA colour values as a single 32 bit value. + + The returned value is of the same form as expected by SetRGB() and + SetRGBA(). + + Notice that GetRGB() returns the value with 0 as its highest byte + independently of the value actually returned by Alpha(). So for a fully + opaque colour, the return value of GetRGBA() is @c 0xFFBBGGRR while + that of GetRGB() is @c 0x00BBGGRR. + + @since 2.9.1 + */ + wxUint32 GetRGB() const; + wxUint32 GetRGBA() const; + //@} + /** Returns a pixel value which is platform-dependent. On Windows, a COLORREF is returned. diff --git a/tests/Makefile.in b/tests/Makefile.in index 7291842378..36eee2044c 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -132,6 +132,7 @@ TEST_GUI_OBJECTS = \ test_gui_rect.o \ test_gui_size.o \ test_gui_point.o \ + test_gui_colour.o \ test_gui_measuring.o \ test_gui_config.o \ test_gui_comboboxtest.o \ @@ -557,6 +558,9 @@ test_gui_size.o: $(srcdir)/geometry/size.cpp $(TEST_GUI_ODEP) test_gui_point.o: $(srcdir)/geometry/point.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/geometry/point.cpp +test_gui_colour.o: $(srcdir)/graphics/colour.cpp $(TEST_GUI_ODEP) + $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/graphics/colour.cpp + test_gui_measuring.o: $(srcdir)/graphics/measuring.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/graphics/measuring.cpp diff --git a/tests/graphics/colour.cpp b/tests/graphics/colour.cpp new file mode 100644 index 0000000000..6f661b12b4 --- /dev/null +++ b/tests/graphics/colour.cpp @@ -0,0 +1,117 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/graphics/colour.cpp +// Purpose: wxColour unit test +// Author: Vadim Zeitlin +// Created: 2009-09-19 +// RCS-ID: $Id$ +// Copyright: (c) 2009 Vadim Zeitlin +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#include "wx/colour.h" + +// ---------------------------------------------------------------------------- +// helper functions +// ---------------------------------------------------------------------------- + +namespace +{ + // by default colour components values are output incorrectly because they + // are unsigned chars, define a small helper struct which formats them in + // a more useful way + struct ColourChannel + { + ColourChannel(unsigned char value) : m_value(value) { } + + unsigned char m_value; + }; + + std::ostream& operator<<(std::ostream& os, const ColourChannel& cc) + { + os.width(2); + os.fill('0'); + os << static_cast(cc.m_value); + return os; + } +} // anonymous namespace + +// this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxColour objects +std::ostream& operator<<(std::ostream& os, const wxColour& c) +{ + os << std::hex << std::noshowbase + << "(" + << ColourChannel(c.Red()) << ", " + << ColourChannel(c.Green()) << ", " + << ColourChannel(c.Blue()); + + if ( const unsigned char a = c.Alpha() ) + { + os << ", " << ColourChannel(c.Alpha()); + } + + os << ")"; + + return os; +} + +// ---------------------------------------------------------------------------- +// test class +// ---------------------------------------------------------------------------- + +class ColourTestCase : public CppUnit::TestCase +{ +public: + ColourTestCase() { } + +private: + CPPUNIT_TEST_SUITE( ColourTestCase ); + CPPUNIT_TEST( GetSetRGB ); + CPPUNIT_TEST_SUITE_END(); + + void GetSetRGB(); + + DECLARE_NO_COPY_CLASS(ColourTestCase) +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( ColourTestCase ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ColourTestCase, "ColourTestCase" ); + +void ColourTestCase::GetSetRGB() +{ + wxColour c; + c.SetRGB(0x123456); + + CPPUNIT_ASSERT_EQUAL( 0x56, (int)c.Red() ); + CPPUNIT_ASSERT_EQUAL( 0x34, (int)c.Green() ); + CPPUNIT_ASSERT_EQUAL( 0x12, (int)c.Blue() ); + CPPUNIT_ASSERT_EQUAL( wxALPHA_OPAQUE, c.Alpha() ); + + CPPUNIT_ASSERT_EQUAL( wxColour(0x123456), c ); + CPPUNIT_ASSERT_EQUAL( 0x123456, c.GetRGB() ); + + c.SetRGBA(0xaabbccdd); + + CPPUNIT_ASSERT_EQUAL( 0xdd, (int)c.Red() ); + CPPUNIT_ASSERT_EQUAL( 0xcc, (int)c.Green() ); + CPPUNIT_ASSERT_EQUAL( 0xbb, (int)c.Blue() ); + CPPUNIT_ASSERT_EQUAL( 0xaa, (int)c.Alpha() ); + + // FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel + // into account: bug or feature? + //CPPUNIT_ASSERT_EQUAL( wxColour(0xaabbccdd), c ); + CPPUNIT_ASSERT_EQUAL( 0xbbccdd, c.GetRGB() ); + CPPUNIT_ASSERT_EQUAL( 0xaabbccdd, c.GetRGBA() ); +} + diff --git a/tests/makefile.bcc b/tests/makefile.bcc index eb3cc79ecd..955e8fad06 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -116,6 +116,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_rect.obj \ $(OBJS)\test_gui_size.obj \ $(OBJS)\test_gui_point.obj \ + $(OBJS)\test_gui_colour.obj \ $(OBJS)\test_gui_measuring.obj \ $(OBJS)\test_gui_config.obj \ $(OBJS)\test_gui_comboboxtest.obj \ @@ -592,6 +593,9 @@ $(OBJS)\test_gui_size.obj: .\geometry\size.cpp $(OBJS)\test_gui_point.obj: .\geometry\point.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\geometry\point.cpp +$(OBJS)\test_gui_colour.obj: .\graphics\colour.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\graphics\colour.cpp + $(OBJS)\test_gui_measuring.obj: .\graphics\measuring.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\graphics\measuring.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index bdfd9ecd1f..86c0188e44 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -109,6 +109,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_rect.o \ $(OBJS)\test_gui_size.o \ $(OBJS)\test_gui_point.o \ + $(OBJS)\test_gui_colour.o \ $(OBJS)\test_gui_measuring.o \ $(OBJS)\test_gui_config.o \ $(OBJS)\test_gui_comboboxtest.o \ @@ -573,6 +574,9 @@ $(OBJS)\test_gui_size.o: ./geometry/size.cpp $(OBJS)\test_gui_point.o: ./geometry/point.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_gui_colour.o: ./graphics/colour.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_gui_measuring.o: ./graphics/measuring.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index 6dc5ddc78c..b93020d164 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -112,6 +112,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_rect.obj \ $(OBJS)\test_gui_size.obj \ $(OBJS)\test_gui_point.obj \ + $(OBJS)\test_gui_colour.obj \ $(OBJS)\test_gui_measuring.obj \ $(OBJS)\test_gui_config.obj \ $(OBJS)\test_gui_comboboxtest.obj \ @@ -675,6 +676,9 @@ $(OBJS)\test_gui_size.obj: .\geometry\size.cpp $(OBJS)\test_gui_point.obj: .\geometry\point.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\geometry\point.cpp +$(OBJS)\test_gui_colour.obj: .\graphics\colour.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\graphics\colour.cpp + $(OBJS)\test_gui_measuring.obj: .\graphics\measuring.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\graphics\measuring.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index b5164a0f18..cb8ed5db34 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -337,6 +337,7 @@ TEST_GUI_OBJECTS = & $(OBJS)\test_gui_rect.obj & $(OBJS)\test_gui_size.obj & $(OBJS)\test_gui_point.obj & + $(OBJS)\test_gui_colour.obj & $(OBJS)\test_gui_measuring.obj & $(OBJS)\test_gui_config.obj & $(OBJS)\test_gui_comboboxtest.obj & @@ -630,6 +631,9 @@ $(OBJS)\test_gui_size.obj : .AUTODEPEND .\geometry\size.cpp $(OBJS)\test_gui_point.obj : .AUTODEPEND .\geometry\point.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< +$(OBJS)\test_gui_colour.obj : .AUTODEPEND .\graphics\colour.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< + $(OBJS)\test_gui_measuring.obj : .AUTODEPEND .\graphics\measuring.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< diff --git a/tests/test.bkl b/tests/test.bkl index d692755fa3..bc12d98403 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -113,6 +113,7 @@ geometry/rect.cpp geometry/size.cpp geometry/point.cpp + graphics/colour.cpp graphics/measuring.cpp config/config.cpp controls/comboboxtest.cpp diff --git a/tests/test_test_gui.dsp b/tests/test_test_gui.dsp index e93919b17f..0248f7c5ef 100644 --- a/tests/test_test_gui.dsp +++ b/tests/test_test_gui.dsp @@ -243,6 +243,10 @@ SOURCE=.\events\clone.cpp # End Source File # Begin Source File +SOURCE=.\graphics\colour.cpp +# End Source File +# Begin Source File + SOURCE=.\controls\comboboxtest.cpp # End Source File # Begin Source File diff --git a/tests/test_vc7_test_gui.vcproj b/tests/test_vc7_test_gui.vcproj index c155db35a7..9555ecd574 100644 --- a/tests/test_vc7_test_gui.vcproj +++ b/tests/test_vc7_test_gui.vcproj @@ -564,6 +564,9 @@ RelativePath=".\events\clone.cpp"> + + + + diff --git a/tests/test_vc9_test_gui.vcproj b/tests/test_vc9_test_gui.vcproj index ed52a4fc61..e9c44cc253 100644 --- a/tests/test_vc9_test_gui.vcproj +++ b/tests/test_vc9_test_gui.vcproj @@ -800,6 +800,10 @@ > + + -- 2.47.2