home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qcolor.h < prev    next >
C/C++ Source or Header  |  2001-10-11  |  6KB  |  214 lines

  1. /****************************************************************************
  2. ** $Id:  qt/qcolor.h   3.0.0   edited Oct 5 04:39 $
  3. **
  4. ** Definition of QColor class
  5. **
  6. ** Created : 940112
  7. **
  8. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  9. **
  10. ** This file is part of the kernel module of the Qt GUI Toolkit.
  11. **
  12. ** This file may be distributed under the terms of the Q Public License
  13. ** as defined by Trolltech AS of Norway and appearing in the file
  14. ** LICENSE.QPL included in the packaging of this file.
  15. **
  16. ** This file may be distributed and/or modified under the terms of the
  17. ** GNU General Public License version 2 as published by the Free Software
  18. ** Foundation and appearing in the file LICENSE.GPL included in the
  19. ** packaging of this file.
  20. **
  21. ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
  22. ** licenses may use this file in accordance with the Qt Commercial License
  23. ** Agreement provided with the Software.
  24. **
  25. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  26. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27. **
  28. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  29. **   information about Qt Commercial License Agreements.
  30. ** See http://www.trolltech.com/qpl/ for QPL licensing information.
  31. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  32. **
  33. ** Contact info@trolltech.com if any conditions of this licensing are
  34. ** not clear to you.
  35. **
  36. **********************************************************************/
  37.  
  38. #ifndef QCOLOR_H
  39. #define QCOLOR_H
  40.  
  41. #ifndef QT_H
  42. #include "qwindowdefs.h"
  43. #endif // QT_H
  44.  
  45. const QRgb  RGB_MASK    = 0x00ffffff;        // masks RGB values
  46.  
  47. Q_EXPORT inline int qRed( QRgb rgb )        // get red part of RGB
  48. { return (int)((rgb >> 16) & 0xff); }
  49.  
  50. Q_EXPORT inline int qGreen( QRgb rgb )        // get green part of RGB
  51. { return (int)((rgb >> 8) & 0xff); }
  52.  
  53. Q_EXPORT inline int qBlue( QRgb rgb )        // get blue part of RGB
  54. { return (int)(rgb & 0xff); }
  55.  
  56. Q_EXPORT inline int qAlpha( QRgb rgb )        // get alpha part of RGBA
  57. { return (int)((rgb >> 24) & 0xff); }
  58.  
  59. Q_EXPORT inline QRgb qRgb( int r, int g, int b )// set RGB value
  60. { return (0xff << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); }
  61.  
  62. Q_EXPORT inline QRgb qRgba( int r, int g, int b, int a )// set RGBA value
  63. { return ((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); }
  64.  
  65. Q_EXPORT inline int qGray( int r, int g, int b )// convert R,G,B to gray 0..255
  66. { return (r*11+g*16+b*5)/32; }
  67.  
  68. Q_EXPORT inline int qGray( QRgb rgb )        // convert RGB to gray 0..255
  69. { return qGray( qRed(rgb), qGreen(rgb), qBlue(rgb) ); }
  70.  
  71.  
  72. class Q_EXPORT QColor
  73. {
  74. public:
  75.     enum Spec { Rgb, Hsv };
  76.  
  77.     QColor();
  78.     QColor( int r, int g, int b );
  79.     QColor( int x, int y, int z, Spec );
  80.     QColor( QRgb rgb, uint pixel=0xffffffff);
  81.     QColor( const QString& name );
  82.     QColor( const char *name );
  83.     QColor( const QColor & );
  84.     QColor &operator=( const QColor & );
  85.  
  86.     bool   isValid() const;
  87.     bool   isDirty() const;
  88. #ifndef QT_NO_SPRINTF
  89.     QString name() const;
  90. #endif    
  91.     void   setNamedColor( const QString& name );
  92.  
  93.     void   rgb( int *r, int *g, int *b ) const;
  94.     QRgb   rgb()    const;
  95.     void   setRgb( int r, int g, int b );
  96.     void   setRgb( QRgb rgb );
  97.  
  98.     int       red()    const;
  99.     int       green()  const;
  100.     int       blue()   const;
  101.  
  102.     void   hsv( int *h, int *s, int *v ) const;
  103.     void   getHsv( int &h, int &s, int &v ) const { hsv( &h, &s, &v ); }
  104.     void   setHsv( int h, int s, int v );
  105.  
  106.     QColor light( int f = 150 ) const;
  107.     QColor dark( int f = 200 )    const;
  108.  
  109.     bool   operator==( const QColor &c ) const;
  110.     bool   operator!=( const QColor &c ) const;
  111.  
  112.     uint   alloc();
  113.     uint   pixel()  const;
  114.  
  115.     static int  maxColors();
  116.     static int  numBitPlanes();
  117.  
  118.     static int  enterAllocContext();
  119.     static void leaveAllocContext();
  120.     static int  currentAllocContext();
  121.     static void destroyAllocContext( int );
  122.  
  123. #if defined(Q_WS_WIN)
  124.     static const QRgb* palette( int* numEntries = 0 );
  125.     static int setPaletteEntries( const QRgb* entries, int numEntries,
  126.                   int base = -1 );
  127.     static HPALETTE hPal()  { return hpal; }
  128.     static uint    realizePal( QWidget * );
  129. #endif
  130.  
  131.     static void initialize();
  132.     static void cleanup();
  133.  
  134.     enum { Dirt = 0x44495254, Invalid = 0x49000000 };
  135.  
  136. private:
  137.     void setSystemNamedColor( const QString& name );
  138.     void setPixel( uint pixel );
  139.     static void initGlobalColors();
  140.     static uint argbToPix32(QRgb);
  141.     static QColor* globalColors();
  142.     static bool color_init;
  143.     static bool globals_init;
  144. #if defined(Q_WS_WIN)
  145.     static HPALETTE hpal;
  146. #endif
  147.     static enum ColorModel { d8, d32 } colormodel;
  148.     union {
  149.     QRgb argb;
  150.     struct {
  151.         QRgb argb;
  152.         uchar pix;
  153.         uchar invalid;
  154.         uchar dirty;
  155.         uchar direct;
  156.     } d8;
  157.     struct D32 {
  158.         QRgb argb;
  159.         uint pix;
  160.         bool invalid() const { return argb == QColor::Invalid && pix == QColor::Dirt; }
  161.         bool probablyDirty() const { return pix == QColor::Dirt; }
  162.     } d32;
  163.     } d;
  164. };
  165.  
  166.  
  167. inline QColor::QColor()
  168. { d.d32.argb = Invalid; d.d32.pix = Dirt; }
  169.  
  170. inline QColor::QColor( int r, int g, int b )
  171. { setRgb( r, g, b ); }
  172.  
  173. inline QRgb QColor::rgb() const
  174. { return d.argb; }
  175.  
  176. inline int QColor::red() const
  177. { return qRed(d.argb); }
  178.  
  179. inline int QColor::green() const
  180. { return qGreen(d.argb); }
  181.  
  182. inline int QColor::blue() const
  183. { return qBlue(d.argb); }
  184.  
  185. inline bool QColor::isValid() const
  186. {
  187.     if ( colormodel == d8 )
  188.     return !d.d8.invalid;
  189.     else
  190.     return !d.d32.invalid();
  191. }
  192.  
  193. inline bool QColor::operator==( const QColor &c ) const
  194. {
  195.     return d.argb == c.d.argb && isValid() == c.isValid();
  196. }
  197.  
  198. inline bool QColor::operator!=( const QColor &c ) const
  199. {
  200.     return !operator==(c);
  201. }
  202.  
  203.  
  204. /*****************************************************************************
  205.   QColor stream functions
  206.  *****************************************************************************/
  207.  
  208. #ifndef QT_NO_DATASTREAM
  209. Q_EXPORT QDataStream &operator<<( QDataStream &, const QColor & );
  210. Q_EXPORT QDataStream &operator>>( QDataStream &, QColor & );
  211. #endif
  212.  
  213. #endif // QCOLOR_H
  214.