Fix RGB rounding bug in material appearance editor (#26134)

* Fix RGB rounding bug in material appearance editor
* Update code to use color
* getColorHash no longer needs range
This commit is contained in:
Rodrigo Olaya Moreno
2025-12-23 21:20:16 -05:00
committed by GitHub
parent eb61ee36a6
commit 345a656125
7 changed files with 21 additions and 31 deletions

View File

@@ -72,24 +72,17 @@ QString BaseDelegate::getStringValue(const QModelIndex& index) const
return propertyValue;
}
QRgb BaseDelegate::parseColor(const QString& color) const
Color BaseDelegate::parseColor(const QString& color) const
{
QString trimmed = color;
trimmed.replace(QRegularExpression(QStringLiteral("\\(([^<]*)\\)")),
QStringLiteral("\\1"));
QStringList parts = trimmed.split(QStringLiteral(","));
if (parts.length() < 3) {
return qRgba(0, 0, 0, 255);
return Color();
}
int red = parts.at(0).toDouble() * 255;
int green = parts.at(1).toDouble() * 255;
int blue = parts.at(2).toDouble() * 255;
int alpha = 255;
if (parts.length() > 3) {
alpha = parts.at(3).toDouble() * 255;
}
return qRgba(red, green, blue, alpha);
return Color(parts.at(0).toDouble(), parts.at(1).toDouble(), parts.at(2).toDouble(),
parts.length() > 3 ? parts.at(3).toDouble() : 1.0);
}
void BaseDelegate::paintQuantity(QPainter* painter,
@@ -164,23 +157,21 @@ void BaseDelegate::paintColor(QPainter* painter,
auto propertyValue = getStringValue(index);
painter->save();
QColor color;
color.setRgba(qRgba(0, 0, 0, 255)); // Black border
int left = option.rect.left() + 2;
int width = option.rect.width() - 4;
if (option.rect.width() > 75) {
left += (option.rect.width() - 75) / 2;
width = 71;
}
painter->fillRect(left, option.rect.top() + 2, width, option.rect.height() - 4, QBrush(color));
painter->fillRect(left, option.rect.top() + 2, width, option.rect.height() - 4, QBrush(Qt::black));
color.setRgba(parseColor(propertyValue));
left = option.rect.left() + 5;
width = option.rect.width() - 10;
if (option.rect.width() > 75) {
left += (option.rect.width() - 75) / 2;
width = 65;
}
auto color = parseColor(propertyValue).asValue<QColor>();
painter->fillRect(left, option.rect.top() + 5, width, option.rect.height() - 10, QBrush(color));
painter->restore();

View File

@@ -31,6 +31,8 @@
#include <QSvgWidget>
#include <QTreeView>
#include <Base/Color.h>
#include <Mod/Material/App/MaterialManager.h>
#include <Mod/Material/App/Materials.h>
#include <Mod/Material/App/ModelManager.h>
@@ -38,6 +40,8 @@
namespace MatGui
{
using Base::Color;
class BaseDelegate: public QStyledItemDelegate
{
Q_OBJECT
@@ -70,7 +74,7 @@ protected:
virtual void notifyChanged(const QAbstractItemModel* model, const QModelIndex& index) const = 0;
QString getStringValue(const QModelIndex& index) const;
QRgb parseColor(const QString& color) const;
Color parseColor(const QString& color) const;
void paintQuantity(QPainter* painter,
const QStyleOptionViewItem& option,

View File

@@ -259,8 +259,7 @@ bool MaterialDelegate::editorEvent(QEvent* event,
void MaterialDelegate::showColorModal(const QString& propertyName, QStandardItem* item)
{
QColor currentColor; // = d->col;
currentColor.setRgba(parseColor(item->text()));
auto currentColor = parseColor(item->text()).asValue<QColor>();
auto dlg = new QColorDialog(currentColor);
dlg->setAttribute(Qt::WA_DeleteOnClose);

View File

@@ -80,7 +80,6 @@ Q_SIGNALS:
private:
QWidget* createWidget(QWidget* parent, const QVariant& item, const QModelIndex& index) const;
// QRgb parseColor(const QString& color) const;
void showColorModal(const QString& propertyName, QStandardItem* item);
void showImageModal(const QString& propertyName, QStandardItem* item);
void showListModal(const QString& propertyName, QStandardItem* item);

View File

@@ -31,7 +31,6 @@
#include <QSpacerItem>
#include <QVBoxLayout>
#include <Base/Color.h>
#include <Base/Console.h>
#include <Base/Tools.h>
#include <Gui/Command.h>

View File

@@ -995,28 +995,28 @@ bool MaterialsEditor::updateMaterialPreview() const
{
if (_material->hasAppearanceProperty(QStringLiteral("AmbientColor"))) {
QString color = _material->getAppearanceValueString(QStringLiteral("AmbientColor"));
_rendered->setAmbientColor(getColorHash(color, 255));
_rendered->setAmbientColor(getColorHash(color));
}
else {
_rendered->resetAmbientColor();
}
if (_material->hasAppearanceProperty(QStringLiteral("DiffuseColor"))) {
QString color = _material->getAppearanceValueString(QStringLiteral("DiffuseColor"));
_rendered->setDiffuseColor(getColorHash(color, 255));
_rendered->setDiffuseColor(getColorHash(color));
}
else {
_rendered->resetDiffuseColor();
}
if (_material->hasAppearanceProperty(QStringLiteral("SpecularColor"))) {
QString color = _material->getAppearanceValueString(QStringLiteral("SpecularColor"));
_rendered->setSpecularColor(getColorHash(color, 255));
_rendered->setSpecularColor(getColorHash(color));
}
else {
_rendered->resetSpecularColor();
}
if (_material->hasAppearanceProperty(QStringLiteral("EmissiveColor"))) {
QString color = _material->getAppearanceValueString(QStringLiteral("EmissiveColor"));
_rendered->setEmissiveColor(getColorHash(color, 255));
_rendered->setEmissiveColor(getColorHash(color));
}
else {
_rendered->resetEmissiveColor();
@@ -1047,7 +1047,7 @@ void MaterialsEditor::updatePreview() const
updateMaterialPreview();
}
QString MaterialsEditor::getColorHash(const QString& colorString, int colorRange)
QString MaterialsEditor::getColorHash(const QString& colorString)
{
/*
returns a '#000000' string from a '(0.1,0.2,0.3)' string. Optionally the string
@@ -1071,11 +1071,9 @@ QString MaterialsEditor::getColorHash(const QString& colorString, int colorRange
stream >> alpha;
}
QColor color(static_cast<int>(red * colorRange),
static_cast<int>(green * colorRange),
static_cast<int>(blue * colorRange),
static_cast<int>(alpha * colorRange));
return color.name();
Base::Color color(red, green, blue, alpha);
QColor qcolor = color.asValue<QColor>();
return qcolor.name();
}
void MaterialsEditor::updateMaterialAppearance()

View File

@@ -155,7 +155,7 @@ private:
bool updateTexturePreview() const;
bool updateMaterialPreview() const;
void updatePreview() const;
static QString getColorHash(const QString& colorString, int colorRange = 255);
static QString getColorHash(const QString& colorString);
static void addExpanded(QTreeView* tree, QStandardItem* parent, QStandardItem* child);
static void addExpanded(QTreeView* tree,