FreeCAD

Форк
0
/
Color.cpp 
187 строк · 5.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2005 Jürgen Riegel <juergen.riegel@web.de>              *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
#include <iomanip>
28
#include <sstream>
29
#endif
30

31
#include "Color.h"
32

33
using namespace App;
34

35

36
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
37
Color::Color(float red, float green, float blue, float alpha)
38
  : r(red)
39
  , g(green)
40
  , b(blue)
41
  , a(alpha)
42
{
43
}
44

45
Color::Color(uint32_t rgba)
46
  : Color{}
47
{
48
    setPackedValue(rgba);
49
}
50

51
bool Color::operator==(const Color& color) const
52
{
53
    return getPackedValue() == color.getPackedValue();
54
}
55

56
bool Color::operator!=(const Color& color) const
57
{
58
    return !operator==(color);
59
}
60

61
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
62
void Color::set(float red, float green, float blue, float alpha)
63
{
64
    r = red;
65
    g = green;
66
    b = blue;
67
    a = alpha;
68
}
69

70
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
71
Color& Color::setPackedValue(uint32_t rgba)
72
{
73
    // clang-format off
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);
78
    return *this;
79
    // clang-format on
80
}
81

82
uint32_t Color::getPackedValue() const
83
{
84
    // clang-format off
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)));
89
    // clang-format on
90
}
91

92
void Color::setPackedRGB(uint32_t rgb)
93
{
94
    // clang-format off
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);
98
    // clang-format on
99
}
100

101
uint32_t Color::getPackedRGB() const
102
{
103
    // clang-format off
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);
107
    // clang-format on
108
}
109

110
uint32_t Color::getPackedARGB() const
111
{
112
    // clang-format off
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)));
117
    // clang-format on
118
}
119

120
void Color::setPackedARGB(uint32_t argb)
121
{
122
    // clang-format off
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);
127
    // clang-format on
128
}
129

130
std::string Color::asHexString() const
131
{
132
    // clang-format off
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));
138
    return ss.str();
139
    // clang-format on
140
}
141

142
bool Color::fromHexString(const std::string& hex)
143
{
144
    if (hex.size() < 7 || hex[0] != '#') {
145
        return false;
146
    }
147

148
    // #RRGGBB
149
    if (hex.size() == 7) {
150
        std::stringstream ss(hex);
151
        unsigned int rgb;
152
        char ch{};
153

154
        ss >> ch >> std::hex >> rgb;
155
        int rc = (rgb >> 16) & 0xff;
156
        int gc = (rgb >> 8) & 0xff;
157
        int bc = rgb & 0xff;
158

159
        r = rc / 255.0F;
160
        g = gc / 255.0F;
161
        b = bc / 255.0F;
162

163
        return true;
164
    }
165
    // #RRGGBBAA
166
    if (hex.size() == 9) {
167
        std::stringstream ss(hex);
168
        unsigned int rgba;
169
        char ch{};
170

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;
176

177
        r = rc / 255.0F;
178
        g = gc / 255.0F;
179
        b = bc / 255.0F;
180
        a = ac / 255.0F;
181

182
        return true;
183
    }
184

185
    return false;
186
}
187
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
188

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.