Skip to content

Commit

Permalink
Core: Convert transparency to alpha value for old project files
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Dec 27, 2024
1 parent 6bb424b commit 6ef1e44
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/App/PropertyStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Interpreter.h>
#include <Base/ProgramVersion.h>
#include <Base/Reader.h>
#include <Base/Writer.h>
#include <Base/Quantity.h>
Expand Down Expand Up @@ -2390,6 +2391,11 @@ void PropertyColor::Restore(Base::XMLReader& reader)
reader.readElement("PropertyColor");
// get the value of my Attribute
unsigned long rgba = reader.getAttributeAsUnsigned("value");
if (Base::getVersion(reader.ProgramVersion) < Base::Version::v1_1) {
// Convert transparency / alpha value
unsigned long alpha = 0xff - (rgba & 0xff);
rgba = rgba - (rgba & 0xff) + alpha;
}
setValue(rgba);
}

Expand Down Expand Up @@ -2471,6 +2477,8 @@ void PropertyColorList::Restore(Base::XMLReader& reader)
// initiate a file read
reader.addFile(file.c_str(), this);
}

oldProgramVersion = Base::getVersion(reader.ProgramVersion) < Base::Version::v1_1;
}
}

Expand All @@ -2495,6 +2503,11 @@ void PropertyColorList::RestoreDocFile(Base::Reader& reader)
str >> value;
it.setPackedValue(value);
}
if (oldProgramVersion) {
for (auto& it : values) {
it.a = 1.0F - it.a;
}
}
setValues(values);
}

Expand Down Expand Up @@ -2725,6 +2738,12 @@ void PropertyMaterial::Restore(Base::XMLReader& reader)
_cMat.emissiveColor.setPackedValue(reader.getAttributeAsUnsigned("emissiveColor"));
_cMat.shininess = (float)reader.getAttributeAsFloat("shininess");
_cMat.transparency = (float)reader.getAttributeAsFloat("transparency");
if (Base::getVersion(reader.ProgramVersion) < Base::Version::v1_1) {
_cMat.ambientColor.a = 1.0F - _cMat.ambientColor.a;
_cMat.diffuseColor.a = 1.0F - _cMat.diffuseColor.a;
_cMat.specularColor.a = 1.0F - _cMat.specularColor.a;
_cMat.emissiveColor.a = 1.0F - _cMat.emissiveColor.a;
}
if (reader.hasAttribute("image")) {
_cMat.image = reader.getAttribute("image");
}
Expand Down Expand Up @@ -3270,6 +3289,8 @@ void PropertyMaterialList::Restore(Base::XMLReader& reader)
// initiate a file read
reader.addFile(file.c_str(), this);
}

oldProgramVersion = Base::getVersion(reader.ProgramVersion) < Base::Version::v1_1;
}
}

Expand Down Expand Up @@ -3353,6 +3374,7 @@ void PropertyMaterialList::RestoreDocFileV0(uint32_t count, Base::Reader& reader
str >> valueF;
it.transparency = valueF;
}
convertAlpha(values);
setValues(values);
}

Expand Down Expand Up @@ -3383,9 +3405,22 @@ void PropertyMaterialList::RestoreDocFileV3(Base::Reader& reader)
readString(str, it.imagePath);
readString(str, it.uuid);
}
convertAlpha(values);
setValues(values);
}

void PropertyMaterialList::convertAlpha(std::vector<App::Material>& materials)
{
if (oldProgramVersion) {
for (auto& it : materials) {
it.ambientColor.a = 1.0F - it.ambientColor.a;
it.diffuseColor.a = 1.0F - it.diffuseColor.a;
it.specularColor.a = 1.0F - it.specularColor.a;
it.emissiveColor.a = 1.0F - it.emissiveColor.a;
}
}
}

void PropertyMaterialList::readString(Base::InputStream& str, std::string& value)
{
uint32_t uCt {};
Expand Down
5 changes: 5 additions & 0 deletions src/App/PropertyStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,9 @@ class AppExport PropertyColorList: public PropertyListsT<Color>

protected:
Color getPyValue(PyObject* py) const override;

private:
bool oldProgramVersion {false};
};


Expand Down Expand Up @@ -1295,8 +1298,10 @@ class AppExport PropertyMaterialList: public PropertyListsT<Material>
void verifyIndex(int index) const;
void setMinimumSizeOne();
int resizeByOneIfNeeded(int index);
void convertAlpha(std::vector<App::Material>& materials);

Format formatVersion {Version_0};
bool oldProgramVersion {false};
};


Expand Down
80 changes: 80 additions & 0 deletions src/Base/ProgramVersion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/

#ifndef BASE_PROGRAM_VERSION_H
#define BASE_PROGRAM_VERSION_H

#include <array>
#include <map>
#include <string_view>
#include <FCGlobal.h>

namespace Base
{

enum class Version
{
v0_1x,
v0_16,
v0_17,
v0_18,
v0_19,
v0_20,
v0_21,
v0_22,
v1_0,
v1_1,
v1_x,
};

BaseExport Version getVersion(std::string_view str)
{

Check failure on line 51 in src/Base/ProgramVersion.h

View workflow job for this annotation

GitHub Actions / Pixi / build_with_pixi (windows-latest)

'Base::getVersion': definition of dllimport function not allowed
// clang-format off
using VersionItem = std::pair<std::string_view, Version>;
static constexpr std::array<VersionItem, 9> items = {{
{"0.16", Version::v0_16},
{"0.17", Version::v0_17},
{"0.18", Version::v0_18},
{"0.19", Version::v0_19},
{"0.20", Version::v0_20},
{"0.21", Version::v0_21},
{"0.22", Version::v0_22},
{"1.0" , Version::v1_0 },
{"1.1" , Version::v1_1 },
}};
// clang-format on
auto it = std::find_if(items.begin(), items.end(), [str](const auto& item) {
return str.compare(0, item.first.size(), item.first) == 0;
});
if (it != items.end()) {
return it->second;
}
if (!str.empty() && str[0] == '0') {
return Version::v0_1x;
}
return Version::v1_x;
}

} // namespace Base

#endif // BASE_PROGRAM_VERSION_H

0 comments on commit 6ef1e44

Please sign in to comment.