2009-09-19 12:29:50 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/graphics/colour.cpp
|
|
|
|
// Purpose: wxColour unit test
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2009-09-19
|
|
|
|
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/colour.h"
|
2010-08-22 18:16:05 -04:00
|
|
|
#include "asserthelper.h"
|
2009-09-19 12:29:50 -04:00
|
|
|
|
2011-03-31 05:28:41 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2020-07-11 08:24:06 -04:00
|
|
|
// helpers for checking wxColour RGB[A] values
|
2011-03-31 05:28:41 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-11 08:24:06 -04:00
|
|
|
typedef wxColour::ChannelType ChannelType;
|
2011-03-31 05:28:41 -04:00
|
|
|
|
2020-07-11 08:24:06 -04:00
|
|
|
class ColourRGBMatcher : public Catch::MatcherBase<wxColour>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ColourRGBMatcher(ChannelType red, ChannelType green, ChannelType blue)
|
|
|
|
: m_red(red),
|
|
|
|
m_green(green),
|
|
|
|
m_blue(blue)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool match(const wxColour& c) const wxOVERRIDE
|
|
|
|
{
|
|
|
|
return c.Red() == m_red && c.Green() == m_green && c.Blue() == m_blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string describe() const wxOVERRIDE
|
|
|
|
{
|
|
|
|
return wxString::Format("!= RGB(%#02x, %#02x, %#02x)",
|
|
|
|
m_red, m_green, m_blue).ToStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const ChannelType m_red, m_green, m_blue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ColourRGBAMatcher : public ColourRGBMatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ColourRGBAMatcher(ChannelType red, ChannelType green, ChannelType blue,
|
|
|
|
ChannelType alpha)
|
|
|
|
: ColourRGBMatcher(red, green, blue),
|
|
|
|
m_alpha(alpha)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool match(const wxColour& c) const wxOVERRIDE
|
|
|
|
{
|
|
|
|
return ColourRGBMatcher::match(c) && c.Alpha() == m_alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string describe() const wxOVERRIDE
|
|
|
|
{
|
|
|
|
return wxString::Format("!= RGBA(%#02x, %#02x, %#02x, %#02x)",
|
|
|
|
m_red, m_green, m_blue, m_alpha).ToStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const ChannelType m_alpha;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
|
|
|
ColourRGBMatcher
|
|
|
|
RGBSameAs(ChannelType red, ChannelType green, ChannelType blue)
|
|
|
|
{
|
|
|
|
return ColourRGBMatcher(red, green, blue);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ColourRGBAMatcher
|
|
|
|
RGBASameAs(ChannelType red, ChannelType green, ChannelType blue, ChannelType alpha)
|
|
|
|
{
|
|
|
|
return ColourRGBAMatcher(red, green, blue, alpha);
|
|
|
|
}
|
2011-03-31 05:28:41 -04:00
|
|
|
|
2009-09-19 12:29:50 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2020-07-10 13:31:33 -04:00
|
|
|
// tests
|
2009-09-19 12:29:50 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-10 13:31:33 -04:00
|
|
|
TEST_CASE("wxColour::GetSetRGB", "[colour][rgb]")
|
2009-09-19 12:29:50 -04:00
|
|
|
{
|
|
|
|
wxColour c;
|
|
|
|
c.SetRGB(0x123456);
|
|
|
|
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK( c.Red() == 0x56 );
|
|
|
|
CHECK( c.Green() == 0x34 );
|
|
|
|
CHECK( c.Blue() == 0x12 );
|
|
|
|
CHECK( c.Alpha() == wxALPHA_OPAQUE );
|
2009-09-19 12:29:50 -04:00
|
|
|
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK( c == wxColour(0x123456) );
|
|
|
|
CHECK( c.GetRGB() == 0x123456 );
|
2009-09-19 12:29:50 -04:00
|
|
|
|
|
|
|
c.SetRGBA(0xaabbccdd);
|
|
|
|
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK( c.Red() == 0xdd);
|
|
|
|
CHECK( c.Green() == 0xcc);
|
|
|
|
CHECK( c.Blue() == 0xbb);
|
2010-11-10 08:52:35 -05:00
|
|
|
|
|
|
|
// wxX11 doesn't support alpha at all currently.
|
|
|
|
#ifndef __WXX11__
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK( c.Alpha() == 0xaa );
|
2010-11-10 08:52:35 -05:00
|
|
|
#endif // __WXX11__
|
2009-09-19 12:29:50 -04:00
|
|
|
|
|
|
|
// FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
|
|
|
|
// into account: bug or feature?
|
2020-07-11 08:24:06 -04:00
|
|
|
//CHECK( c == wxColour(0xaabbccdd) );
|
|
|
|
CHECK( c.GetRGB() == 0xbbccdd );
|
2010-11-10 08:52:35 -05:00
|
|
|
#ifndef __WXX11__
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK( c.GetRGBA() == 0xaabbccdd );
|
2010-11-10 08:52:35 -05:00
|
|
|
#endif // __WXX11__
|
2009-09-19 12:29:50 -04:00
|
|
|
}
|
|
|
|
|
2020-07-10 13:31:33 -04:00
|
|
|
TEST_CASE("wxColour::FromString", "[colour][string]")
|
2011-03-31 05:28:41 -04:00
|
|
|
{
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK_THAT( wxColour("rgb(11, 22, 33)"), RGBSameAs(11, 22, 33) );
|
2014-09-23 13:39:41 -04:00
|
|
|
// wxX11 doesn't support alpha at all currently.
|
|
|
|
#ifndef __WXX11__
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK_THAT( wxColour("rgba(11, 22, 33, 0.5)"), RGBASameAs(11, 22, 33, 128) );
|
|
|
|
CHECK_THAT( wxColour("rgba( 11, 22, 33, 0.5 )"), RGBASameAs(11, 22, 33, 128) );
|
2014-09-23 13:39:41 -04:00
|
|
|
#endif // __WXX11__
|
2011-03-31 05:28:41 -04:00
|
|
|
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK_THAT( wxColour("#aabbcc"), RGBSameAs(0xaa, 0xbb, 0xcc) );
|
2011-03-31 05:28:41 -04:00
|
|
|
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK_THAT( wxColour("red"), RGBSameAs(0xff, 0, 0) );
|
2011-03-31 05:28:41 -04:00
|
|
|
|
|
|
|
wxColour col;
|
2020-07-11 08:24:06 -04:00
|
|
|
CHECK( !wxFromString("rgb(1, 2)", &col) );
|
|
|
|
CHECK( !wxFromString("rgba(1, 2, 3.456)", &col) );
|
|
|
|
CHECK( !wxFromString("rgba(1, 2, 3.456, foo)", &col) );
|
2011-03-31 05:28:41 -04:00
|
|
|
}
|
|
|
|
|
2020-07-11 08:31:54 -04:00
|
|
|
TEST_CASE("wxColour::GetAsString", "[colour][string]")
|
|
|
|
{
|
|
|
|
CHECK( wxColour().GetAsString() == "" );
|
|
|
|
|
|
|
|
wxColour red("red");
|
|
|
|
CHECK( red.GetAsString() == "red" );
|
|
|
|
CHECK( red.GetAsString(wxC2S_CSS_SYNTAX) == "rgb(255, 0, 0)" );
|
|
|
|
CHECK( red.GetAsString(wxC2S_HTML_SYNTAX) == "#FF0000" );
|
|
|
|
}
|
|
|
|
|
2019-04-18 12:52:50 -04:00
|
|
|
TEST_CASE("wxColour::GetLuminance", "[colour][luminance]")
|
|
|
|
{
|
|
|
|
CHECK( wxBLACK->GetLuminance() == Approx(0.0) );
|
|
|
|
CHECK( wxWHITE->GetLuminance() == Approx(1.0) );
|
|
|
|
CHECK( wxRED->GetLuminance() > 0 );
|
|
|
|
CHECK( wxRED->GetLuminance() < 1 );
|
|
|
|
}
|