• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ut_color.h

Go to the documentation of this file.
00001 /* AbiSource Program Utilities
00002  * Copyright (C) 1998 AbiSource, Inc.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301 USA.
00018  */
00019 
00020 #ifndef UTCOLOR_H
00021 #define UTCOLOR_H
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include "config.h"
00025 #endif
00026 
00027 /* pre-emptive dismissal; ut_types.h is needed by just about everything,
00028  * so even if it's commented out in-file that's still a lot of work for
00029  * the preprocessor to do...
00030  */
00031 #ifndef UT_TYPES_H
00032 #include "ut_types.h"
00033 #endif
00034 
00035 
00036 #include <string>
00037 
00038 // ----------------------------------------------------------------
00039 #define UT_RGBCOLOR_PROXIMITY 45
00040 
00041 class ABI_EXPORT UT_ColorPatImpl
00042 {
00043 public:
00044     virtual ~UT_ColorPatImpl();
00045     virtual UT_ColorPatImpl * clone() const= 0;
00046 };
00047 
00048 class ABI_EXPORT UT_RGBColor
00049 {
00050 public:
00051     UT_RGBColor();
00052     UT_RGBColor(unsigned char, unsigned char, unsigned char, bool bTransparent = false);
00053     UT_RGBColor(const UT_RGBColor&);
00054     // take ownership
00055     UT_RGBColor(const UT_ColorPatImpl * pattern);
00056     ~UT_RGBColor();
00057     bool operator != (const UT_RGBColor &op1)
00058     {
00059         return (op1.m_red != m_red || op1.m_grn != m_grn || op1.m_blu != m_blu);
00060     }
00061 
00062     bool operator == (const UT_RGBColor &op1)
00063     {
00064         return (op1.m_red == m_red && op1.m_grn == m_grn && op1.m_blu == m_blu);
00065     }
00066 
00067     // returns true if the two colors are near each other in the RGB space
00068     bool operator %= (const UT_RGBColor &op1)
00069     {
00070         UT_uint32 iDiff = abs(m_red - op1.m_red) + abs(m_grn - op1.m_grn) + abs(m_blu - op1.m_blu);
00071         return (iDiff < UT_RGBCOLOR_PROXIMITY);
00072     }
00073 
00074     UT_RGBColor & operator ^= (const UT_RGBColor &op1)
00075     {
00076         m_red ^= op1.m_red;
00077         m_grn ^= op1.m_grn;
00078         m_blu ^= op1.m_blu;
00079         return *this;
00080     }
00081 
00082     UT_RGBColor & operator += (const unsigned char inc)
00083     {
00084         m_red += inc;
00085         m_grn += inc;
00086         m_blu += inc;
00087         return *this;
00088     }
00089 
00090     UT_RGBColor & operator += (const  UT_RGBColor &inc)
00091     {
00092         m_red += inc.m_red;
00093         m_grn += inc.m_grn;
00094         m_blu += inc.m_blu;
00095         return *this;
00096     }
00097 
00098     UT_RGBColor & operator -= (const  UT_RGBColor &inc)
00099     {
00100         m_red -= inc.m_red;
00101         m_grn -= inc.m_grn;
00102         m_blu -= inc.m_blu;
00103         return *this;
00104     }
00105 
00106     UT_RGBColor & operator=(const  UT_RGBColor &inc);
00107 
00108     bool isTransparent() const
00109     {
00110         return m_bIsTransparent;
00111     }
00112     bool setColor(const char * pszColor);
00113 
00114     bool isPattern() const
00115     {
00116         return m_patImpl != NULL;
00117     }
00118     const UT_ColorPatImpl *pattern() const
00119     {
00120         return m_patImpl;
00121     }
00122     // take ownership
00123     void setPattern(const UT_ColorPatImpl *p)
00124     {
00125         if(m_patImpl) {
00126             delete m_patImpl;
00127         }
00128         m_patImpl = p;
00129     }
00130 
00131     unsigned char m_red;
00132     unsigned char m_grn;
00133     unsigned char m_blu;
00134     bool m_bIsTransparent;
00135 private:
00136     const UT_ColorPatImpl * m_patImpl;
00137 };
00138 
00139 void UT_setColor(UT_RGBColor & col, unsigned char r, unsigned char g, unsigned char b, bool bTransparent = false);
00140 ABI_EXPORT void UT_parseColor(const char*, UT_RGBColor&);
00141 ABI_EXPORT std::string UT_colorToHex(const char*, bool bPrefix = false);
00142 
00143 class ABI_EXPORT UT_HashColor
00144 {
00145 private:
00146     char m_colorBuffer[8]; // format: "" or "#abc123" (i.e., '#' + 6 lower-case hex digits)
00147 
00148 public:
00149     UT_HashColor ();
00150     ~UT_HashColor ();
00151 
00152     const char * c_str() const
00153     { return m_colorBuffer; }
00154     /* The following 5 functions return a pointer to m_colorBuffer on success,
00155      * or 0 on failure (invalid or unknown color).
00156      */
00157     const char * setColor (unsigned char r, unsigned char g, unsigned char b);
00158     const char * setColor (const UT_RGBColor & color) { return setColor (color.m_red, color.m_grn, color.m_blu); }
00159     const char * setColor (const char * color); // try match hash (e.g., "#C01bB7") or name (e.g., "turquoise")
00160     const char * lookupNamedColor (const char * color_name); // "Orange" or "blue" or "LightGoldenRodYellow" or...
00161     const char * setHashIfValid (const char * color_hash);   // "ff0013" or "AD5FE6" or... (NOTE: no '#')
00162 
00163     const UT_RGBColor rgb (); // Call this *if* setColor () succeeds; otherwise defaults to black.
00164 };
00165 
00166 
00167 // Hack so we get AbiNativeWidget with an xp include
00168 #ifdef TOOLKIT_GTK_ALL
00169 #include "ut_unixColor.h"
00170 #else
00171 // TODO maintainers please fix their platform
00172 typedef void AbiNativeWidget;
00173 #endif
00174 
00175 #endif /* UTCOLOR_H */

Generated on Sun Feb 14 2021 for AbiWord by  doxygen 1.7.1