ManaPlus
color.h
Go to the documentation of this file.
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 
75 class Color final
76 {
77  public:
82  r(0U),
83  g(0U),
84  b(0U),
85  a(255U)
86  {
87  }
88 
102  explicit constexpr Color(const unsigned int color) :
103  r((color >> 16) & 0xFFU),
104  g((color >> 8) & 0xFFU),
105  b(color & 0xFFU),
106  a(255U)
107  {
108  }
109 
119  constexpr Color(const unsigned int ar,
120  const unsigned int ag,
121  const unsigned int ab,
122  const unsigned int aa) :
123  r(ar),
124  g(ag),
125  b(ab),
126  a(aa)
127  {
128  }
129 
130 
132 
133 
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 
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 
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 
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 
224  constexpr bool operator!=(const Color& color) const
225  {
226  return !(r == color.r &&
227  g == color.g &&
228  b == color.b &&
229  a == color.a);
230  }
231 
235  unsigned int r;
236 
240  unsigned int g;
241 
245  unsigned int b;
246 
251  unsigned int a;
252 };
253 
254 #endif // GUI_COLOR_H
#define CAST_U32
Definition: cast.h:31
Definition: color.h:76
Color(const unsigned int ar, const unsigned int ag, const unsigned int ab, const unsigned int aa)
Definition: color.h:119
bool operator==(const Color &color) const
Definition: color.h:210
unsigned int a
Definition: color.h:251
Color(const unsigned int color)
Definition: color.h:102
Color()
Definition: color.h:81
unsigned int b
Definition: color.h:245
unsigned int r
Definition: color.h:235
bool operator!=(const Color &color) const
Definition: color.h:224
Color operator-(const Color &color) const
Definition: color.h:167
unsigned int g
Definition: color.h:240
Color operator*(const float value) const
Definition: color.h:190
#define constexpr2
Definition: localconsts.h:49
#define constexpr
Definition: localconsts.h:48
#define final
Definition: localconsts.h:46
#define A_DEFAULT_COPY(func)
Definition: localconsts.h:41