Debian Codegraph @

src/Base/Color.cpp

raw
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 #include <iomanip>
26 #include <sstream>
27
28 #include "Color.h"
29
30 using namespace Base;
31
32
33 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
34 Color::Color(uint32_t rgba)
35 : Color {}
36 {
37 setPackedValue(rgba);
38 }
39
40 bool Color::operator==(const Color& color) const
41 {
42 return getPackedValue() == color.getPackedValue();
43 }
44
45 bool Color::operator!=(const Color& color) const
46 {
47 return !operator==(color);
48 }
49
50 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
51 void Color::set(float red, float green, float blue, float alpha)
52 {
53 r = red;
54 g = green;
55 b = blue;
56 a = alpha;
57 }
58
59 float Color::transparency() const
60 {
61 return 1.0F - a;
62 }
63
64 void Color::setTransparency(float value)
65 {
66 a = 1.0F - value;
67 }
68
69 // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
70 Color& Color::setPackedValue(uint32_t rgba)
71 {
72 // clang-format off
73 this->set(static_cast<float> (rgba >> 24) / 255.0F,
74 static_cast<float>((rgba >> 16) & 0xff) / 255.0F,
75 static_cast<float>((rgba >> 8) & 0xff) / 255.0F,
76 static_cast<float> (rgba & 0xff) / 255.0F);
77 return *this;
78 // clang-format on
79 }
80
81 uint32_t Color::getPackedValue() const
82 {
83 // clang-format off
84 return (static_cast<uint32_t>(std::lround(r * 255.0F)) << 24 |
85 static_cast<uint32_t>(std::lround(g * 255.0F)) << 16 |
86 static_cast<uint32_t>(std::lround(b * 255.0F)) << 8 |
87 static_cast<uint32_t>(std::lround(a * 255.0F)));
88 // clang-format on
89 }
90
91 void Color::setPackedRGB(uint32_t rgb)
92 {
93 // clang-format off
94 this->set(static_cast<float>((rgb >> 24) & 0xff) / 255.0F,
95 static_cast<float>((rgb >> 16) & 0xff) / 255.0F,
96 static_cast<float>((rgb >> 8) & 0xff) / 255.0F);
97 // clang-format on
98 }
99
100 uint32_t Color::getPackedRGB() const
101 {
102 // clang-format off
103 return (static_cast<uint32_t>(std::lround(r * 255.0F)) << 24 |
104 static_cast<uint32_t>(std::lround(g * 255.0F)) << 16 |
105 static_cast<uint32_t>(std::lround(b * 255.0F)) << 8);
106 // clang-format on
107 }
108
109 uint32_t Color::getPackedARGB() const
110 {
111 // clang-format off
112 return (static_cast<uint32_t>(std::lround(a * 255.0F)) << 24 |
113 static_cast<uint32_t>(std::lround(r * 255.0F)) << 16 |
114 static_cast<uint32_t>(std::lround(g * 255.0F)) << 8 |
115 static_cast<uint32_t>(std::lround(b * 255.0F)));
116 // clang-format on
117 }
118
119 void Color::setPackedARGB(uint32_t argb)
120 {
121 // clang-format off
122 this->set(static_cast<float>((argb >> 16) & 0xff) / 255.0F,
123 static_cast<float>((argb >> 8) & 0xff) / 255.0F,
124 static_cast<float> (argb & 0xff) / 255.0F,
125 static_cast<float> (argb >> 24) / 255.0F);
126 // clang-format on
127 }
128
129 std::string Color::asHexString() const
130 {
131 // clang-format off
132 std::stringstream ss;
133 ss << "#" << std::hex << std::uppercase << std::setfill('0')
134 << std::setw(2) << int(std::lround(r * 255.0F))
135 << std::setw(2) << int(std::lround(g * 255.0F))
136 << std::setw(2) << int(std::lround(b * 255.0F));
137 return ss.str();
138 // clang-format on
139 }
140
141 bool Color::fromHexString(const std::string& hex)
142 {
143 if (hex.size() < 7 || hex[0] != '#') {
144 return false;
145 }
146
147 // #RRGGBB
148 if (hex.size() == 7) {
149 std::stringstream ss(hex);
150 unsigned int rgb;
151 char ch {};
152
153 ss >> ch >> std::hex >> rgb;
154 int rc = (rgb >> 16) & 0xff;
155 int gc = (rgb >> 8) & 0xff;
156 int bc = rgb & 0xff;
157
158 r = rc / 255.0F;
159 g = gc / 255.0F;
160 b = bc / 255.0F;
161
162 return true;
163 }
164 // #RRGGBBAA
165 if (hex.size() == 9) {
166 std::stringstream ss(hex);
167 unsigned int rgba;
168 char ch {};
169
170 ss >> ch >> std::hex >> rgba;
171 int rc = (rgba >> 24) & 0xff;
172 int gc = (rgba >> 16) & 0xff;
173 int bc = (rgba >> 8) & 0xff;
174 int ac = rgba & 0xff;
175
176 r = rc / 255.0F;
177 g = gc / 255.0F;
178 b = bc / 255.0F;
179 a = ac / 255.0F;
180
181 return true;
182 }
183
184 return false;
185 }
186 // NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)