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

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






78
            return !(r == color.r &&
227


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