add a color picker dialog to palette editor

This commit is contained in:
garak
2022-06-17 15:04:12 -04:00
committed by garakmon
parent 11661818b6
commit 45cb2a19af
7 changed files with 2079 additions and 1163 deletions

81
src/ui/colorpicker.cpp Normal file
View File

@@ -0,0 +1,81 @@
#include "colorpicker.h"
#include "ui_colorpicker.h"
#include <QtWidgets>
const int zoom_box_dimensions = 7;
ColorPicker::ColorPicker(QWidget *parent) :
QDialog(parent),
ui(new Ui::ColorPicker)
{
ui->setupUi(this);
this->scene = new QGraphicsScene;
// listen for spacebar press to take color
QShortcut *takeColor = new QShortcut(Qt::Key_Space, this);
QObject::connect(takeColor, &QShortcut::activated, [this](){
timer->stop();
this->accept();
});
// need to set up a timer because there is no good way to get global mouse movement
// outside of the application in a cross-platform way
timer = new QTimer(this);
connect(timer, &QTimer::timeout, [this]() {
QPoint cursorPos = QCursor::pos();
if (lastCursorPos != cursorPos) {
lastCursorPos = cursorPos;
this->hover(cursorPos.x(), cursorPos.y());
}
});
timer->start(10);
}
ColorPicker::~ColorPicker()
{
delete scene;
delete timer;
delete ui;
}
void ColorPicker::hover(int mouseX, int mouseY) {
QScreen *screen = QGuiApplication::primaryScreen();
if (const QWindow *window = windowHandle())
screen = window->screen();
if (!screen)
return;
// 15 X 15 box with 8x magnification = 120px square)
QRect zoomRect(mouseX - 7, mouseY - 7, 15, 15);
QPixmap grab = screen->grabWindow(0, mouseX - 7, mouseY - 7, 15, 15);
int pixelRatio = grab.devicePixelRatio();
// TODO: investigate for high dpi displays why text is too high res
grab.setDevicePixelRatio(1);
QPixmap magnified = grab.scaled(120, 120, Qt::KeepAspectRatio);
QPainter painter(&magnified);
painter.setRenderHint(QPainter::Antialiasing, false);
QRectF rect(55, 55, 9, 9);
painter.drawRect(rect);
painter.end();
// TODO: bounds checking?
this->color = grab.toImage().pixelColor(7 * pixelRatio, 7 * pixelRatio);
int r = this->color.red();
int g = this->color.green();
int b = this->color.blue();
// update the displayed color value
QString rgb = QString("rgb(%1, %2, %3)").arg(r).arg(g).arg(b);
QString stylesheet = QString("background-color: %1;").arg(rgb);
this->ui->frame_centralColor->setStyleSheet(stylesheet);
this->ui->label_RGB->setText(rgb);
this->ui->label_HEX->setText(color.name());
this->ui->viewport->setPixmap(magnified);
}

View File

@@ -1,8 +1,10 @@
#include "paletteeditor.h"
#include "ui_paletteeditor.h"
#include "colorpicker.h"
#include "paletteutil.h"
#include "config.h"
#include "log.h"
#include <QFileDialog>
#include <QMessageBox>
@@ -105,11 +107,40 @@ PaletteEditor::PaletteEditor(Project *project, Tileset *primaryTileset, Tileset
this->rgbLabels.append(this->ui->label_rgb14);
this->rgbLabels.append(this->ui->label_rgb15);
this->pickButtons.clear();
this->pickButtons.append(this->ui->toolButton_0);
this->pickButtons.append(this->ui->toolButton_1);
this->pickButtons.append(this->ui->toolButton_2);
this->pickButtons.append(this->ui->toolButton_3);
this->pickButtons.append(this->ui->toolButton_4);
this->pickButtons.append(this->ui->toolButton_5);
this->pickButtons.append(this->ui->toolButton_6);
this->pickButtons.append(this->ui->toolButton_7);
this->pickButtons.append(this->ui->toolButton_8);
this->pickButtons.append(this->ui->toolButton_9);
this->pickButtons.append(this->ui->toolButton_10);
this->pickButtons.append(this->ui->toolButton_11);
this->pickButtons.append(this->ui->toolButton_12);
this->pickButtons.append(this->ui->toolButton_13);
this->pickButtons.append(this->ui->toolButton_14);
this->pickButtons.append(this->ui->toolButton_15);
// Setup edit-undo history for each of the palettes.
for (int i = 0; i < Project::getNumPalettesTotal(); i++) {
this->palettesHistory.append(History<PaletteHistoryItem*>());
}
// Connect the color picker's selection to the correct color index
for (int i = 0; i < 16; i++) {
connect(this->pickButtons[i], &QToolButton::clicked, [this, i](){
disableSliderSignals();
this->pickColor(i);
this->setColor(i);
enableSliderSignals();
});
this->pickButtons[i]->setEnabled(i < (Project::getNumPalettesTotal() - 1));
}
this->initColorSliders();
this->setPaletteId(paletteId);
this->commitEditHistory(this->ui->spinBox_PaletteId->value());
@@ -211,6 +242,18 @@ void PaletteEditor::setColor(int colorIndex) {
emit this->changedPaletteColor();
}
void PaletteEditor::pickColor(int i) {
ColorPicker picker(this);
if (picker.exec() == QDialog::Accepted) {
QColor c = picker.getColor();
// TODO: round? or keep floor?
this->sliders[i][0]->setValue(c.red() / 8);
this->sliders[i][1]->setValue(c.green() / 8);
this->sliders[i][2]->setValue(c.blue() / 8);
}
return;
}
void PaletteEditor::on_spinBox_PaletteId_valueChanged(int paletteId) {
this->refreshColorSliders();
this->refreshColors();