there is no moc file generated for this class
[kdegraphics.git] / kolourpaint / imagelib / kpColor.h
blob0e77cb2387488a2863e5fdfceb6bb0436331b048
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef KP_COLOR_H
30 #define KP_COLOR_H
33 #include <qcolor.h>
36 class QDataStream;
40 // kpColor is an object-oriented abstraction of QRgb, for document image data,
41 // with the additional restriction of enforcing the KolourPaint convention of
42 // only supporting totally transparent and totally opaque colors. Eventually,
43 // this restriction will be dropped. In the future, other color models such as
44 // 8-bit indexed will be supported. It also provides better error handling,
45 // reporting (noisy kError()'s) and recovery compared to Qt. This abstraction
46 // will allow us to eventually dump the Qt paint routines.
48 // In general, you should pass around kpColor objects instead of QRgb
49 // and QColor. Only convert an opaque kpColor to a QColor (using toQColor())
50 // if you need to draw something on-screen.
52 // Constructing a kpColor object from QColor is usually wrong since QColor's
53 // come from on-screen pixels, which may lack the full color resolution of
54 // kpColor, due to the limited color range on e.g. a 16-bit screen.
56 class kpColor
58 public:
59 kpColor ();
60 kpColor (int red, int green, int blue, bool isTransparent = false);
61 kpColor (const QRgb &rgba);
62 kpColor (const kpColor &rhs);
63 friend QDataStream &operator<< (QDataStream &stream, const kpColor &color);
64 friend QDataStream &operator>> (QDataStream &stream, kpColor &color);
65 kpColor &operator= (const kpColor &rhs);
66 bool operator== (const kpColor &rhs) const;
67 bool operator!= (const kpColor &rhs) const;
71 // Constants
73 public:
74 // "lhs.isSimilarTo (rhs, kpColor::Exact)" is exactly the same as calling
75 // "lhs == rhs".
76 static const int Exact;
78 static const kpColor Invalid;
79 static const kpColor Transparent;
83 // Primary Colors + B&W
86 static const kpColor Red, Green, Blue;
87 static const kpColor Black, White;
91 // Full-brightness Colors
94 static const kpColor Yellow, Purple, Aqua;
98 // Mixed Colors
101 static const kpColor Gray, LightGray, Orange;
105 // Pastel Colors
108 static const kpColor Pink, LightGreen, LightBlue, Tan;
112 // Dark Colors
115 static const kpColor DarkRed;
117 // (identical)
118 static const kpColor DarkOrange, Brown;
120 static const kpColor DarkYellow, DarkGreen, DarkAqua, DarkBlue,
121 DarkPurple, DarkGray;
124 public:
125 static int processSimilarity (double colorSimilarity);
126 // Usage: isSimilarTo (rhs, kpColor::processSimilarity (.1)) checks for
127 // Color Similarity within 10%
128 bool isSimilarTo (const kpColor &rhs, int processedSimilarity) const;
129 ~kpColor ();
131 bool isValid () const;
133 int red () const;
134 int green () const;
135 int blue () const;
136 int alpha () const;
137 bool isTransparent () const;
138 bool isOpaque () const;
140 // Cast operators will most likely result in careless conversions so
141 // use explicit functions instead:
142 QRgb toQRgb () const;
144 // (only valid if isOpaque())
145 QColor toQColor () const;
147 QColor maskColor () const;
149 private:
150 // Catch accidental call to "const QRgb &rgba" (unsigned int) ctor
151 // by e.g. "kpColor(Qt::black)" (Qt::black is an enum element that can cast
152 // to "unsigned int").
153 kpColor (Qt::GlobalColor color);
155 bool m_rgbaIsValid;
156 QRgb m_rgba;
158 mutable bool m_colorCacheIsValid;
159 mutable QColor m_colorCache;
163 #endif // KP_COLOR_H