File: colour.cpp

package info (click to toggle)
wxpython4.0 4.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 218,584 kB
  • sloc: cpp: 962,669; python: 231,226; ansic: 170,755; makefile: 51,757; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (161 lines) | stat: -rw-r--r-- 4,682 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
///////////////////////////////////////////////////////////////////////////////
// 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"


#include "wx/colour.h"
#include "asserthelper.h"

// ----------------------------------------------------------------------------
// helpers for checking wxColour RGB[A] values
// ----------------------------------------------------------------------------

typedef wxColour::ChannelType ChannelType;

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);
}

// ----------------------------------------------------------------------------
// tests
// ----------------------------------------------------------------------------

TEST_CASE("wxColour::GetSetRGB", "[colour][rgb]")
{
    wxColour c;
    c.SetRGB(0x123456);

    CHECK( c.Red()   == 0x56 );
    CHECK( c.Green() == 0x34 );
    CHECK( c.Blue()  == 0x12 );
    CHECK( c.Alpha() == wxALPHA_OPAQUE );

    CHECK( c == wxColour(0x123456) );
    CHECK( c.GetRGB() == 0x123456 );

    c.SetRGBA(0xaabbccdd);

    CHECK( c.Red()   == 0xdd);
    CHECK( c.Green() == 0xcc);
    CHECK( c.Blue()  == 0xbb);

    // wxX11 doesn't support alpha at all currently.
#ifndef __WXX11__
    CHECK( c.Alpha() == 0xaa );
#endif // __WXX11__

    // FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
    //        into account: bug or feature?
    //CHECK( c == wxColour(0xaabbccdd) );
    CHECK( c.GetRGB() == 0xbbccdd );
#ifndef __WXX11__
    CHECK( c.GetRGBA() == 0xaabbccdd );
#endif // __WXX11__
}

TEST_CASE("wxColour::FromString", "[colour][string]")
{
    CHECK_THAT( wxColour("rgb(11, 22, 33)"), RGBSameAs(11, 22, 33) );
    // wxX11 doesn't support alpha at all currently.
#ifndef __WXX11__
    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) );
#endif // __WXX11__

    CHECK_THAT( wxColour("#aabbcc"), RGBSameAs(0xaa, 0xbb, 0xcc) );

    CHECK_THAT( wxColour("red"), RGBSameAs(0xff, 0, 0) );

    wxColour col;
    CHECK( !wxFromString("rgb(1, 2)", &col) );
    CHECK( !wxFromString("rgba(1, 2, 3.456)", &col) );
    CHECK( !wxFromString("rgba(1, 2, 3.456, foo)", &col) );
}

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" );
}

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 );
}