From 972f05ba5aeee155bf7b6ed9652600587733c572 Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Thu, 3 Apr 2025 00:31:49 +0200 Subject: [PATCH] Gui: Move color traits to Utilities.h --- src/Gui/Tools/color_traits.h | 288 ----------------------------------- src/Gui/Utilities.h | 253 ++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+), 288 deletions(-) delete mode 100644 src/Gui/Tools/color_traits.h diff --git a/src/Gui/Tools/color_traits.h b/src/Gui/Tools/color_traits.h deleted file mode 100644 index 94fe6f1336..0000000000 --- a/src/Gui/Tools/color_traits.h +++ /dev/null @@ -1,288 +0,0 @@ -// SPDX-License-Identifier: LGPL-2.1-or-later - -/*************************************************************************** - * Copyright (c) 2024 Werner Mayer * - * * - * 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 * - * . * - * * - **************************************************************************/ - -#ifndef GUI_COLORTRAITS_H -#define GUI_COLORTRAITS_H - -#include -#include -#include -#include - -namespace Base -{ -// Specialization for SbColor -template<> -struct color_traits -{ - using color_type = SbColor; - color_traits() = default; - explicit color_traits(const color_type& ct) - : ct(ct) - {} - float redF() const - { - return ct[0]; - } - float greenF() const - { - return ct[1]; - } - float blueF() const - { - return ct[2]; - } - float alphaF() const - { - return 1.0F; - } - void setRedF(float red) - { - ct[0] = red; - } - void setGreenF(float green) - { - ct[1] = green; - } - void setBlueF(float blue) - { - ct[2] = blue; - } - void setAlphaF(float alpha) - { - (void)alpha; - } - int red() const - { - return int(std::lround(ct[0] * 255.0F)); - } - int green() const - { - return int(std::lround(ct[1] * 255.0F)); - } - int blue() const - { - return int(std::lround(ct[2] * 255.0F)); - } - int alpha() const - { - return 255; - } - void setRed(int red) - { - ct[0] = static_cast(red) / 255.0F; - } - void setGreen(int green) - { - ct[1] = static_cast(green) / 255.0F; - } - void setBlue(int blue) - { - ct[2] = static_cast(blue) / 255.0F; - } - void setAlpha(int alpha) - { - (void)alpha; - } - static color_type makeColor(int red, int green, int blue, int alpha = 255) - { - (void)alpha; - return color_type{static_cast(red) / 255.0F, - static_cast(green) / 255.0F, - static_cast(blue) / 255.0F}; - } - -private: - color_type ct; -}; - -// Specialization for SbColor4f -template<> -struct color_traits -{ - using color_type = SbColor4f; - color_traits() = default; - explicit color_traits(const color_type& ct) - : ct(ct) - {} - float redF() const - { - return ct[0]; - } - float greenF() const - { - return ct[1]; - } - float blueF() const - { - return ct[2]; - } - float alphaF() const - { - return ct[3]; - } - void setRedF(float red) - { - ct[0] = red; - } - void setGreenF(float green) - { - ct[1] = green; - } - void setBlueF(float blue) - { - ct[2] = blue; - } - void setAlphaF(float alpha) - { - ct[3] = alpha; - } - int red() const - { - return int(std::lround(ct[0] * 255.0F)); - } - int green() const - { - return int(std::lround(ct[1] * 255.0F)); - } - int blue() const - { - return int(std::lround(ct[2] * 255.0F)); - } - int alpha() const - { - return int(std::lround(ct[3] * 255.0F)); - } - void setRed(int red) - { - ct[0] = static_cast(red) / 255.0F; - } - void setGreen(int green) - { - ct[1] = static_cast(green) / 255.0F; - } - void setBlue(int blue) - { - ct[2] = static_cast(blue) / 255.0F; - } - void setAlpha(int alpha) - { - ct[3] = static_cast(alpha) / 255.0F; - } - static color_type makeColor(int red, int green, int blue, int alpha = 255) - { - return color_type{static_cast(red) / 255.0F, - static_cast(green) / 255.0F, - static_cast(blue) / 255.0F, - static_cast(alpha) / 255.0F}; - } - -private: - color_type ct; -}; - -// Specialization for QColor -template<> -struct color_traits -{ - using color_type = QColor; - color_traits() = default; - explicit color_traits(const color_type& ct) - : ct(ct) - {} - float redF() const - { - return static_cast(ct.redF()); - } - float greenF() const - { - return static_cast(ct.greenF()); - } - float blueF() const - { - return static_cast(ct.blueF()); - } - float alphaF() const - { - return static_cast(ct.alphaF()); - } - void setRedF(float red) - { - ct.setRedF(red); - } - void setGreenF(float green) - { - ct.setGreenF(green); - } - void setBlueF(float blue) - { - ct.setBlueF(blue); - } - void setAlphaF(float alpha) - { - ct.setAlphaF(alpha); - } - int red() const - { - return ct.red(); - } - int green() const - { - return ct.green(); - } - int blue() const - { - return ct.blue(); - } - int alpha() const - { - return ct.alpha(); - } - void setRed(int red) - { - ct.setRed(red); - } - void setGreen(int green) - { - ct.setGreen(green); - } - void setBlue(int blue) - { - ct.setBlue(blue); - } - void setAlpha(int alpha) - { - ct.setAlpha(alpha); - } - static color_type makeColor(int red, int green, int blue, int alpha = 255) - { - return color_type{red, green, blue, alpha}; - } - -private: - color_type ct; -}; - -} // namespace App - -#endif // GUI_COLORTRAITS_H diff --git a/src/Gui/Utilities.h b/src/Gui/Utilities.h index cdb4a86523..13814cfccd 100644 --- a/src/Gui/Utilities.h +++ b/src/Gui/Utilities.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -117,6 +118,258 @@ private: const vec_type& v; }; +// Specialization for SbColor +template<> +struct color_traits +{ + using color_type = SbColor; + color_traits() = default; + explicit color_traits(const color_type& ct) + : ct(ct) + {} + float redF() const + { + return ct[0]; + } + float greenF() const + { + return ct[1]; + } + float blueF() const + { + return ct[2]; + } + float alphaF() const + { + return 1.0F; + } + void setRedF(float red) + { + ct[0] = red; + } + void setGreenF(float green) + { + ct[1] = green; + } + void setBlueF(float blue) + { + ct[2] = blue; + } + void setAlphaF(float alpha) + { + (void)alpha; + } + int red() const + { + return int(std::lround(ct[0] * 255.0F)); + } + int green() const + { + return int(std::lround(ct[1] * 255.0F)); + } + int blue() const + { + return int(std::lround(ct[2] * 255.0F)); + } + int alpha() const + { + return 255; + } + void setRed(int red) + { + ct[0] = static_cast(red) / 255.0F; + } + void setGreen(int green) + { + ct[1] = static_cast(green) / 255.0F; + } + void setBlue(int blue) + { + ct[2] = static_cast(blue) / 255.0F; + } + void setAlpha(int alpha) + { + (void)alpha; + } + static color_type makeColor(int red, int green, int blue, int alpha = 255) + { + (void)alpha; + return color_type{static_cast(red) / 255.0F, + static_cast(green) / 255.0F, + static_cast(blue) / 255.0F}; + } + +private: + color_type ct; +}; + +// Specialization for SbColor4f +template<> +struct color_traits +{ + using color_type = SbColor4f; + color_traits() = default; + explicit color_traits(const color_type& ct) + : ct(ct) + {} + float redF() const + { + return ct[0]; + } + float greenF() const + { + return ct[1]; + } + float blueF() const + { + return ct[2]; + } + float alphaF() const + { + return ct[3]; + } + void setRedF(float red) + { + ct[0] = red; + } + void setGreenF(float green) + { + ct[1] = green; + } + void setBlueF(float blue) + { + ct[2] = blue; + } + void setAlphaF(float alpha) + { + ct[3] = alpha; + } + int red() const + { + return int(std::lround(ct[0] * 255.0F)); + } + int green() const + { + return int(std::lround(ct[1] * 255.0F)); + } + int blue() const + { + return int(std::lround(ct[2] * 255.0F)); + } + int alpha() const + { + return int(std::lround(ct[3] * 255.0F)); + } + void setRed(int red) + { + ct[0] = static_cast(red) / 255.0F; + } + void setGreen(int green) + { + ct[1] = static_cast(green) / 255.0F; + } + void setBlue(int blue) + { + ct[2] = static_cast(blue) / 255.0F; + } + void setAlpha(int alpha) + { + ct[3] = static_cast(alpha) / 255.0F; + } + static color_type makeColor(int red, int green, int blue, int alpha = 255) + { + return color_type{static_cast(red) / 255.0F, + static_cast(green) / 255.0F, + static_cast(blue) / 255.0F, + static_cast(alpha) / 255.0F}; + } + +private: + color_type ct; +}; + +// Specialization for QColor +template<> +struct color_traits +{ + using color_type = QColor; + color_traits() = default; + explicit color_traits(const color_type& ct) + : ct(ct) + {} + float redF() const + { + return static_cast(ct.redF()); + } + float greenF() const + { + return static_cast(ct.greenF()); + } + float blueF() const + { + return static_cast(ct.blueF()); + } + float alphaF() const + { + return static_cast(ct.alphaF()); + } + void setRedF(float red) + { + ct.setRedF(red); + } + void setGreenF(float green) + { + ct.setGreenF(green); + } + void setBlueF(float blue) + { + ct.setBlueF(blue); + } + void setAlphaF(float alpha) + { + ct.setAlphaF(alpha); + } + int red() const + { + return ct.red(); + } + int green() const + { + return ct.green(); + } + int blue() const + { + return ct.blue(); + } + int alpha() const + { + return ct.alpha(); + } + void setRed(int red) + { + ct.setRed(red); + } + void setGreen(int green) + { + ct.setGreen(green); + } + void setBlue(int blue) + { + ct.setBlue(blue); + } + void setAlpha(int alpha) + { + ct.setAlpha(alpha); + } + static color_type makeColor(int red, int green, int blue, int alpha = 255) + { + return color_type{red, green, blue, alpha}; + } + +private: + color_type ct; +}; + template <> inline SbMatrix convertTo(const Base::Matrix4D& vec2) {