]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxColour::{Set,Get}RGB[A]().
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 19 Sep 2009 16:29:50 +0000 (16:29 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 19 Sep 2009 16:29:50 +0000 (16:29 +0000)
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

14 files changed:
docs/changes.txt
include/wx/colour.h
interface/wx/colour.h
tests/Makefile.in
tests/graphics/colour.cpp [new file with mode: 0644]
tests/makefile.bcc
tests/makefile.gcc
tests/makefile.vc
tests/makefile.wat
tests/test.bkl
tests/test_test_gui.dsp
tests/test_vc7_test_gui.vcproj
tests/test_vc8_test_gui.vcproj
tests/test_vc9_test_gui.vcproj

index f9055f7267df1606021a4785dac82eb195a38189..52e2f71b57568d98939b9bbb72a000fbb62a153f 100644 (file)
@@ -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:
 
index 68f68457879c6f2d37c31eb12ae807bea6e6ca1c..6c731ef546bd04b83947cea6e402bc4f011c6865 100644 (file)
@@ -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;
 
index 36cbfb025becd743746821074ff08a19f46a90ea..d6f3fa5f828f35f43d9792dce25c861667a2419f 100644 (file)
@@ -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.
index 7291842378e6f247c40acf6d760677c21f792f8a..36eee2044cec99d77540f0a2c7677d7ce112bf35 100644 (file)
@@ -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 (file)
index 0000000..6f661b1
--- /dev/null
@@ -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 <vadim@wxwidgets.org>
+///////////////////////////////////////////////////////////////////////////////
+
+// ----------------------------------------------------------------------------
+// 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<int>(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() );
+}
+
index eb3cc79ecda48ad47a9346e9e528150058ab1447..955e8fad060031933afe3ef4b15036ec468a583c 100644 (file)
@@ -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
 
index bdfd9ecd1f21df12a6ad22d022e970f6ab5420b6..86c0188e447f14f7fb447f5a91e68a09cdb86920 100644 (file)
@@ -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) $<
 
index 6dc5ddc78ce314e8a7e70622abd2c546b88eb213..b93020d16475c4ce7380eabb436af5a45d70a1e6 100644 (file)
@@ -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
 
index b5164a0f18ede84e5cb0118858bc3d97a4b3184c..cb8ed5db341c04633ad97d66e399a584182b41ea 100644 (file)
@@ -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) $<
 
index d692755fa37d3dd32e331a19237c74351925c657..bc12d98403b74aa3007527040950caba89d4ac8f 100644 (file)
             geometry/rect.cpp
             geometry/size.cpp
             geometry/point.cpp
+            graphics/colour.cpp
             graphics/measuring.cpp
             config/config.cpp
             controls/comboboxtest.cpp
index e93919b17fc8e149b64e07564d593ae205401bae..0248f7c5ef2e7487786c223d00a00b0a10dda6ca 100644 (file)
@@ -243,6 +243,10 @@ SOURCE=.\events\clone.cpp
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\graphics\colour.cpp
+# End Source File
+# Begin Source File
+
 SOURCE=.\controls\comboboxtest.cpp\r
 # End Source File\r
 # Begin Source File\r
index c155db35a78e06c1256ca7f464c9d1ecd7750087..9555ecd574824c014843de0d43ff4cf59698487a 100644 (file)
                                RelativePath=".\events\clone.cpp">\r
                        </File>\r
                        <File\r
+                               RelativePath=".\graphics\colour.cpp">
+                       </File>
+                       <File
                                RelativePath=".\controls\comboboxtest.cpp">\r
                        </File>\r
                        <File\r
index d6c699eac36f5d92a0aa0b26d3ab4b040ca4b037..0dacb80a0490491b91a0b1a6df15f5b180ad0eab 100644 (file)
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\graphics\colour.cpp"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\controls\comboboxtest.cpp"\r
                                >\r
                        </File>\r
index ed52a4fc61919e40bd0887351fc436b2a5648bc1..e9c44cc253981642c482e036bee7f201ef35a574 100644 (file)
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\graphics\colour.cpp"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\controls\comboboxtest.cpp"\r
                                >\r
                        </File>\r