From 7a3bcded061b513805a8651cee846bd6fa0918b2 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 25 Feb 2023 16:59:36 +0100 Subject: [PATCH] App: move Color class to its own sources --- src/App/CMakeLists.txt | 2 + src/App/Color.cpp | 145 ++++++++++++++++++++++++++++++++++++++ src/App/Color.h | 126 +++++++++++++++++++++++++++++++++ src/App/Material.h | 155 +---------------------------------------- 4 files changed, 274 insertions(+), 154 deletions(-) create mode 100644 src/App/Color.cpp create mode 100644 src/App/Color.h diff --git a/src/App/CMakeLists.txt b/src/App/CMakeLists.txt index fbaeeee22f..3f20baebd6 100644 --- a/src/App/CMakeLists.txt +++ b/src/App/CMakeLists.txt @@ -256,6 +256,7 @@ SET(FreeCADApp_CPP_SRCS ApplicationPy.cpp AutoTransaction.cpp Branding.cpp + Color.cpp ColorModel.cpp ComplexGeoData.cpp ComplexGeoDataPyImp.cpp @@ -272,6 +273,7 @@ SET(FreeCADApp_HPP_SRCS Application.h AutoTransaction.h Branding.h + Color.h ColorModel.h ComplexGeoData.h Enumeration.h diff --git a/src/App/Color.cpp b/src/App/Color.cpp new file mode 100644 index 0000000000..64f44e3468 --- /dev/null +++ b/src/App/Color.cpp @@ -0,0 +1,145 @@ +/*************************************************************************** + * Copyright (c) 2005 Jürgen Riegel * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library 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 Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#ifndef _PreComp_ +#include +#include +#endif + +#include "Color.h" + +using namespace App; + + +Color::Color(float R, float G, float B, float A) + : r(R) + , g(G) + , b(B) + , a(A) +{ +} + +Color::Color(uint32_t rgba) +{ + setPackedValue( 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 +{ + return !operator==(c); +} + +void Color::set(float R,float G, float B, float A) +{ + r = R; + g = G; + b = B; + a = A; +} + +Color& Color::setPackedValue(uint32_t rgba) +{ + this->set((rgba >> 24)/255.0f, + ((rgba >> 16)&0xff)/255.0f, + ((rgba >> 8)&0xff)/255.0f, + (rgba&0xff)/255.0f); + return *this; +} + +uint32_t Color::getPackedValue() const +{ + return (static_cast(r*255.0f + 0.5f) << 24 | + static_cast(g*255.0f + 0.5f) << 16 | + static_cast(b*255.0f + 0.5f) << 8 | + static_cast(a*255.0f + 0.5f)); +} + +uint32_t Color::getPackedARGB() const +{ + return (static_cast(a*255.0f + 0.5f) << 24 | + static_cast(r*255.0f + 0.5f) << 16 | + static_cast(g*255.0f + 0.5f) << 8 | + static_cast(b*255.0f + 0.5f)); +} + +std::string Color::asHexString() const +{ + std::stringstream ss; + ss << "#" << std::hex << std::uppercase << std::setfill('0') + << std::setw(2) << int(r*255.0f) + << std::setw(2) << int(g*255.0f) + << std::setw(2) << int(b*255.0f); + return ss.str(); +} + +bool Color::fromHexString(const std::string& hex) +{ + if (hex.size() < 7 || hex[0] != '#') + return false; + // #RRGGBB + if (hex.size() == 7) { + std::stringstream ss(hex); + unsigned int rgb; + char c; + + ss >> c >> std::hex >> rgb; + int rc = (rgb >> 16) & 0xff; + int gc = (rgb >> 8) & 0xff; + int bc = rgb & 0xff; + + r = rc / 255.0f; + g = gc / 255.0f; + b = bc / 255.0f; + + return true; + } + // #RRGGBBAA + if (hex.size() == 9) { + std::stringstream ss(hex); + unsigned int rgba; + char c; + + ss >> c >> std::hex >> rgba; + int rc = (rgba >> 24) & 0xff; + int gc = (rgba >> 16) & 0xff; + int bc = (rgba >> 8) & 0xff; + int ac = rgba & 0xff; + + r = rc / 255.0f; + g = gc / 255.0f; + b = bc / 255.0f; + a = ac / 255.0f; + + return true; + } + + return false; +} diff --git a/src/App/Color.h b/src/App/Color.h new file mode 100644 index 0000000000..7aba40d0ba --- /dev/null +++ b/src/App/Color.h @@ -0,0 +1,126 @@ +/*************************************************************************** + * Copyright (c) 2005 Jürgen Riegel * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library 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 Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#ifndef APP_COLOR_H +#define APP_COLOR_H + +#ifdef __GNUC__ +# include +#endif +#include + +#include + +namespace App +{ + +/** Color class + */ +class AppExport Color +{ +public: + /** + * Defines the color as (R,G,B,A) whereas all values are in the range [0,1]. + * \a A defines the transparency. + */ + explicit Color(float R=0.0,float G=0.0, float B=0.0, float A=0.0); + + /** + * Does basically the same as the constructor above unless that (R,G,B,A) is + * encoded as an unsigned int. + */ + explicit Color(uint32_t rgba); + + /** Copy constructor. */ + Color(const Color& c) = default; + Color(Color&&) = default; + + /** Returns true if both colors are equal. Therefore all components must be equal. */ + bool operator==(const Color& c) const; + bool operator!=(const Color& c) const; + /** + * Defines the color as (R,G,B,A) whereas all values are in the range [0,1]. + * \a A defines the transparency, 0 means complete opaque and 1 invisible. + */ + void set(float R,float G, float B, float A=0.0); + Color& operator=(const Color& c) = default; + Color& operator=(Color&& c) = default; + /** + * Sets the color value as a 32 bit combined red/green/blue/alpha value. + * Each component is 8 bit wide (i.e. from 0x00 to 0xff), and the red + * value should be stored leftmost, like this: 0xRRGGBBAA. + * + * \sa getPackedValue(). + */ + Color& setPackedValue(uint32_t rgba); + /** + * Returns color as a 32 bit packed unsigned int in the form 0xRRGGBBAA. + * + * \sa setPackedValue(). + */ + uint32_t getPackedValue() const; + /** + * Returns color as a 32 bit packed unsigned int in the form 0xAARRGGBB. + */ + uint32_t getPackedARGB() const; + template + static uint32_t asPackedRGBA(const T& color) { + return (color.red() << 24) | (color.green() << 16) | (color.blue() << 8) | color.alpha(); + } + template + static uint32_t asPackedRGB(const T& color) { + return (color.red() << 24) | (color.green() << 16) | (color.blue() << 8); + } + /** + * creates FC Color from template type, e.g. Qt QColor + */ + template + void setValue(const T& q) + { set(q.redF(),q.greenF(),q.blueF()); } + /** + * returns a template type e.g. Qt color equivalent to FC color + * + */ + template + inline T asValue() const { + return(T(int(r*255.0f),int(g*255.0f),int(b*255.0f))); + } + /** + * returns color as hex color "#RRGGBB" + * + */ + std::string asHexString() const; + + /** + * gets color from hex color "#RRGGBB" + * + */ + bool fromHexString(const std::string& hex); + + /// color values, public accessible + float r,g,b,a; +}; + +} //namespace App + +#endif // APP_COLOR_H diff --git a/src/App/Material.h b/src/App/Material.h index 4179015ee6..c2e60a6753 100644 --- a/src/App/Material.h +++ b/src/App/Material.h @@ -24,164 +24,11 @@ #ifndef APP_MATERIAL_H #define APP_MATERIAL_H -#ifdef __GNUC__ -# include -#endif - -#include -#include -#include +#include namespace App { -/** Color class - */ -class AppExport Color -{ -public: - /** - * Defines the color as (R,G,B,A) whereas all values are in the range [0,1]. - * \a A defines the transparency. - */ - explicit Color(float R=0.0,float G=0.0, float B=0.0, float A=0.0) - :r(R),g(G),b(B),a(A){} - /** - * Does basically the same as the constructor above unless that (R,G,B,A) is - * encoded as an unsigned int. - */ - explicit Color(uint32_t rgba) - { setPackedValue( rgba ); } - /** Copy constructor. */ - Color(const Color& c) - :r(c.r),g(c.g),b(c.b),a(c.a){} - /** Returns true if both colors are equal. Therefore all components must be equal. */ - bool operator==(const Color& c) const - { - return getPackedValue() == c.getPackedValue(); - //return (c.r==r && c.g==g && c.b==b && c.a==a); - } - bool operator!=(const Color& c) const - { - return !operator==(c); - } - /** - * Defines the color as (R,G,B,A) whereas all values are in the range [0,1]. - * \a A defines the transparency, 0 means complete opaque and 1 invisible. - */ - void set(float R,float G, float B, float A=0.0) - { - r=R;g=G;b=B;a=A; - } - Color& operator=(const Color& c) - { - r=c.r;g=c.g;b=c.b;a=c.a; - return *this; - } - /** - * Sets the color value as a 32 bit combined red/green/blue/alpha value. - * Each component is 8 bit wide (i.e. from 0x00 to 0xff), and the red - * value should be stored leftmost, like this: 0xRRGGBBAA. - * - * \sa getPackedValue(). - */ - Color& setPackedValue(uint32_t rgba) - { - this->set((rgba >> 24)/255.0f, - ((rgba >> 16)&0xff)/255.0f, - ((rgba >> 8)&0xff)/255.0f, - (rgba&0xff)/255.0f); - return *this; - } - /** - * Returns color as a 32 bit packed unsigned int in the form 0xRRGGBBAA. - * - * \sa setPackedValue(). - */ - uint32_t getPackedValue() const - { - return (static_cast(r*255.0f + 0.5f) << 24 | - static_cast(g*255.0f + 0.5f) << 16 | - static_cast(b*255.0f + 0.5f) << 8 | - static_cast(a*255.0f + 0.5f)); - } - /** - * creates FC Color from template type, e.g. Qt QColor - */ - template - void setValue(const T& q) - { set(q.redF(),q.greenF(),q.blueF()); } - /** - * returns a template type e.g. Qt color equivalent to FC color - * - */ - template - inline T asValue() const { - return(T(int(r*255.0f),int(g*255.0f),int(b*255.0f))); - } - /** - * returns color as hex color "#RRGGBB" - * - */ - std::string asHexString() const { - std::stringstream ss; - ss << "#" << std::hex << std::uppercase << std::setfill('0') << std::setw(2) << int(r*255.0f) - << std::setw(2) << int(g*255.0f) - << std::setw(2) << int(b*255.0f); - return ss.str(); - } - - /** - * gets color from hex color "#RRGGBB" - * - */ - bool fromHexString(const std::string& hex) { - if (hex.size() < 7 || hex[0] != '#') - return false; - // #RRGGBB - if (hex.size() == 7) { - std::stringstream ss(hex); - unsigned int rgb; - char c; - - ss >> c >> std::hex >> rgb; - int rc = (rgb >> 16) & 0xff; - int gc = (rgb >> 8) & 0xff; - int bc = rgb & 0xff; - - r = rc / 255.0f; - g = gc / 255.0f; - b = bc / 255.0f; - - return true; - } - // #RRGGBBAA - if (hex.size() == 9) { - std::stringstream ss(hex); - unsigned int rgba; - char c; - - ss >> c >> std::hex >> rgba; - int rc = (rgba >> 24) & 0xff; - int gc = (rgba >> 16) & 0xff; - int bc = (rgba >> 8) & 0xff; - int ac = rgba & 0xff; - - r = rc / 255.0f; - g = gc / 255.0f; - b = bc / 255.0f; - a = ac / 255.0f; - - return true; - } - - return false; - } - - /// color values, public accessible - float r,g,b,a; -}; - /** Material class */ class AppExport Material