diff --git a/src/App/Color.cpp b/src/App/Color.cpp index 64f44e3468..1d74c335f3 100644 --- a/src/App/Color.cpp +++ b/src/App/Color.cpp @@ -24,8 +24,8 @@ #include "PreCompiled.h" #ifndef _PreComp_ -#include #include +#include #endif #include "Color.h" @@ -49,7 +49,6 @@ Color::Color(uint32_t rgba) bool Color::operator==(const Color& c) const { return getPackedValue() == c.getPackedValue(); - //return (c.r==r && c.g==g && c.b==b && c.a==a); } bool Color::operator!=(const Color& c) const @@ -57,7 +56,7 @@ bool Color::operator!=(const Color& c) const return !operator==(c); } -void Color::set(float R,float G, float B, float A) +void Color::set(float R, float G, float B, float A) { r = R; g = G; @@ -90,6 +89,16 @@ uint32_t Color::getPackedARGB() const static_cast(b*255.0f + 0.5f)); } +void Color::setPackedARGB(uint32_t argb) +{ + // clang-format off + this->set(((argb >> 16) & 0xff) / 255.0f, + ((argb >> 8) & 0xff) / 255.0f, + (argb & 0xff) / 255.0f, + (argb >> 24) / 255.0f); + // clang-format on +} + std::string Color::asHexString() const { std::stringstream ss; diff --git a/src/App/Color.h b/src/App/Color.h index 7aba40d0ba..2cff05d547 100644 --- a/src/App/Color.h +++ b/src/App/Color.h @@ -83,20 +83,37 @@ public: * Returns color as a 32 bit packed unsigned int in the form 0xAARRGGBB. */ uint32_t getPackedARGB() const; + /** + * Sets color as a 32 bit packed unsigned int in the form 0xAARRGGBB. + */ + void setPackedARGB(uint32_t); + template static uint32_t asPackedRGBA(const T& color) { return (color.red() << 24) | (color.green() << 16) | (color.blue() << 8) | color.alpha(); } + + template + static T fromPackedRGBA(uint32_t color) { + return T((color >> 24) & 0xff, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff); + } + template static uint32_t asPackedRGB(const T& color) { return (color.red() << 24) | (color.green() << 16) | (color.blue() << 8); } + + template + static T fromPackedRGB(uint32_t color) { + return T((color >> 24) & 0xff, (color >> 16) & 0xff, (color >> 8) & 0xff); + } /** * creates FC Color from template type, e.g. Qt QColor */ template - void setValue(const T& q) - { set(q.redF(),q.greenF(),q.blueF()); } + void setValue(const T& q) { + set(q.redF(),q.greenF(),q.blueF()); + } /** * returns a template type e.g. Qt color equivalent to FC color * diff --git a/src/Gui/DlgEditorImp.cpp b/src/Gui/DlgEditorImp.cpp index ebb8688101..563602d3af 100644 --- a/src/Gui/DlgEditorImp.cpp +++ b/src/Gui/DlgEditorImp.cpp @@ -184,7 +184,7 @@ void DlgSettingsEditorImp::on_displayItems_currentItemChanged(QTreeWidgetItem *i { int index = ui->displayItems->indexOfTopLevelItem(item); unsigned int col = d->colormap[index].second; - ui->colorButton->setColor(QColor((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff)); + ui->colorButton->setColor(App::Color::fromPackedRGB(col)); } /** Updates the color map if a color was changed */ @@ -263,8 +263,7 @@ void DlgSettingsEditorImp::loadSettings() auto col = static_cast((*it).second); col = hGrp->GetUnsigned((*it).first.toLatin1(), col); (*it).second = static_cast(col); - QColor color; - color.setRgb((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff); + QColor color = App::Color::fromPackedRGB(col); pythonSyntax->setColor((*it).first, color); } diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index 37c1a97832..316cbefb91 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -102,6 +102,7 @@ #include "View3DInventorViewer.h" #include "DlgObjectSelection.h" #include "Tools.h" +#include FC_LOG_LEVEL_INIT("MainWindow",false,true,true) @@ -2124,15 +2125,15 @@ void StatusBarObserver::OnChange(Base::Subject &rCaller, const char auto format = QString::fromLatin1("#statusBar{color: %1}"); if (strcmp(sReason, "colorText") == 0) { unsigned long col = rclGrp.GetUnsigned( sReason ); - this->msg = format.arg(QColor((col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff).name()); + this->msg = format.arg(App::Color::fromPackedRGB(col).name()); } else if (strcmp(sReason, "colorWarning") == 0) { unsigned long col = rclGrp.GetUnsigned( sReason ); - this->wrn = format.arg(QColor((col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff).name()); + this->wrn = format.arg(App::Color::fromPackedRGB(col).name()); } else if (strcmp(sReason, "colorError") == 0) { unsigned long col = rclGrp.GetUnsigned( sReason ); - this->err = format.arg(QColor((col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff).name()); + this->err = format.arg(App::Color::fromPackedRGB(col).name()); } } diff --git a/src/Gui/PrefWidgets.cpp b/src/Gui/PrefWidgets.cpp index 0917495208..35e6fda65b 100644 --- a/src/Gui/PrefWidgets.cpp +++ b/src/Gui/PrefWidgets.cpp @@ -561,13 +561,10 @@ void PrefColorButton::restorePreferences() unsigned long lcol = static_cast(icol); lcol = getWindowParameter()->GetUnsigned( entryName(), lcol ); icol = static_cast(lcol); - int r = (icol >> 24)&0xff; - int g = (icol >> 16)&0xff; - int b = (icol >> 8)&0xff; - int a = (icol )&0xff; + QColor value = App::Color::fromPackedRGB(icol); if (!this->allowTransparency()) - a = 0xff; - setColor(QColor(r,g,b,a)); + value.setAlpha(0xff); + setColor(value); } void PrefColorButton::savePreferences() diff --git a/src/Gui/ReportView.cpp b/src/Gui/ReportView.cpp index dff29ef9a9..d8f5795910 100644 --- a/src/Gui/ReportView.cpp +++ b/src/Gui/ReportView.cpp @@ -763,19 +763,19 @@ void ReportOutput::OnChange(Base::Subject &rCaller, const char * sR } else if (strcmp(sReason, "colorText") == 0) { unsigned long col = rclGrp.GetUnsigned( sReason ); - reportHl->setTextColor( QColor( (col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff) ); + reportHl->setTextColor(App::Color::fromPackedRGB(col)); } else if (strcmp(sReason, "colorLogging") == 0) { unsigned long col = rclGrp.GetUnsigned( sReason ); - reportHl->setLogColor( QColor( (col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff) ); + reportHl->setLogColor(App::Color::fromPackedRGB(col)); } else if (strcmp(sReason, "colorWarning") == 0) { unsigned long col = rclGrp.GetUnsigned( sReason ); - reportHl->setWarningColor( QColor( (col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff) ); + reportHl->setWarningColor(App::Color::fromPackedRGB(col)); } else if (strcmp(sReason, "colorError") == 0) { unsigned long col = rclGrp.GetUnsigned( sReason ); - reportHl->setErrorColor( QColor( (col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff) ); + reportHl->setErrorColor(App::Color::fromPackedRGB(col)); } else if (strcmp(sReason, "checkGoToEnd") == 0) { gotoEnd = rclGrp.GetBool(sReason, gotoEnd); diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index ea2edb584d..2ed813925c 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -44,6 +44,7 @@ #include #include +#include #include #include #include @@ -3238,7 +3239,7 @@ void DocumentItem::slotInEdit(const Gui::ViewProviderDocumentObject& v) ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( "User parameter:BaseApp/Preferences/TreeView"); unsigned long col = hGrp->GetUnsigned("TreeEditColor", 4294902015); - QColor color((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff); + QColor color(App::Color::fromPackedRGB(col)); if (!getTree()->editingItem) { auto doc = Application::Instance->editDocument(); @@ -4618,7 +4619,7 @@ void DocumentObjectItem::setHighlight(bool set, Gui::HighlightMode high) { f.setOverline(overlined); unsigned long col = hGrp->GetUnsigned("TreeActiveColor", 3873898495); - color = QColor((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff); + color = App::Color::fromPackedRGB(col); } else { f.setBold(false); diff --git a/src/Gui/Widgets.cpp b/src/Gui/Widgets.cpp index 5e5be5bfc5..8f84982e95 100644 --- a/src/Gui/Widgets.cpp +++ b/src/Gui/Widgets.cpp @@ -1020,7 +1020,7 @@ void StatefulLabel::setState(QString state) if (auto entry = _availableStates.find(state); entry != _availableStates.end()) { // Order of precedence: first, check if the user has set this in their preferences: - if (!entry->second.preferenceString.empty()) { + if (!entry->second.preferenceString.empty()) { // First, try to see if it's just stored a color (as an unsigned int): auto availableColorPrefs = _parameterGroup->GetUnsignedMap(); std::string lookingForGroup = entry->second.preferenceString; @@ -1029,7 +1029,7 @@ void StatefulLabel::setState(QString state) if (unsignedEntry.first == entry->second.preferenceString) { // Convert the stored Uint into usable color data: unsigned int col = unsignedEntry.second; - QColor qcolor((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff); + QColor qcolor(App::Color::fromPackedRGB(col)); this->setStyleSheet(QString::fromUtf8("Gui--StatefulLabel{ color : rgba(%1,%2,%3,%4) ;}").arg(qcolor.red()).arg(qcolor.green()).arg(qcolor.blue()).arg(qcolor.alpha())); _styleCache[state] = this->styleSheet(); return; diff --git a/src/Mod/Points/App/PointsAlgos.cpp b/src/Mod/Points/App/PointsAlgos.cpp index 9b8af1dadf..60ef029adf 100644 --- a/src/Mod/Points/App/PointsAlgos.cpp +++ b/src/Mod/Points/App/PointsAlgos.cpp @@ -1089,14 +1089,9 @@ void PcdReader::read(const std::string& filename) if (types[rgba] == "U") { for (std::size_t i=0; i(data(i,rgba)); - uint32_t a = (packed >> 24) & 0xff; - uint32_t r = (packed >> 16) & 0xff; - uint32_t g = (packed >> 8) & 0xff; - uint32_t b = packed & 0xff; - colors.emplace_back(static_cast(r)/255.0f, - static_cast(g)/255.0f, - static_cast(b)/255.0f, - static_cast(a)/255.0f); + App::Color col; + col.setPackedARGB(packed); + colors.emplace_back(col); } } else if (types[rgba] == "F") { @@ -1105,14 +1100,9 @@ void PcdReader::read(const std::string& filename) float f = static_cast(data(i,rgba)); uint32_t packed; std::memcpy(&packed, &f, sizeof(packed)); - uint32_t a = (packed >> 24) & 0xff; - uint32_t r = (packed >> 16) & 0xff; - uint32_t g = (packed >> 8) & 0xff; - uint32_t b = packed & 0xff; - colors.emplace_back(static_cast(r)/255.0f, - static_cast(g)/255.0f, - static_cast(b)/255.0f, - static_cast(a)/255.0f); + App::Color col; + col.setPackedARGB(packed); + colors.emplace_back(col); } } }