| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2005 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library 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 Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#ifndef APP_COLOR_H |
| 27 |
#define APP_COLOR_H |
| 28 |
|
| 29 |
#ifdef __GNUC__ |
| 30 |
# include <cstdint> |
| 31 |
#endif |
| 32 |
#include <cmath> |
| 33 |
#include <string> |
| 34 |
|
| 35 |
#include <FCGlobal.h> |
| 36 |
|
| 37 |
// NOLINTBEGIN(readability-magic-numbers) |
| 38 |
namespace Base |
| 39 |
{ |
| 40 |
|
| 41 |
template<class color_type> |
| 42 |
struct color_traits |
| 43 |
{ |
| 44 |
color_traits() = default; |
| 45 |
explicit color_traits(const color_type& ct) |
| 46 |
: ct(ct) |
| 47 |
{} |
| 48 |
float redF() const |
| 49 |
{ |
| 50 |
return static_cast<float>(ct.redF()); |
| 51 |
} |
| 52 |
float greenF() const |
| 53 |
{ |
| 54 |
return static_cast<float>(ct.greenF()); |
| 55 |
} |
| 56 |
float blueF() const |
| 57 |
{ |
| 58 |
return static_cast<float>(ct.blueF()); |
| 59 |
} |
| 60 |
float alphaF() const |
| 61 |
{ |
| 62 |
return static_cast<float>(ct.alphaF()); |
| 63 |
} |
| 64 |
int red() const |
| 65 |
{ |
| 66 |
return ct.red(); |
| 67 |
} |
| 68 |
int green() const |
| 69 |
{ |
| 70 |
return ct.green(); |
| 71 |
} |
| 72 |
int blue() const |
| 73 |
{ |
| 74 |
return ct.blue(); |
| 75 |
} |
| 76 |
int alpha() const |
| 77 |
{ |
| 78 |
return ct.alpha(); |
| 79 |
} |
| 80 |
static color_type makeColor(int red, int green, int blue, int alpha = 255) |
| 81 |
{ |
| 82 |
return color_type {red, green, blue, alpha}; |
| 83 |
} |
| 84 |
|
| 85 |
private: |
| 86 |
color_type ct; |
| 87 |
}; |
| 88 |
|
| 89 |
/** Color class |
| 90 |
*/ |
| 91 |
class BaseExport Color |
| 92 |
{ |
| 93 |
public: |
| 94 |
/** |
| 95 |
* Defines the color as (R,G,B,A) whereas all values are in the range [0,1]. |
| 96 |
* \a A defines the alpha value. |
| 97 |
*/ |
| 98 |
constexpr explicit Color(float R = 0.0, float G = 0.0, float B = 0.0, float A = 1.0) |
| 99 |
: r(R) |
| 100 |
, g(G) |
| 101 |
, b(B) |
| 102 |
, a(A) |
| 103 |
{} |
| 104 |
|
| 105 |
/** |
| 106 |
* Does basically the same as the constructor above unless that (R,G,B,A) is |
| 107 |
* encoded as an unsigned int. |
| 108 |
*/ |
| 109 |
explicit Color(uint32_t rgba); |
| 110 |
|
| 111 |
/** Copy constructor. */ |
| 112 |
Color(const Color& c) = default; |
| 113 |
Color(Color&&) = default; |
| 114 |
|
| 115 |
/** Returns true if both colors are equal. Therefore all components must be equal. */ |
| 116 |
bool operator==(const Color& c) const; |
| 117 |
bool operator!=(const Color& c) const; |
| 118 |
/** |
| 119 |
* Defines the color as (R,G,B,A) whereas all values are in the range [0,1]. |
| 120 |
* \a A defines the alpha value, 1 means fully opaque and 0 transparent. |
| 121 |
*/ |
| 122 |
void set(float R, float G, float B, float A = 1.0); |
| 123 |
float transparency() const; |
| 124 |
void setTransparency(float value); |
| 125 |
Color& operator=(const Color& c) = default; |
| 126 |
Color& operator=(Color&& c) = default; |
| 127 |
/** |
| 128 |
* Sets the color value as a 32 bit combined red/green/blue/alpha value. |
| 129 |
* Each component is 8 bit wide (i.e. from 0x00 to 0xff), and the red |
| 130 |
* value should be stored leftmost, like this: 0xRRGGBBAA. |
| 131 |
* |
| 132 |
* \sa getPackedValue(). |
| 133 |
*/ |
| 134 |
Color& setPackedValue(uint32_t rgba); |
| 135 |
/** |
| 136 |
* Returns color as a 32 bit packed unsigned int in the form 0xRRGGBBAA. |
| 137 |
* |
| 138 |
* \sa setPackedValue(). |
| 139 |
*/ |
| 140 |
uint32_t getPackedValue() const; |
| 141 |
/** |
| 142 |
* Returns color as a 32 bit packed unsigned int in the form 0xRRGGBB. |
| 143 |
*/ |
| 144 |
uint32_t getPackedRGB() const; |
| 145 |
/** |
| 146 |
* Sets color as a 32 bit packed unsigned int in the form 0xRRGGBB. |
| 147 |
*/ |
| 148 |
void setPackedRGB(uint32_t); |
| 149 |
/** |
| 150 |
* Returns color as a 32 bit packed unsigned int in the form 0xAARRGGBB. |
| 151 |
*/ |
| 152 |
uint32_t getPackedARGB() const; |
| 153 |
/** |
| 154 |
* Sets color as a 32 bit packed unsigned int in the form 0xAARRGGBB. |
| 155 |
*/ |
| 156 |
void setPackedARGB(uint32_t); |
| 157 |
|
| 158 |
template<typename T> |
| 159 |
static uint32_t asPackedRGBA(const T& color) |
| 160 |
{ |
| 161 |
color_traits<T> ct {color}; |
| 162 |
return (ct.red() << 24) | (ct.green() << 16) | (ct.blue() << 8) | ct.alpha(); |
| 163 |
} |
| 164 |
|
| 165 |
template<typename T> |
| 166 |
static T fromPackedRGBA(uint32_t color) |
| 167 |
{ |
| 168 |
return color_traits<T>::makeColor( |
| 169 |
(color >> 24) & 0xff, |
| 170 |
(color >> 16) & 0xff, |
| 171 |
(color >> 8) & 0xff, |
| 172 |
(color & 0xff) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
template<typename T> |
| 177 |
static uint32_t asPackedRGB(const T& color) |
| 178 |
{ |
| 179 |
color_traits<T> ct {color}; |
| 180 |
return (ct.red() << 24) | (ct.green() << 16) | (ct.blue() << 8); |
| 181 |
} |
| 182 |
|
| 183 |
template<typename T> |
| 184 |
static T fromPackedRGB(uint32_t color) |
| 185 |
{ |
| 186 |
return color_traits<T>::makeColor((color >> 24) & 0xff, (color >> 16) & 0xff, (color >> 8) & 0xff); |
| 187 |
} |
| 188 |
/** |
| 189 |
* creates FC Color from template type, e.g. Qt QColor |
| 190 |
*/ |
| 191 |
template<typename T> |
| 192 |
void setValue(const T& q) |
| 193 |
{ |
| 194 |
color_traits<T> ct {q}; |
| 195 |
set(ct.redF(), ct.greenF(), ct.blueF(), ct.alphaF()); |
| 196 |
} |
| 197 |
/** |
| 198 |
* returns a template type e.g. Qt color equivalent to FC color |
| 199 |
* |
| 200 |
*/ |
| 201 |
template<typename T> |
| 202 |
inline T asValue() const |
| 203 |
{ |
| 204 |
// clang-format off |
| 205 |
return color_traits<T>::makeColor(int(std::lround(r * 255.0F)), |
| 206 |
int(std::lround(g * 255.0F)), |
| 207 |
int(std::lround(b * 255.0F)), |
| 208 |
int(std::lround(a * 255.0F))); |
| 209 |
// clang-format on |
| 210 |
} |
| 211 |
/** |
| 212 |
* creates FC Color from template type, e.g. Qt QColor |
| 213 |
*/ |
| 214 |
template<typename T> |
| 215 |
static Color fromValue(const T& q) |
| 216 |
{ |
| 217 |
color_traits<T> ct {q}; |
| 218 |
return Color(ct.redF(), ct.greenF(), ct.blueF(), ct.alphaF()); |
| 219 |
} |
| 220 |
/** |
| 221 |
* returns color as hex color "#RRGGBB" |
| 222 |
* |
| 223 |
*/ |
| 224 |
std::string asHexString() const; |
| 225 |
|
| 226 |
/** |
| 227 |
* gets color from hex color "#RRGGBB" |
| 228 |
* |
| 229 |
*/ |
| 230 |
bool fromHexString(const std::string& hex); |
| 231 |
|
| 232 |
/// color values, public accessible |
| 233 |
float r {}, g {}, b {}, a {}; |
| 234 |
}; |
| 235 |
|
| 236 |
// Specialization for Color |
| 237 |
template<> |
| 238 |
struct color_traits<Base::Color> |
| 239 |
{ |
| 240 |
using color_type = Base::Color; |
| 241 |
color_traits() = default; |
| 242 |
explicit color_traits(const color_type& ct) |
| 243 |
: ct(ct) |
| 244 |
{} |
| 245 |
float redF() const |
| 246 |
{ |
| 247 |
return ct.r; |
| 248 |
} |
| 249 |
float greenF() const |
| 250 |
{ |
| 251 |
return ct.g; |
| 252 |
} |
| 253 |
float blueF() const |
| 254 |
{ |
| 255 |
return ct.b; |
| 256 |
} |
| 257 |
float alphaF() const |
| 258 |
{ |
| 259 |
return ct.a; |
| 260 |
} |
| 261 |
void setRedF(float red) |
| 262 |
{ |
| 263 |
ct.r = red; |
| 264 |
} |
| 265 |
void setGreenF(float green) |
| 266 |
{ |
| 267 |
ct.g = green; |
| 268 |
} |
| 269 |
void setBlueF(float blue) |
| 270 |
{ |
| 271 |
ct.b = blue; |
| 272 |
} |
| 273 |
void setAlphaF(float alpha) |
| 274 |
{ |
| 275 |
ct.a = alpha; |
| 276 |
} |
| 277 |
int red() const |
| 278 |
{ |
| 279 |
return int(std::lround(ct.r * 255.0F)); |
| 280 |
} |
| 281 |
int green() const |
| 282 |
{ |
| 283 |
return int(std::lround(ct.g * 255.0F)); |
| 284 |
} |
| 285 |
int blue() const |
| 286 |
{ |
| 287 |
return int(std::lround(ct.b * 255.0F)); |
| 288 |
} |
| 289 |
int alpha() const |
| 290 |
{ |
| 291 |
return int(std::lround(ct.a * 255.0F)); |
| 292 |
} |
| 293 |
void setRed(int red) |
| 294 |
{ |
| 295 |
ct.r = static_cast<float>(red) / 255.0F; |
| 296 |
} |
| 297 |
void setGreen(int green) |
| 298 |
{ |
| 299 |
ct.g = static_cast<float>(green) / 255.0F; |
| 300 |
} |
| 301 |
void setBlue(int blue) |
| 302 |
{ |
| 303 |
ct.b = static_cast<float>(blue) / 255.0F; |
| 304 |
} |
| 305 |
void setAlpha(int alpha) |
| 306 |
{ |
| 307 |
ct.a = static_cast<float>(alpha) / 255.0F; |
| 308 |
} |
| 309 |
static color_type makeColor(int red, int green, int blue, int alpha = 255) |
| 310 |
{ |
| 311 |
return color_type { |
| 312 |
static_cast<float>(red) / 255.0F, |
| 313 |
static_cast<float>(green) / 255.0F, |
| 314 |
static_cast<float>(blue) / 255.0F, |
| 315 |
static_cast<float>(alpha) / 255.0F |
| 316 |
}; |
| 317 |
} |
| 318 |
|
| 319 |
private: |
| 320 |
color_type ct; |
| 321 |
}; |
| 322 |
|
| 323 |
} // namespace Base |
| 324 |
// NOLINTEND(readability-magic-numbers) |
| 325 |
|
| 326 |
#endif // APP_COLOR_H |