1
/***************************************************************************
2
* Copyright (c) 2005 Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
36
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
37
Color::Color(float red, float green, float blue, float alpha)
45
Color::Color(uint32_t rgba)
51
bool Color::operator==(const Color& color) const
53
return getPackedValue() == color.getPackedValue();
56
bool Color::operator!=(const Color& color) const
58
return !operator==(color);
61
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
62
void Color::set(float red, float green, float blue, float alpha)
70
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
71
Color& Color::setPackedValue(uint32_t rgba)
74
this->set(static_cast<float> (rgba >> 24) / 255.0F,
75
static_cast<float>((rgba >> 16) & 0xff) / 255.0F,
76
static_cast<float>((rgba >> 8) & 0xff) / 255.0F,
77
static_cast<float> (rgba & 0xff) / 255.0F);
82
uint32_t Color::getPackedValue() const
85
return (static_cast<uint32_t>(std::lround(r * 255.0F)) << 24 |
86
static_cast<uint32_t>(std::lround(g * 255.0F)) << 16 |
87
static_cast<uint32_t>(std::lround(b * 255.0F)) << 8 |
88
static_cast<uint32_t>(std::lround(a * 255.0F)));
92
void Color::setPackedRGB(uint32_t rgb)
95
this->set(static_cast<float>((rgb >> 24) & 0xff) / 255.0F,
96
static_cast<float>((rgb >> 16) & 0xff) / 255.0F,
97
static_cast<float>((rgb >> 8) & 0xff) / 255.0F);
101
uint32_t Color::getPackedRGB() const
104
return (static_cast<uint32_t>(std::lround(r * 255.0F)) << 24 |
105
static_cast<uint32_t>(std::lround(g * 255.0F)) << 16 |
106
static_cast<uint32_t>(std::lround(b * 255.0F)) << 8);
110
uint32_t Color::getPackedARGB() const
113
return (static_cast<uint32_t>(std::lround(a * 255.0F)) << 24 |
114
static_cast<uint32_t>(std::lround(r * 255.0F)) << 16 |
115
static_cast<uint32_t>(std::lround(g * 255.0F)) << 8 |
116
static_cast<uint32_t>(std::lround(b * 255.0F)));
120
void Color::setPackedARGB(uint32_t argb)
123
this->set(static_cast<float>((argb >> 16) & 0xff) / 255.0F,
124
static_cast<float>((argb >> 8) & 0xff) / 255.0F,
125
static_cast<float> (argb & 0xff) / 255.0F,
126
static_cast<float> (argb >> 24) / 255.0F);
130
std::string Color::asHexString() const
133
std::stringstream ss;
134
ss << "#" << std::hex << std::uppercase << std::setfill('0')
135
<< std::setw(2) << int(std::lround(r * 255.0F))
136
<< std::setw(2) << int(std::lround(g * 255.0F))
137
<< std::setw(2) << int(std::lround(b * 255.0F));
142
bool Color::fromHexString(const std::string& hex)
144
if (hex.size() < 7 || hex[0] != '#') {
149
if (hex.size() == 7) {
150
std::stringstream ss(hex);
154
ss >> ch >> std::hex >> rgb;
155
int rc = (rgb >> 16) & 0xff;
156
int gc = (rgb >> 8) & 0xff;
166
if (hex.size() == 9) {
167
std::stringstream ss(hex);
171
ss >> ch >> std::hex >> rgba;
172
int rc = (rgba >> 24) & 0xff;
173
int gc = (rgba >> 16) & 0xff;
174
int bc = (rgba >> 8) & 0xff;
175
int ac = rgba & 0xff;
187
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)