Vault  4.1
vcolor.h
Go to the documentation of this file.
00001 /*
00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved.
00003 This file is part of the Code Vault version 4.1
00004 http://www.bombaydigital.com/
00005 License: MIT. See LICENSE.md in the Vault top level directory.
00006 */
00007 
00008 #ifndef vcolor_h
00009 #define vcolor_h
00010 
00013 #include "vtypes.h"
00014 
00015 #include "vstring.h"
00016 #include "vbinaryiostream.h"
00017 
00018 #ifdef VAULT_QT_SUPPORT
00019 #include <QColor>
00020 #endif
00021 
00022 class VSettingsNode;
00023 
00028 class VColor {
00029     public:
00030 
00031         // For clarity and speed, we provide constant accessors for the standard CSS colors.
00032         // These are the fastest way to use this set of colors, because it can avoid unnecessary
00033         // construction or copying in some use cases.
00034         static const VColor& AQUA();
00035         static const VColor& BLACK();
00036         static const VColor& BLUE();
00037         static const VColor& FUCHSIA();
00038         static const VColor& GREEN();
00039         static const VColor& GRAY();
00040         static const VColor& LIME();
00041         static const VColor& MAROON();
00042         static const VColor& NAVY();
00043         static const VColor& OLIVE();
00044         static const VColor& ORANGE();
00045         static const VColor& PURPLE();
00046         static const VColor& RED();
00047         static const VColor& SILVER();
00048         static const VColor& TEAL();
00049         static const VColor& WHITE();
00050         static const VColor& YELLOW();
00051 
00055         VColor() : mRed(0), mGreen(0), mBlue(0), mAlpha(255) {}
00063         VColor(int r, int g, int b, int alpha = 255) : mRed(VColor::_constrain(r)), mGreen(VColor::_constrain(g)), mBlue(VColor::_constrain(b)), mAlpha(VColor::_constrain(alpha)) {}
00086         explicit VColor(const VString& cssColor) : mRed(0), mGreen(0), mBlue(0), mAlpha(255) { this->setCSSColor(cssColor); }
00087         //explicit VColor(const char* cssColor) : mRed(0), mGreen(0), mBlue(0), mAlpha(255) { this->setCSSColor(cssColor); }
00091         VColor(VBinaryIOStream& stream) : mRed(0), mGreen(0), mBlue(0), mAlpha(255) { this->readFromStream(stream); }
00092         ~VColor() {}
00093 
00094 #ifdef VAULT_QT_SUPPORT
00095         explicit VColor(const QColor& c) : mRed(VColor::_constrain(c.red())), mGreen(VColor::_constrain(c.green())), mBlue(VColor::_constrain(c.blue())), mAlpha(VColor::_constrain(c.alpha())) {}
00096 #endif
00097 
00102         void readFromStream(VBinaryIOStream& stream) { this->_setStreamValue(stream.readU32()); }
00106         void writeToStream(VBinaryIOStream& stream) const { stream.writeU32(this->_getStreamValue()); }
00107 
00108         int getRed() const { return static_cast<int>(mRed); }
00109         int getGreen() const { return static_cast<int>(mGreen); }
00110         int getBlue() const { return static_cast<int>(mBlue); }
00111         int getAlpha() const { return static_cast<int>(mAlpha); }
00112         VDouble getLightness() const; 
00113         VString getCSSColor() const;
00114         void setRed(int val) { mRed = VColor::_constrain(val); }
00115         void setGreen(int val) { mGreen = VColor::_constrain(val); }
00116         void setBlue(int val) { mBlue = VColor::_constrain(val); }
00117         void setAlpha(int val) { mAlpha = VColor::_constrain(val); }
00118         void setValues(int r, int g, int b, int alpha = 255) { this->setRed(r); this->setGreen(g); this->setBlue(b); this->setAlpha(alpha); }
00119         void setCSSColor(const VString& cssColor); 
00120 
00121         friend inline bool operator==(const VColor& lhs, const VColor& rhs);  // exact equality
00122         friend inline bool operator!=(const VColor& lhs, const VColor& rhs);  // exact inequality
00123         // The < operator is needed for to support STL sort(); others provided for completeness.
00124         // It would be just as valid, perhaps more so, to sort based on HSV values or something like that.
00125         friend inline bool operator< (const VColor& lhs, const VColor& rhs);
00126         friend inline bool operator<=(const VColor& lhs, const VColor& rhs);
00127         friend inline bool operator>=(const VColor& lhs, const VColor& rhs);
00128         friend inline bool operator> (const VColor& lhs, const VColor& rhs);
00129 
00130 #ifdef VAULT_QT_SUPPORT
00131         void setQColor(const QColor& c) { this->setValues(c.red(), c.blue(), c.green(), c.alpha()); }
00132         QColor getQColor() const { return QColor(static_cast<int>(mRed), static_cast<int>(mGreen), static_cast<int>(mBlue), static_cast<int>(mAlpha)); }
00133 #endif
00134 
00135     private:
00136 
00137         static Vu8 _constrain(int val) { return static_cast<Vu8>(V_CONSTRAIN_MINMAX(val, 0, 255)); }
00138         void _setStreamValue(Vu32 value) { mRed = (Vu8)((value & 0xFF000000) >> 24); mGreen = (Vu8)((value & 0x00FF0000) >> 16); mBlue = (Vu8)((value & 0x0000FF00) >> 8); mAlpha = (Vu8)(value & 0x000000FF); }
00139         Vu32 _getStreamValue() const { return (mRed << 24) | (mGreen << 16) | (mBlue << 8) | mAlpha; }
00140 
00141         Vu8 mRed;
00142         Vu8 mGreen;
00143         Vu8 mBlue;
00144         Vu8 mAlpha;
00145 };
00146 
00147 inline bool operator==(const VColor& lhs, const VColor& rhs) { return lhs.mRed == rhs.mRed && lhs.mGreen == rhs.mGreen && lhs.mBlue == rhs.mBlue && lhs.mAlpha == rhs.mAlpha; }
00148 inline bool operator!=(const VColor& lhs, const VColor& rhs) { return !operator==(lhs, rhs); }
00149 inline bool operator< (const VColor& lhs, const VColor& rhs) { return lhs._getStreamValue() < rhs._getStreamValue(); }
00150 inline bool operator<=(const VColor& lhs, const VColor& rhs) { return !operator>(lhs, rhs); }
00151 inline bool operator>=(const VColor& lhs, const VColor& rhs) { return !operator<(lhs, rhs); }
00152 inline bool operator> (const VColor& lhs, const VColor& rhs) { return  operator<(rhs, lhs); }
00153 
00154 // VColorPair -----------------------------------------------------------------
00155 
00164 class VColorPair {
00165     public:
00166 
00167         VColorPair();
00168         VColorPair(const VColor& bg); // fg set to contrast with bg
00169         VColorPair(const VColor& bg, const VColor& fg);
00170         ~VColorPair() {} // non-virtual for space efficiency
00171 
00172         const VColor& getBg() const { return mBg; }
00173         const VColor& getFg() const { return mFg; }
00174         const VColor& getColor(bool isBg) const { return isBg ? mBg : mFg; }
00175 
00176         // Mainly for debugging purposes, a string describing the color pair.
00177         VString getCSSColor() const;
00178 
00185         static VColor generateContrastingForeground(const VColor& bg);
00194         static VColorPair safeConstructColorPair(const VString& bgCssColor, const VString& fgCssColor);
00195 
00196         friend inline bool operator==(const VColorPair& c1, const VColorPair& c2);  // exact equality
00197         friend inline bool operator!=(const VColorPair& c1, const VColorPair& c2);  // exact inequality
00198 
00199     private:
00200 
00201         VColor mBg;
00202         VColor mFg;
00203 };
00204 
00205 inline bool operator==(const VColorPair& c1, const VColorPair& c2) { return c1.mBg == c2.mBg && c1.mFg == c2.mFg; }
00206 inline bool operator!=(const VColorPair& c1, const VColorPair& c2) { return c1.mBg != c2.mBg || c1.mFg != c2.mFg; }
00207 
00208 // VColorMapper ---------------------------------------------------------------
00209 
00230 class VColorMapper {
00231     public:
00232 
00233         VColorMapper() : mDefaultColors() {}
00234         virtual ~VColorMapper() {}
00235 
00236         virtual void readColors(const VSettingsNode& mapperNode, VStringVector* errorList);
00237 
00238         virtual VColorPair getColors(const VString& stringValue) const = 0;
00239         virtual VColorPair getColors(int intValue) const = 0;
00240         virtual VColorPair getColors(Vs64 int64Value) const = 0;
00241         virtual VColorPair getColors(VDouble doubleValue) const = 0;
00242 
00243         void setDefaultColors(const VColorPair& defaultColors) { mDefaultColors = defaultColors; }
00244 
00245     protected:
00246 
00247         virtual void _readColorElement(const VSettingsNode& colorNode) = 0; // *Should* throw VException if element is invalid.
00248         VColorPair _readColorPair(const VSettingsNode& colorNode);
00249 
00250         VColorPair mDefaultColors;
00251 };
00252 
00253 // VColorPalette --------------------------------------------------------------
00254 
00255 class VSettingsNode;
00256 typedef std::map<VString, VColorMapper*> VColorPaletteMap;
00257 
00267 class VColorPalette {
00268     public:
00269 
00270         VColorPalette();
00271         VColorPalette(const VSettingsNode& paletteNode, VStringVector* errorList);
00272         virtual ~VColorPalette();
00273 
00274         const VString& getName() const { return mName; }
00275         void setName(const VString& name) { mName = name; }
00276 
00277         void adoptColorMapper(const VString& mapperName, VColorMapper* mapper);
00278         const VColorMapper* findMapper(const VString& mapperName) const;
00279 
00280         VColorPair getColors(const VString& mapperName, const VString& stringValue) const;
00281         VColorPair getColors(const VString& mapperName, int intValue) const;
00282         VColorPair getColors(const VString& mapperName, Vs64 int64Value) const;
00283         VColorPair getColors(const VString& mapperName, VDouble doubleValue) const;
00284 
00285     private:
00286 
00287         void _addMapper(const VSettingsNode& mapperNode, VStringVector* errorList);
00288         VColorMapper* _readNewMapper(const VString& mapperType, const VSettingsNode& mapperNode, bool usesPrefixMode, VStringVector* errorList);
00289         void _addMapperNameAliases(VColorMapper* mapper, const VSettingsNode& mapperNode, VStringVector* errorList);
00290 
00291         VString          mName;
00292         VColorPaletteMap mColorMappers;
00293         VStringVector    mAliases; // Tracks aliases in use in map so destructor can *safely* clean up.
00294 };
00295 
00296 // VStringColorMapper ---------------------------------------------------------
00297 
00298 typedef std::map<VString, VColorPair> VStringColorMap;
00299 
00303 class VStringColorMapper : public VColorMapper {
00304     public:
00305 
00306         VStringColorMapper();
00307         virtual ~VStringColorMapper();
00308 
00309         virtual void readColors(const VSettingsNode& mapperNode, VStringVector* errorList);
00310 
00311         virtual VColorPair getColors(const VString& stringValue) const;
00312         virtual VColorPair getColors(int intValue) const;
00313         virtual VColorPair getColors(Vs64 int64Value) const;
00314         virtual VColorPair getColors(VDouble doubleValue) const;
00315 
00316         void addColors(const VString& stringValue, const VColorPair& colors);
00317 
00318     protected:
00319 
00320         virtual void _readColorElement(const VSettingsNode& colorNode);
00321 
00322     private:
00323 
00324         VStringColorMap mColorMap;
00325         bool            mCaseSensitive;
00326 };
00327 
00328 // VIntegerColorMapper --------------------------------------------------------
00329 
00330 typedef std::map<Vs64, VColorPair> VIntegerColorMap;
00331 
00338 class VIntegerColorMapper : public VColorMapper {
00339     public:
00340 
00341         VIntegerColorMapper();
00342         virtual ~VIntegerColorMapper();
00343 
00344         virtual VColorPair getColors(const VString& stringValue) const;
00345         virtual VColorPair getColors(int intValue) const;
00346         virtual VColorPair getColors(Vs64 int64Value) const;
00347         virtual VColorPair getColors(VDouble doubleValue) const;
00348 
00349         void addColors(Vs64 intValue, const VColorPair& colors);
00350 
00351     protected:
00352 
00353         virtual void _readColorElement(const VSettingsNode& colorNode);
00354 
00355     private:
00356 
00357         VIntegerColorMap    mColorMap;
00358 };
00359 
00360 // VDoubleColorMapper ---------------------------------------------------------
00361 
00373 class VDoubleColorMapper : public VColorMapper {
00374     public:
00375 
00376         VDoubleColorMapper();
00377         virtual ~VDoubleColorMapper();
00378 
00379         virtual VColorPair getColors(const VString& stringValue) const;
00380         virtual VColorPair getColors(int intValue) const;
00381         virtual VColorPair getColors(Vs64 int64Value) const;
00382         virtual VColorPair getColors(VDouble doubleValue) const;
00383 
00384         void addColors(VDouble doubleValue, const VColorPair& rangeColors);
00385 
00386     protected:
00387 
00388         virtual void _readColorElement(const VSettingsNode& colorNode);
00389 
00390     private:
00391 
00392         VStringColorMap mColorMap;
00393 };
00394 
00395 // VStringRangeColorMapper --------------------------------------------------------
00396 
00401 class VStringRangeColorElement {
00402     public:
00403 
00404         VStringRangeColorElement(const VString& rangeMin, const VColorPair& colors) :
00405             mRangeMin(rangeMin), mColors(colors) {}
00406         ~VStringRangeColorElement() {}
00407 
00408         VString mRangeMin;
00409         VColorPair mColors;
00410 
00411         // Required to allow STL to sort objects of this class:
00412         friend inline bool operator<(const VStringRangeColorElement& e1, const VStringRangeColorElement& e2);
00413 };
00414 
00415 inline bool operator<(const VStringRangeColorElement& e1, const VStringRangeColorElement& e2) { return e1.mRangeMin < e2.mRangeMin; }
00416 
00417 typedef std::vector<VStringRangeColorElement> VStringRangeVector;
00418 
00434 class VStringRangeColorMapper : public VColorMapper {
00435     public:
00436 
00437         VStringRangeColorMapper(bool usesPrefixMode);
00438         virtual ~VStringRangeColorMapper();
00439 
00440         virtual void readColors(const VSettingsNode& mapperNode, VStringVector* errorList);
00441 
00442         virtual VColorPair getColors(const VString& stringValue) const;
00443         virtual VColorPair getColors(int intValue) const;
00444         virtual VColorPair getColors(Vs64 int64Value) const;
00445         virtual VColorPair getColors(VDouble doubleValue) const;
00446 
00447         void addColors(const VString& rangeMin, const VColorPair& rangeColors);
00448 
00449     protected:
00450 
00451         virtual void _readColorElement(const VSettingsNode& colorNode);
00452 
00453     private:
00454 
00455         VColorPair _getColorsWithPrefixModeCheck(const VColorPair& foundColors) const;
00456 
00457         VStringRangeVector mColorRanges;
00458         bool               mCaseSensitive;
00459         bool               mUsesPrefixMode;
00460 
00461         friend class VColorUnit;
00462 };
00463 
00464 // VIntegerRangeColorMapper --------------------------------------------------------
00465 
00470 class VIntegerRangeColorElement {
00471     public:
00472 
00473         VIntegerRangeColorElement(Vs64 rangeMin, const VColorPair& colors) :
00474             mRangeMin(rangeMin), mColors(colors) {}
00475         ~VIntegerRangeColorElement() {}
00476 
00477         Vs64 mRangeMin;
00478         VColorPair mColors;
00479 
00480         // Required to allow STL to sort objects of this class:
00481         friend inline bool operator<(const VIntegerRangeColorElement& e1, const VIntegerRangeColorElement& e2);
00482 };
00483 
00484 inline bool operator<(const VIntegerRangeColorElement& e1, const VIntegerRangeColorElement& e2) { return e1.mRangeMin < e2.mRangeMin; }
00485 
00486 typedef std::vector<VIntegerRangeColorElement> VIntegerRangeVector;
00487 
00494 class VIntegerRangeColorMapper : public VColorMapper {
00495     public:
00496 
00497         VIntegerRangeColorMapper();
00498         virtual ~VIntegerRangeColorMapper();
00499 
00500         virtual VColorPair getColors(const VString& stringValue) const;
00501         virtual VColorPair getColors(int intValue) const;
00502         virtual VColorPair getColors(Vs64 int64Value) const;
00503         virtual VColorPair getColors(VDouble doubleValue) const;
00504 
00505         void addColors(Vs64 rangeMin, const VColorPair& rangeColors);
00506 
00507     protected:
00508 
00509         virtual void _readColorElement(const VSettingsNode& colorNode);
00510 
00511     private:
00512 
00513         VIntegerRangeVector mColorRanges;
00514 
00515         friend class VColorUnit;
00516 };
00517 
00518 // VDoubleRangeColorMapper --------------------------------------------------------
00519 
00524 class VDoubleRangeColorElement {
00525     public:
00526 
00527         VDoubleRangeColorElement(VDouble rangeMin, const VColorPair& colors) :
00528             mRangeMin(rangeMin), mColors(colors) {}
00529         ~VDoubleRangeColorElement() {}
00530 
00531         VDouble mRangeMin;
00532         VColorPair mColors;
00533 
00534         // Required to allow STL to sort objects of this class:
00535         friend inline bool operator<(const VDoubleRangeColorElement& e1, const VDoubleRangeColorElement& e2);
00536 };
00537 
00538 inline bool operator<(const VDoubleRangeColorElement& e1, const VDoubleRangeColorElement& e2) { return e1.mRangeMin < e2.mRangeMin; }
00539 
00540 typedef std::vector<VDoubleRangeColorElement> VDoubleRangeVector;
00541 
00545 class VDoubleRangeColorMapper : public VColorMapper {
00546     public:
00547 
00548         VDoubleRangeColorMapper();
00549         virtual ~VDoubleRangeColorMapper();
00550 
00551         virtual VColorPair getColors(const VString& stringValue) const;
00552         virtual VColorPair getColors(int intValue) const;
00553         virtual VColorPair getColors(Vs64 int64Value) const;
00554         virtual VColorPair getColors(VDouble doubleValue) const;
00555 
00556         void addColors(VDouble rangeMin, const VColorPair& rangeColors);
00557 
00558     protected:
00559 
00560         virtual void _readColorElement(const VSettingsNode& colorNode);
00561 
00562     private:
00563 
00564         VDoubleRangeVector mColorRanges;
00565 
00566         friend class VColorUnit;
00567 };
00568 
00569 #endif /* vcolor_h */

Copyright ©1997-2014 Trygve Isaacson. All rights reserved. This documentation was generated with Doxygen.