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 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15
16 #include "wx/colour.h"
17 #include "asserthelper.h"
18
19 // ----------------------------------------------------------------------------
20 // helpers for checking wxColour RGB[A] values
21 // ----------------------------------------------------------------------------
22
23 typedef wxColour::ChannelType ChannelType;
24
25 class ColourRGBMatcher : public Catch::MatcherBase<wxColour>
26 {
27 public:
ColourRGBMatcher(ChannelType red,ChannelType green,ChannelType blue)28 ColourRGBMatcher(ChannelType red, ChannelType green, ChannelType blue)
29 : m_red(red),
30 m_green(green),
31 m_blue(blue)
32 {
33 }
34
match(const wxColour & c) const35 bool match(const wxColour& c) const wxOVERRIDE
36 {
37 return c.Red() == m_red && c.Green() == m_green && c.Blue() == m_blue;
38 }
39
describe() const40 std::string describe() const wxOVERRIDE
41 {
42 return wxString::Format("!= RGB(%#02x, %#02x, %#02x)",
43 m_red, m_green, m_blue).ToStdString();
44 }
45
46 protected:
47 const ChannelType m_red, m_green, m_blue;
48 };
49
50 class ColourRGBAMatcher : public ColourRGBMatcher
51 {
52 public:
ColourRGBAMatcher(ChannelType red,ChannelType green,ChannelType blue,ChannelType alpha)53 ColourRGBAMatcher(ChannelType red, ChannelType green, ChannelType blue,
54 ChannelType alpha)
55 : ColourRGBMatcher(red, green, blue),
56 m_alpha(alpha)
57 {
58 }
59
match(const wxColour & c) const60 bool match(const wxColour& c) const wxOVERRIDE
61 {
62 return ColourRGBMatcher::match(c) && c.Alpha() == m_alpha;
63 }
64
describe() const65 std::string describe() const wxOVERRIDE
66 {
67 return wxString::Format("!= RGBA(%#02x, %#02x, %#02x, %#02x)",
68 m_red, m_green, m_blue, m_alpha).ToStdString();
69 }
70
71 private:
72 const ChannelType m_alpha;
73 };
74
75 inline
76 ColourRGBMatcher
RGBSameAs(ChannelType red,ChannelType green,ChannelType blue)77 RGBSameAs(ChannelType red, ChannelType green, ChannelType blue)
78 {
79 return ColourRGBMatcher(red, green, blue);
80 }
81
82 inline
83 ColourRGBAMatcher
RGBASameAs(ChannelType red,ChannelType green,ChannelType blue,ChannelType alpha)84 RGBASameAs(ChannelType red, ChannelType green, ChannelType blue, ChannelType alpha)
85 {
86 return ColourRGBAMatcher(red, green, blue, alpha);
87 }
88
89 // ----------------------------------------------------------------------------
90 // tests
91 // ----------------------------------------------------------------------------
92
93 TEST_CASE("wxColour::GetSetRGB", "[colour][rgb]")
94 {
95 wxColour c;
96 c.SetRGB(0x123456);
97
98 CHECK( c.Red() == 0x56 );
99 CHECK( c.Green() == 0x34 );
100 CHECK( c.Blue() == 0x12 );
101 CHECK( c.Alpha() == wxALPHA_OPAQUE );
102
103 CHECK( c == wxColour(0x123456) );
104 CHECK( c.GetRGB() == 0x123456 );
105
106 c.SetRGBA(0xaabbccdd);
107
108 CHECK( c.Red() == 0xdd);
109 CHECK( c.Green() == 0xcc);
110 CHECK( c.Blue() == 0xbb);
111
112 // wxX11 doesn't support alpha at all currently.
113 #ifndef __WXX11__
114 CHECK( c.Alpha() == 0xaa );
115 #endif // __WXX11__
116
117 // FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
118 // into account: bug or feature?
119 //CHECK( c == wxColour(0xaabbccdd) );
120 CHECK( c.GetRGB() == 0xbbccdd );
121 #ifndef __WXX11__
122 CHECK( c.GetRGBA() == 0xaabbccdd );
123 #endif // __WXX11__
124 }
125
126 TEST_CASE("wxColour::FromString", "[colour][string]")
127 {
128 CHECK_THAT( wxColour("rgb(11, 22, 33)"), RGBSameAs(11, 22, 33) );
129 // wxX11 doesn't support alpha at all currently.
130 #ifndef __WXX11__
131 CHECK_THAT( wxColour("rgba(11, 22, 33, 0.5)"), RGBASameAs(11, 22, 33, 128) );
132 CHECK_THAT( wxColour("rgba( 11, 22, 33, 0.5 )"), RGBASameAs(11, 22, 33, 128) );
133 #endif // __WXX11__
134
135 CHECK_THAT( wxColour("#aabbcc"), RGBSameAs(0xaa, 0xbb, 0xcc) );
136
137 CHECK_THAT( wxColour("red"), RGBSameAs(0xff, 0, 0) );
138
139 wxColour col;
140 CHECK( !wxFromString("rgb(1, 2)", &col) );
141 CHECK( !wxFromString("rgba(1, 2, 3.456)", &col) );
142 CHECK( !wxFromString("rgba(1, 2, 3.456, foo)", &col) );
143 }
144
145 TEST_CASE("wxColour::GetAsString", "[colour][string]")
146 {
147 CHECK( wxColour().GetAsString() == "" );
148
149 wxColour red("red");
150 CHECK( red.GetAsString() == "red" );
151 CHECK( red.GetAsString(wxC2S_CSS_SYNTAX) == "rgb(255, 0, 0)" );
152 CHECK( red.GetAsString(wxC2S_HTML_SYNTAX) == "#FF0000" );
153 }
154
155 TEST_CASE("wxColour::GetLuminance", "[colour][luminance]")
156 {
157 CHECK( wxBLACK->GetLuminance() == Approx(0.0) );
158 CHECK( wxWHITE->GetLuminance() == Approx(1.0) );
159 CHECK( wxRED->GetLuminance() > 0 );
160 CHECK( wxRED->GetLuminance() < 1 );
161 }
162