#include "coloredit.h" #include #include QColor fromVec3(glm::vec3 vec) { int r = 0, g = 0, b = 0; if(vec.x != 0) r = static_cast(255.0f * vec.x); if(vec.y != 0) g = static_cast(255.0f * vec.y); if(vec.z != 0) b = static_cast(255.0f * vec.z); return QColor::fromRgb(r, g, b); } glm::vec3 fromQColor(QColor color) { glm::vec3 vec; int r, g, b, a; color.getRgb(&r, &g, &b, &a); vec.x = r / 255.0f; vec.y = g / 255.0f; vec.z = b / 255.0f; return vec; } ColorEdit::ColorEdit(glm::vec3& ref, QWidget* parent) : QWidget(parent), reference(ref) { QHBoxLayout* itemsLayout = new QHBoxLayout(this); QPushButton* colorButton = new QPushButton(); colorButton->setFlat(true); QPalette pal = colorButton->palette(); pal.setColor(QPalette::Button, fromVec3(reference)); colorButton->setAutoFillBackground(true); colorButton->setPalette(pal); colorButton->update(); connect(colorButton, &QPushButton::clicked, [=](bool) { QColor oldcolor = fromVec3(reference); QColor newcolor = QColorDialog::getColor(oldcolor); reference = fromQColor(newcolor); QPalette newpal = colorButton->palette(); newpal.setColor(QPalette::Button, newcolor); colorButton->setPalette(newpal); colorButton->update(); emit onValueChanged(); }); itemsLayout->addWidget(colorButton); }