From 345a6561251abde73c6155d7da49da20ab69b98c Mon Sep 17 00:00:00 2001 From: Rodrigo Olaya Moreno Date: Tue, 23 Dec 2025 21:20:16 -0500 Subject: [PATCH] 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 --- src/Mod/Material/Gui/BaseDelegate.cpp | 21 ++++++--------------- src/Mod/Material/Gui/BaseDelegate.h | 6 +++++- src/Mod/Material/Gui/MaterialDelegate.cpp | 3 +-- src/Mod/Material/Gui/MaterialDelegate.h | 1 - src/Mod/Material/Gui/MaterialTreeWidget.cpp | 1 - src/Mod/Material/Gui/MaterialsEditor.cpp | 18 ++++++++---------- src/Mod/Material/Gui/MaterialsEditor.h | 2 +- 7 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/Mod/Material/Gui/BaseDelegate.cpp b/src/Mod/Material/Gui/BaseDelegate.cpp index 5d2cc41a42..e8a9bbfc1f 100644 --- a/src/Mod/Material/Gui/BaseDelegate.cpp +++ b/src/Mod/Material/Gui/BaseDelegate.cpp @@ -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(); painter->fillRect(left, option.rect.top() + 5, width, option.rect.height() - 10, QBrush(color)); painter->restore(); diff --git a/src/Mod/Material/Gui/BaseDelegate.h b/src/Mod/Material/Gui/BaseDelegate.h index 7644d76175..616382a0fe 100644 --- a/src/Mod/Material/Gui/BaseDelegate.h +++ b/src/Mod/Material/Gui/BaseDelegate.h @@ -31,6 +31,8 @@ #include #include +#include + #include #include #include @@ -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, diff --git a/src/Mod/Material/Gui/MaterialDelegate.cpp b/src/Mod/Material/Gui/MaterialDelegate.cpp index 38003a1770..8b117e3824 100644 --- a/src/Mod/Material/Gui/MaterialDelegate.cpp +++ b/src/Mod/Material/Gui/MaterialDelegate.cpp @@ -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(); auto dlg = new QColorDialog(currentColor); dlg->setAttribute(Qt::WA_DeleteOnClose); diff --git a/src/Mod/Material/Gui/MaterialDelegate.h b/src/Mod/Material/Gui/MaterialDelegate.h index 1da289868b..af28ffc01e 100644 --- a/src/Mod/Material/Gui/MaterialDelegate.h +++ b/src/Mod/Material/Gui/MaterialDelegate.h @@ -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); diff --git a/src/Mod/Material/Gui/MaterialTreeWidget.cpp b/src/Mod/Material/Gui/MaterialTreeWidget.cpp index 8ec464a2f0..07648e9ba2 100644 --- a/src/Mod/Material/Gui/MaterialTreeWidget.cpp +++ b/src/Mod/Material/Gui/MaterialTreeWidget.cpp @@ -31,7 +31,6 @@ #include #include -#include #include #include #include diff --git a/src/Mod/Material/Gui/MaterialsEditor.cpp b/src/Mod/Material/Gui/MaterialsEditor.cpp index e536fc2e52..0cdbcf0ff0 100644 --- a/src/Mod/Material/Gui/MaterialsEditor.cpp +++ b/src/Mod/Material/Gui/MaterialsEditor.cpp @@ -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(red * colorRange), - static_cast(green * colorRange), - static_cast(blue * colorRange), - static_cast(alpha * colorRange)); - return color.name(); + Base::Color color(red, green, blue, alpha); + QColor qcolor = color.asValue(); + return qcolor.name(); } void MaterialsEditor::updateMaterialAppearance() diff --git a/src/Mod/Material/Gui/MaterialsEditor.h b/src/Mod/Material/Gui/MaterialsEditor.h index e80d61875a..9f74371653 100644 --- a/src/Mod/Material/Gui/MaterialsEditor.h +++ b/src/Mod/Material/Gui/MaterialsEditor.h @@ -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,