Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/duckstation-qt/colorpickerbutton.cpp
4246 views
1
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#include "colorpickerbutton.h"
5
#include "qtutils.h"
6
7
#include <QtWidgets/QColorDialog>
8
9
#include "moc_colorpickerbutton.cpp"
10
11
ColorPickerButton::ColorPickerButton(QWidget* parent) : QPushButton(parent)
12
{
13
connect(this, &QPushButton::clicked, this, &ColorPickerButton::onClicked);
14
updateBackgroundColor();
15
}
16
17
u32 ColorPickerButton::color()
18
{
19
return m_color;
20
}
21
22
void ColorPickerButton::setColor(u32 rgb)
23
{
24
if (m_color == rgb)
25
return;
26
27
m_color = rgb;
28
updateBackgroundColor();
29
}
30
31
void ColorPickerButton::updateBackgroundColor()
32
{
33
setStyleSheet(QStringLiteral("background-color: #%1;").arg(static_cast<uint>(m_color), 8, 16, QChar('0')));
34
}
35
36
void ColorPickerButton::onClicked()
37
{
38
const u32 red = (m_color >> 16) & 0xff;
39
const u32 green = (m_color >> 8) & 0xff;
40
const u32 blue = m_color & 0xff;
41
42
const QColor initial(QColor::fromRgb(red, green, blue));
43
const QColor selected(QColorDialog::getColor(initial, QtUtils::GetRootWidget(this), tr("Select LED Color")));
44
45
// QColorDialog returns Invalid on cancel, and apparently initial == Invalid is true...
46
if (!selected.isValid() || initial == selected)
47
return;
48
49
const u32 new_rgb = (static_cast<u32>(selected.red()) << 16) | (static_cast<u32>(selected.green()) << 8) |
50
static_cast<u32>(selected.blue());
51
m_color = new_rgb;
52
updateBackgroundColor();
53
emit colorChanged(new_rgb);
54
}
55
56