File size: 5,857 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2005 Jürgen Riegel <juergen.riegel@web.de>              *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/

#include <iomanip>
#include <sstream>

#include "Color.h"

using namespace Base;


// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
Color::Color(uint32_t rgba)
    : Color {}
{
    setPackedValue(rgba);
}

bool Color::operator==(const Color& color) const
{
    return getPackedValue() == color.getPackedValue();
}

bool Color::operator!=(const Color& color) const
{
    return !operator==(color);
}

// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
void Color::set(float red, float green, float blue, float alpha)
{
    r = red;
    g = green;
    b = blue;
    a = alpha;
}

float Color::transparency() const
{
    return 1.0F - a;
}

void Color::setTransparency(float value)
{
    a = 1.0F - value;
}

// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
Color& Color::setPackedValue(uint32_t rgba)
{
    // clang-format off
    this->set(static_cast<float> (rgba >> 24)         / 255.0F,
              static_cast<float>((rgba >> 16) & 0xff) / 255.0F,
              static_cast<float>((rgba >>  8) & 0xff) / 255.0F,
              static_cast<float> (rgba        & 0xff) / 255.0F);
    return *this;
    // clang-format on
}

uint32_t Color::getPackedValue() const
{
    // clang-format off
    return (static_cast<uint32_t>(std::lround(r * 255.0F)) << 24 |
            static_cast<uint32_t>(std::lround(g * 255.0F)) << 16 |
            static_cast<uint32_t>(std::lround(b * 255.0F)) << 8  |
            static_cast<uint32_t>(std::lround(a * 255.0F)));
    // clang-format on
}

void Color::setPackedRGB(uint32_t rgb)
{
    // clang-format off
    this->set(static_cast<float>((rgb >> 24) & 0xff) / 255.0F,
              static_cast<float>((rgb >> 16) & 0xff) / 255.0F,
              static_cast<float>((rgb >>  8) & 0xff) / 255.0F);
    // clang-format on
}

uint32_t Color::getPackedRGB() const
{
    // clang-format off
    return (static_cast<uint32_t>(std::lround(r * 255.0F)) << 24 |
            static_cast<uint32_t>(std::lround(g * 255.0F)) << 16 |
            static_cast<uint32_t>(std::lround(b * 255.0F)) << 8);
    // clang-format on
}

uint32_t Color::getPackedARGB() const
{
    // clang-format off
    return (static_cast<uint32_t>(std::lround(a * 255.0F)) << 24 |
            static_cast<uint32_t>(std::lround(r * 255.0F)) << 16 |
            static_cast<uint32_t>(std::lround(g * 255.0F)) << 8  |
            static_cast<uint32_t>(std::lround(b * 255.0F)));
    // clang-format on
}

void Color::setPackedARGB(uint32_t argb)
{
    // clang-format off
    this->set(static_cast<float>((argb >> 16) & 0xff) / 255.0F,
              static_cast<float>((argb >>  8) & 0xff) / 255.0F,
              static_cast<float> (argb        & 0xff) / 255.0F,
              static_cast<float> (argb >> 24)         / 255.0F);
    // clang-format on
}

std::string Color::asHexString() const
{
    // clang-format off
    std::stringstream ss;
    ss << "#" << std::hex << std::uppercase << std::setfill('0')
       << std::setw(2) << int(std::lround(r * 255.0F))
       << std::setw(2) << int(std::lround(g * 255.0F))
       << std::setw(2) << int(std::lround(b * 255.0F));
    return ss.str();
    // clang-format on
}

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

    // #RRGGBB
    if (hex.size() == 7) {
        std::stringstream ss(hex);
        unsigned int rgb;
        char ch {};

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

        r = rc / 255.0F;
        g = gc / 255.0F;
        b = bc / 255.0F;

        return true;
    }
    // #RRGGBBAA
    if (hex.size() == 9) {
        std::stringstream ss(hex);
        unsigned int rgba;
        char ch {};

        ss >> ch >> std::hex >> rgba;
        int rc = (rgba >> 24) & 0xff;
        int gc = (rgba >> 16) & 0xff;
        int bc = (rgba >> 8) & 0xff;
        int ac = rgba & 0xff;

        r = rc / 255.0F;
        g = gc / 255.0F;
        b = bc / 255.0F;
        a = ac / 255.0F;

        return true;
    }

    return false;
}
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)