GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/color.h Lines: 13 17 76.5 %
Date: 2017-11-29 Branches: 9 64 14.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/*      _______   __   __   __   ______   __   __   _______   __   __
22
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
23
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
24
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
25
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
26
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
27
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
28
 *
29
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
30
 *
31
 *
32
 * Per Larsson a.k.a finalman
33
 * Olof Naessén a.k.a jansem/yakslem
34
 *
35
 * Visit: http://guichan.sourceforge.net
36
 *
37
 * License: (BSD)
38
 * Redistribution and use in source and binary forms, with or without
39
 * modification, are permitted provided that the following conditions
40
 * are met:
41
 * 1. Redistributions of source code must retain the above copyright
42
 *    notice, this list of conditions and the following disclaimer.
43
 * 2. Redistributions in binary form must reproduce the above copyright
44
 *    notice, this list of conditions and the following disclaimer in
45
 *    the documentation and/or other materials provided with the
46
 *    distribution.
47
 * 3. Neither the name of Guichan nor the names of its contributors may
48
 *    be used to endorse or promote products derived from this software
49
 *    without specific prior written permission.
50
 *
51
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
57
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
58
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
59
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
60
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62
 */
63
64
#ifndef GUI_COLOR_H
65
#define GUI_COLOR_H
66
67
#include "utils/cast.h"
68
69
#include "localconsts.h"
70
71
/**
72
  * Represents a color with red, green, blue and alpha components.
73
  */
74
class Color final
75
{
76
    public:
77
        /**
78
          * Constructor. Initializes the color to black.
79
          */
80
11580
        constexpr Color() :
81
            r(0U),
82
            g(0U),
83
            b(0U),
84
11580
            a(255U)
85
        {
86
        }
87
88
        /**
89
          * Constructor. Constructs a color from the bytes in an integer.
90
          * Call it with a hexadecimal constant for HTML-style color
91
          * representation.
92
          * The alpha component is 255 by default.
93
          *
94
          * EXAMPLE: Color(0xff50a0) constructs a very nice pinkish color.
95
          *
96
          * NOTE: Because of this constructor, integers will be automatically
97
          *       casted to a color by your compiler.
98
          *
99
          * @param color The color to initialise the object with.
100
          */
101
1436124
        explicit constexpr Color(const unsigned int color) :
102
195138
            r((color >> 16) & 0xFFU),
103
195138
            g((color >>  8) & 0xFFU),
104
195138
            b(color         & 0xFFU),
105
1436124
            a(255U)
106
        {
107
        }
108
109
        /**
110
          * Constructor. The default alpha value is 255.
111
          *
112
          * @param ar Red color component (range 0-255).
113
          * @param ag Green color component  (range 0-255).
114
          * @param ab Blue color component (range 0-255).
115
          * @param aa Alpha, used for transparency. A value of 0 means
116
          *          totaly transparent, 255 is totaly opaque.
117
          */
118
        constexpr Color(const unsigned int ar,
119
                        const unsigned int ag,
120
                        const unsigned int ab,
121
188
                        const unsigned int aa = 255) :
122
            r(ar),
123
            g(ag),
124
            b(ab),
125
188
            a(aa)
126
        {
127
        }
128
129
130
        A_DEFAULT_COPY(Color)
131
132
        /**
133
          * Adds the RGB values of two colors together. The values will be
134
          * clamped if they go out of range.
135
          *
136
          * WARNING: This function will reset the alpha value of the
137
          *          returned color to 255.
138
          *
139
          * @param color A color to add to this color.
140
          * @return The added colors with an alpha value set to 255.
141
          */
142
        constexpr2 Color operator+(const Color& color) const
143
        {
144
            Color result(r + color.r,
145
                         g + color.g,
146
                         b + color.b,
147
                         255U);
148
149
            result.r = (result.r > 255U ? 255U : result.r);
150
            result.g = (result.g > 255U ? 255U : result.g);
151
            result.b = (result.b > 255U ? 255U : result.b);
152
153
            return result;
154
        }
155
156
        /**
157
          * Subtracts the RGB values of one color from another.
158
          * The values will be clamped if they go out of range.
159
          *
160
          * WARNING: This function will reset the alpha value of the
161
          *          returned color to 255.
162
          *
163
          * @param color A color to subtract from this color.
164
          * @return The subtracted colors with an alpha value set to 255.
165
          */
166
        constexpr2 Color operator-(const Color& color) const
167
        {
168
            Color result(r - color.r,
169
                         g - color.g,
170
                         b - color.b,
171
                         255U);
172
173
            result.r = (result.r > 255U ? 255U : result.r);
174
            result.g = (result.g > 255U ? 255U : result.g);
175
            result.b = (result.b > 255U ? 255U : result.b);
176
177
            return result;
178
        }
179
180
        /**
181
          * Multiplies the RGB values of a color with a float value.
182
          * The values will be clamped if they go out of range.
183
          *
184
          * @param value The value to multiply the color with.
185
          * @return The multiplied colors. The alpha value will, unlike
186
          *         the add and subtract operations, be multiplied as
187
          *         well.
188
          */
189
        constexpr2 Color operator*(const float value) const
190
        {
191
            Color result(CAST_U32(static_cast<float>(r) * value),
192
                         CAST_U32(static_cast<float>(g) * value),
193
                         CAST_U32(static_cast<float>(b) * value),
194
                         a);
195
196
            result.r = (result.r > 255U ? 255U : result.r);
197
            result.g = (result.g > 255U ? 255U : result.g);
198
            result.b = (result.b > 255U ? 255U : result.b);
199
200
            return result;
201
        }
202
203
        /**
204
          * Compares two colors.
205
          *
206
          * @return True if the two colors have the same RGBA components
207
          *         false otherwise.
208
          */
209
        constexpr bool operator==(const Color& color) const
210
        {
211
            return r == color.r &&
212
                g == color.g &&
213
                b == color.b &&
214
                a == color.a;
215
        }
216
217
        /**
218
          * Compares two colors.
219
          *
220
          * @return True if the two colors have different RGBA components,
221
          *         false otherwise.
222
          */
223
        constexpr bool operator!=(const Color& color) const
224
        {
225






156
            return !(r == color.r &&
226


16
                g == color.g &&
227
8
                b == color.b &&
228
8
                a == color.a);
229
        }
230
231
        /**
232
          * Holds the red color component (range 0-255).
233
          */
234
        unsigned int r;
235
236
        /**
237
          *  Holds the green color component (range 0-255).
238
          */
239
        unsigned int g;
240
241
        /**
242
          *  Holds the blue color component (range 0-255).
243
          */
244
        unsigned int b;
245
246
        /**
247
          * Holds the alpha color component. A value of 0 means totally
248
          * transparent while a value of 255 is considered opaque.
249
          */
250
        unsigned int a;
251
};
252
253
#endif  // GUI_COLOR_H