1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2020 A. Stebich (librecad@mail.lordofbikes.de)
6 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
7 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
8 **
9 **
10 ** This file may be distributed and/or modified under the terms of the
11 ** GNU General Public License version 2 as published by the Free Software
12 ** Foundation and appearing in the file gpl-2.0.txt included in the
13 ** packaging of this file.
14 **
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ** GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 **
24 ** This copyright notice MUST APPEAR in all copies of the script!
25 **
26 **********************************************************************/
27 
28 
29 #ifndef RS_COLOR_H
30 #define RS_COLOR_H
31 
32 #include <QColor>
33 
34 #include "rs.h"
35 #include "rs_flags.h"
36 
37 //! Color defined by layer not entity
38 //#define C_BY_LAYER     0x00000001
39 //! Color defined by block not entity
40 //#define C_BY_BLOCK     0x00000002
41 
42 /**
43  * Color class.
44  *
45  * @author Andrew Mustun
46  */
47 class RS_Color: public QColor, public RS_Flags {
48 public:
RS_Color()49     RS_Color() : QColor(), RS_Flags() {}
RS_Color(int r,int g,int b)50     RS_Color(int r, int g, int b) : QColor(r, g, b), RS_Flags() {}
RS_Color(int r,int g,int b,int a)51     RS_Color(int r, int g, int b, int a) : QColor(r, g, b, a), RS_Flags() {}
RS_Color(const QColor & c)52     RS_Color(const QColor& c) : QColor(c), RS_Flags() {}
RS_Color(const Qt::GlobalColor color)53     RS_Color(const Qt::GlobalColor color) : QColor(color), RS_Flags() {}
RS_Color(const RS_Color & c)54     RS_Color(const RS_Color& c) : QColor(c), RS_Flags() {
55         setFlags(c.getFlags());
56     }
RS_Color(unsigned int f)57     RS_Color(unsigned int f) : QColor(), RS_Flags(f) {}
RS_Color(QString name)58     RS_Color(QString name) : QColor(name), RS_Flags() {}
59 
60 
61     /** @return A copy of this color without flags. */
stripFlags()62     RS_Color stripFlags() const {
63         return RS_Color(red(), green(), blue());
64     }
65 
66     /** @return true if the color is defined by layer. */
isByLayer()67     bool isByLayer() const {
68         return getFlag(RS2::FlagByLayer);
69     }
70 
71     /** @return true if the color is defined by block. */
isByBlock()72     bool isByBlock() const {
73         return getFlag(RS2::FlagByBlock);
74     }
75 
toQColor(void)76     QColor toQColor(void) const {
77             QColor c0;
78             c0.setRgb(red(),green(),blue());
79             return c0;
80     }
81 
82     //These 3 methods are used for plugins
83     int toIntColor(void) const;
84     void fromIntColor(int co);
85     int colorDistance(const RS_Color& c) const;
86 
87     enum {
88         Black = 0,
89         /**
90          * Minimum acceptable distance between two colors before visibility
91          * enhancement is required. Determined empirically.
92          */
93         MinColorDistance = 20,  //< in %
94     };
95 
96     RS_Color& operator = (const RS_Color& c) {
97         setRgb(c.red(), c.green(), c.blue());
98         setFlags(c.getFlags());
99 
100         return *this;
101     }
102 
103     bool operator == (const RS_Color& c) const {
104         return (red()==c.red() &&
105                 green()==c.green() &&
106                 blue()==c.blue() &&
107                 getFlags()==c.getFlags());
108     }
109 
110     friend std::ostream& operator << (std::ostream& os, const RS_Color& c);
111 };
112 
113 #endif
114 
115