Path: blob/master/src/duckstation-qt/colorpickerbutton.cpp
4246 views
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <[email protected]>1// SPDX-License-Identifier: CC-BY-NC-ND-4.023#include "colorpickerbutton.h"4#include "qtutils.h"56#include <QtWidgets/QColorDialog>78#include "moc_colorpickerbutton.cpp"910ColorPickerButton::ColorPickerButton(QWidget* parent) : QPushButton(parent)11{12connect(this, &QPushButton::clicked, this, &ColorPickerButton::onClicked);13updateBackgroundColor();14}1516u32 ColorPickerButton::color()17{18return m_color;19}2021void ColorPickerButton::setColor(u32 rgb)22{23if (m_color == rgb)24return;2526m_color = rgb;27updateBackgroundColor();28}2930void ColorPickerButton::updateBackgroundColor()31{32setStyleSheet(QStringLiteral("background-color: #%1;").arg(static_cast<uint>(m_color), 8, 16, QChar('0')));33}3435void ColorPickerButton::onClicked()36{37const u32 red = (m_color >> 16) & 0xff;38const u32 green = (m_color >> 8) & 0xff;39const u32 blue = m_color & 0xff;4041const QColor initial(QColor::fromRgb(red, green, blue));42const QColor selected(QColorDialog::getColor(initial, QtUtils::GetRootWidget(this), tr("Select LED Color")));4344// QColorDialog returns Invalid on cancel, and apparently initial == Invalid is true...45if (!selected.isValid() || initial == selected)46return;4748const u32 new_rgb = (static_cast<u32>(selected.red()) << 16) | (static_cast<u32>(selected.green()) << 8) |49static_cast<u32>(selected.blue());50m_color = new_rgb;51updateBackgroundColor();52emit colorChanged(new_rgb);53}545556