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
This commit is contained in:
@@ -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:
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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.
|
||||
|
@@ -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
|
||||
|
||||
|
117
tests/graphics/colour.cpp
Normal file
117
tests/graphics/colour.cpp
Normal file
@@ -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() );
|
||||
}
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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) $<
|
||||
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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) $<
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -564,6 +564,9 @@
|
||||
RelativePath=".\events\clone.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\graphics\colour.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\controls\comboboxtest.cpp">
|
||||
</File>
|
||||
<File
|
||||
|
@@ -828,6 +828,10 @@
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\graphics\colour.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\controls\comboboxtest.cpp"
|
||||
>
|
||||
</File>
|
||||
|
@@ -800,6 +800,10 @@
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\graphics\colour.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\controls\comboboxtest.cpp"
|
||||
>
|
||||
</File>
|
||||
|
Reference in New Issue
Block a user