File indexing completed on 2024-05-05 04:21:07

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #ifndef KP_COLOR_H
0030 #define KP_COLOR_H
0031 
0032 
0033 #include <QColor>
0034 
0035 
0036 class QDataStream;
0037 
0038 
0039 //
0040 // kpColor is an object-oriented abstraction of QRgb, for document image data.
0041 // In the future, other color models such as
0042 // 8-bit indexed will be supported.  It also provides better error handling,
0043 // reporting (noisy qCCritical(kpLogImagelib)'s) and recovery compared to Qt.  This abstraction
0044 // will allow us to eventually dump the Qt paint routines.
0045 //
0046 // In general, you should pass around kpColor objects instead of QRgb
0047 // and QColor.  Only convert an opaque kpColor to a QColor (using toQColor())
0048 // if you need to draw something on-screen.
0049 //
0050 // Constructing a kpColor object from QColor is usually wrong since QColor's
0051 // come from on-screen pixels, which may lack the full color resolution of
0052 // kpColor, due to the limited color range on e.g. a 16-bit screen.
0053 //
0054 class kpColor
0055 {
0056 public:
0057     kpColor ();
0058     kpColor (int red, int green, int blue, bool isTransparent = false);
0059     explicit kpColor (const QRgb &rgba);
0060     kpColor (const kpColor &rhs);
0061     friend QDataStream &operator<< (QDataStream &stream, const kpColor &color);
0062     friend QDataStream &operator>> (QDataStream &stream, kpColor &color);
0063     kpColor &operator= (const kpColor &rhs);
0064     bool operator== (const kpColor &rhs) const;
0065     bool operator!= (const kpColor &rhs) const;
0066 
0067 
0068 //
0069 // Constants
0070 //
0071 public:
0072     // "lhs.isSimilarTo (rhs, kpColor::Exact)" is exactly the same as calling
0073     // "lhs == rhs".
0074     static const int Exact;
0075   
0076     static const kpColor Invalid;
0077     static const kpColor Transparent;
0078 
0079 
0080     //
0081     // Primary Colors + B&W
0082     //
0083     
0084     static const kpColor Red, Green, Blue;
0085     static const kpColor Black, White;
0086 
0087 
0088     //
0089     // Full-brightness Colors
0090     //
0091 
0092     static const kpColor Yellow, Purple, Aqua;
0093 
0094 
0095     //
0096     // Mixed Colors
0097     //
0098     
0099     static const kpColor Gray, LightGray, Orange;
0100      
0101 
0102     //
0103     // Pastel Colors
0104     //
0105 
0106     static const kpColor Pink, LightGreen, LightBlue, Tan;
0107 
0108 
0109     //
0110     // Dark Colors
0111     //
0112     
0113     static const kpColor DarkRed;
0114 
0115     // (identical)
0116     static const kpColor DarkOrange, Brown;
0117 
0118     static const kpColor DarkYellow, DarkGreen, DarkAqua, DarkBlue,
0119         DarkPurple, DarkGray;
0120 
0121 
0122 public:
0123     static int processSimilarity (double colorSimilarity);
0124     // Usage: isSimilarTo (rhs, kpColor::processSimilarity (.1)) checks for
0125     //        Color Similarity within 10%
0126     bool isSimilarTo (const kpColor &rhs, int processedSimilarity) const;
0127 
0128     bool isValid () const;
0129 
0130     int red () const;
0131     int green () const;
0132     int blue () const;
0133     int alpha () const;
0134     bool isTransparent () const;
0135 
0136     // Cast operators will most likely result in careless conversions so
0137     // use explicit functions instead:
0138     QRgb toQRgb () const;
0139 
0140     QColor toQColor () const;
0141 
0142 private:
0143     // Catch accidental call to "const QRgb &rgba" (unsigned int) ctor
0144     // by e.g. "kpColor(Qt::black)" (Qt::black is an enum element that can cast
0145     // to "unsigned int").
0146     kpColor (Qt::GlobalColor color);
0147     
0148     bool m_rgbaIsValid;
0149     QRgb m_rgba;
0150 
0151     mutable bool m_colorCacheIsValid;
0152     mutable QColor m_colorCache;
0153 };
0154 
0155 
0156 #endif  // KP_COLOR_H