home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / WINSYS.PAK / COLOR.H next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  13.0 KB  |  637 lines

  1. //----------------------------------------------------------------------------
  2. // Borland WinSys Library
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.8  $
  6. //
  7. // Definition of windowing system color classes
  8. //----------------------------------------------------------------------------
  9. #if !defined(WINSYS_COLOR_H)
  10. #define WINSYS_COLOR_H
  11.  
  12. #if !defined(WINSYS_DEFS_H)
  13. # include <winsys/defs.h>
  14. #endif
  15. #if !defined(CLASSLIB_OBJSTRM_H)
  16. # include <classlib/objstrm.h>    // Need persist streaming classes & operators
  17. #endif
  18.  
  19. #if defined(BI_PLAT_MSW)
  20. # if !defined(WINSYS_WSYSINC_H)
  21. #   include <winsys/wsysinc.h>
  22. # endif
  23. #else
  24.  
  25. #if defined(BI_NAMESPACE)
  26. namespace ClassLib {
  27. #endif
  28.  
  29. //
  30. // Simulate Windows structures
  31. //
  32.  
  33.   typedef uint32 COLORREF;
  34.  
  35.   struct tagPALETTEENTRY {
  36.     uint8    peRed;
  37.     uint8    peGreen;
  38.     uint8    peBlue;
  39.     uint8    peFlags;
  40.   };
  41. # include <pshpack1.h>
  42.   struct tagRGBTRIPLE {
  43.     uint8    rgbtBlue;
  44.     uint8    rgbtGreen;
  45.     uint8    rgbtRed;
  46.   };
  47. # include <poppack.h>
  48.   struct tagRGBQUAD {
  49.     uint8    rgbBlue;
  50.     uint8    rgbGreen;
  51.     uint8    rgbRed;
  52.     uint8    rgbReserved;
  53. };
  54.  
  55. #if defined(BI_NAMESPACE)
  56. }       // namespace ClassLib
  57. #endif
  58.  
  59. #endif
  60.  
  61. #if defined(BI_NAMESPACE)
  62. namespace ClassLib {
  63. #endif
  64.  
  65. //
  66. // Functions to convert number of bits to number of palette colors and back,
  67. // and build an RGB COLORREF
  68. //
  69. long _WSYSFUNC NColors(uint16 bitCount);
  70. uint16 _WSYSFUNC NBits(long colors);
  71.  
  72. //
  73. // Convert the red, green, and blue components into a COLORREF.
  74. //
  75. inline COLORREF MkRGB(uint r, uint g, uint b)
  76. {
  77.   return COLORREF((r & 0xFF) |
  78.                   ((g & 0xFF) << 8) |
  79.                   ((uint32(b) & 0xFF) << 16)
  80.                  );
  81. }
  82.  
  83. //
  84. // class TColor
  85. // ~~~~~ ~~~~~~
  86. // Class wrapper for management of color values.
  87. // Internally, all colors are stored as a COLORREF.
  88. //
  89. class _WSYSCLASS TColor {
  90.   public:
  91.     // Constructors
  92.     //
  93.     TColor();
  94.     TColor(const TColor& src);
  95.  
  96.     TColor(COLORREF value);
  97.     TColor(long value);
  98.  
  99.     TColor(int r, int g, int b);
  100.     TColor(int r, int g, int b, int f);
  101.     TColor(int index);
  102.  
  103.     TColor(const PALETTEENTRY far& pe);
  104.     TColor(const RGBQUAD far& q);
  105.     TColor(const RGBTRIPLE far& t);
  106.  
  107.     // Conversion operator & value accessor
  108.     //
  109.     operator      COLORREF() const;
  110.     COLORREF      GetValue() const;
  111.     void          SetValue(const COLORREF& value);
  112.  
  113.     // Comparison operators
  114.     //
  115.     bool operator ==(const TColor& other) const;
  116.     bool operator !=(const TColor& other) const;
  117.     bool operator ==(COLORREF cr) const;
  118.     bool operator !=(COLORREF cr) const;
  119.  
  120.     TColor& operator =(const TColor& src);
  121.  
  122.     // Accessors
  123.     //
  124.     int           Index() const;
  125.     TColor        Rgb() const;
  126.     TColor        PalIndex() const;
  127.     TColor        PalRelative() const;
  128.  
  129.     uint8         Red() const;
  130.     uint8         Green() const;
  131.     uint8         Blue() const;
  132.     uint8         Flags() const;
  133.     bool          IsSysColor() const;
  134.     bool          IsSpecialColor() const;
  135.  
  136.     // Common RGB color values
  137.     //
  138.     static const TColor  Black;
  139.     static const TColor  LtGray;
  140.     static const TColor  Gray;
  141.     static const TColor  LtRed;
  142.     static const TColor  LtGreen;
  143.     static const TColor  LtYellow;
  144.     static const TColor  LtBlue;
  145.     static const TColor  LtMagenta;
  146.     static const TColor  LtCyan;
  147.     static const TColor  White;
  148.  
  149.     // Special color values
  150.     //
  151.     static const TColor  None;         // not-a-color
  152.     static const TColor  Transparent;  // a non-painting color
  153.  
  154.     // Symbolic system color values. Perform GetSysColor() on conversion to
  155.     // COLORREF
  156.     //
  157.     static const TColor  SysScrollbar;
  158.     static const TColor  SysDesktop;
  159.     static const TColor  SysActiveCaption;
  160.     static const TColor  SysInactiveCaption;
  161.     static const TColor  SysMenu;
  162.     static const TColor  SysWindow;
  163.     static const TColor  SysWindowFrame;
  164.     static const TColor  SysMenuText;
  165.     static const TColor  SysWindowText;
  166.     static const TColor  SysCaptionText;
  167.     static const TColor  SysActiveBorder;
  168.     static const TColor  SysInactiveBorder;
  169.     static const TColor  SysAppWorkspace;
  170.     static const TColor  SysHighlight;
  171.     static const TColor  SysHighlightText;
  172.     static const TColor  Sys3dFace;
  173.     static const TColor  Sys3dShadow;
  174.     static const TColor  SysGrayText;
  175.     static const TColor  SysBtnText;
  176.     static const TColor  SysInactiveCaptionText;
  177.     static const TColor  Sys3dHilight;
  178.     static const TColor  Sys3dDkShadow;
  179.     static const TColor  Sys3dLight;
  180.     static const TColor  SysInfoText;
  181.     static const TColor  SysInfoBk;
  182.  
  183.   private:
  184.     COLORREF    Value;          // The 32bit color value type (not a struct)
  185.  
  186.   friend ipstream& operator >>(ipstream& is, TColor& c);
  187.   friend opstream& operator <<(opstream& os, const TColor& c);
  188. };
  189.  
  190.  
  191. //
  192. // class TPaletteEntry
  193. // ~~~~~ ~~~~~~~~~~~~~
  194. // Wrapper for Windows' PALETTEENTRY type
  195. //
  196. class TPaletteEntry : public tagPALETTEENTRY {
  197.   public:
  198.     TPaletteEntry(int r, int g, int b, int f = 0);
  199.     TPaletteEntry(const TColor& c);
  200.  
  201.     bool operator ==(COLORREF cr) const;
  202. };
  203.  
  204.  
  205. //
  206. // class TRgbQuad
  207. // ~~~~~ ~~~~~~~~
  208. // Wrapper for Windows' RBGQUAD type
  209. //
  210. class TRgbQuad : public tagRGBQUAD {
  211.   public:
  212.     TRgbQuad(int r, int g, int b);
  213.     TRgbQuad(const TColor& c);
  214.     TRgbQuad(const RGBQUAD far& q);
  215.  
  216.     bool operator ==(COLORREF cr) const;
  217. };
  218.  
  219.  
  220. //
  221. // class TRgbTriple
  222. // ~~~~~ ~~~~~~~~~~
  223. // Wrapper for Windows' RBGTRIPLE type
  224. //
  225. class TRgbTriple : public tagRGBTRIPLE {
  226.   public:
  227.     TRgbTriple(int r, int g, int b);
  228.     TRgbTriple(const TColor& c);
  229.     TRgbTriple(const RGBTRIPLE far& t);
  230.  
  231.     bool operator ==(COLORREF cr) const;
  232. };
  233.  
  234. #if defined(BI_NAMESPACE)
  235. }     // namespace ClassLib
  236. #endif
  237.  
  238. //----------------------------------------------------------------------------
  239. // Inline Implementations
  240. //
  241.  
  242. #if defined(BI_NAMESPACE)
  243. namespace ClassLib {
  244. #endif
  245. //
  246. // Extract the color value from a persistent input stream.
  247. //
  248. inline ipstream& operator >>(ipstream& is, TColor& c)
  249. {
  250.   is >> c.Value;
  251.   return is;
  252. }
  253.  
  254. //
  255. // Insert the color value into a persistent output stream.
  256. //
  257. inline opstream& operator <<(opstream& os, const TColor& c)
  258. {
  259.   os << c.Value;
  260.   return os;
  261. }
  262.  
  263. #if defined(BI_NAMESPACE)
  264. }     // namespace ClassLib
  265. #endif
  266.  
  267. //
  268. // Construct a null color.
  269. //
  270. inline
  271. TColor::TColor()
  272. :
  273.   Value(0)
  274. {
  275. }
  276.  
  277. //
  278. // Construct a color as a copy of another color.
  279. //
  280. inline
  281. TColor::TColor(const TColor& src)
  282. :
  283.   Value(src.Value)
  284. {
  285. }
  286.  
  287. //
  288. // Construct a color with an existing COLORREF value.
  289. //
  290. inline
  291. TColor::TColor(COLORREF value)
  292. :
  293.   Value(value)
  294. {
  295. }
  296.  
  297. //
  298. // Construct a color given a particular system value representation.
  299. //
  300. inline
  301. TColor::TColor(long value)
  302. :
  303.   Value((COLORREF)value)
  304. {
  305. }
  306.  
  307. //
  308. // Construct a color given the red, green, and blue components of color.
  309. //
  310. inline
  311. TColor::TColor(int r, int g, int b)
  312. :
  313.   Value(MkRGB(r, g, b))
  314. {
  315. }
  316.  
  317. //
  318. // Construct a color given red, green, blue, and flag components of color.
  319. // The flag component is defined in the Windows SDK reference.
  320. //
  321. inline
  322. TColor::TColor(int r, int g, int b, int f)
  323. :
  324.   Value(((uint32) f << 24) | MkRGB(r, g, b))
  325. {
  326. }
  327.  
  328. //
  329. // Construct a color that is an index into a palette.
  330. //
  331. inline
  332. TColor::TColor(int index)
  333. :
  334.   Value(PALETTEINDEX(index))
  335. {
  336. }
  337.  
  338. //
  339. // Construct a color based on an existing PALETTEENTRY.
  340. //
  341. inline
  342. TColor::TColor(const PALETTEENTRY far& pe)
  343. :
  344.   Value(MkRGB(pe.peRed, pe.peGreen, pe.peBlue))
  345. {
  346. }
  347.  
  348. //
  349. // Construct a color based on an existing RGBQUAD.
  350. //
  351. inline
  352. TColor::TColor(const RGBQUAD far& q)
  353. :
  354.   Value(MkRGB(q.rgbRed, q.rgbGreen, q.rgbBlue))
  355. {
  356. }
  357.  
  358. //
  359. // Construct a color based on an existing RGBTRIPLE.
  360. //
  361. inline
  362. TColor::TColor(const RGBTRIPLE far& t)
  363. :
  364.   Value(MkRGB(t.rgbtRed, t.rgbtGreen, t.rgbtBlue))
  365. {
  366. }
  367.  
  368. //
  369. // Convert the stored color into a COLORREF.
  370. //
  371. inline
  372. TColor::operator COLORREF() const
  373. {
  374.   return GetValue();
  375. }
  376.  
  377. //
  378. // Return true if two colors are equal.
  379. // This function compares between two binary representation of colors, it
  380. // does not compare colors logically.
  381. //
  382. // For example, if palette entry 4 is solid red (rgb components (255, 0, 0)),
  383. // The following will return false:
  384. //   if (TColor(4) == TColor(255, 0, 0))
  385. //     /* colors match */
  386. //   else
  387. //     /* colors do not match */
  388. //
  389. //  To actually compare the RGB values of two TColor objects, use the
  390. //  operator == (COLORREF). For example,
  391. //  TColor colorA, colorB;
  392. //  if (colorA == colorB.GetValue()) {
  393. //      // ColorA & ColorB
  394. //  }
  395. //
  396. inline bool
  397. TColor::operator ==(const TColor& other) const
  398. {
  399.   return Value == other.Value;
  400. }
  401.  
  402. //
  403. // Return true if two colors are not equal.
  404. // See additional comments in TColor::operator ==.
  405. //
  406. inline bool
  407. TColor::operator !=(const TColor& other) const
  408. {
  409.   return Value != other.Value;
  410. }
  411.  
  412. //
  413. // Return true if this color matches a COLORREF.
  414. // See additional comments in TColor::operator ==.
  415. //
  416. inline bool
  417. TColor::operator ==(COLORREF cr) const
  418. {
  419.   return GetValue() == cr;
  420. }
  421.  
  422. //
  423. // Return true if this color does not match a COLORREF.
  424. // See additional comments in TColor::operator ==.
  425. //
  426. inline bool
  427. TColor::operator !=(COLORREF cr) const
  428. {
  429.   return GetValue() != cr;
  430. }
  431.  
  432. //
  433. // Change the color after it has been constructed.
  434. //
  435. inline void
  436. TColor::SetValue(const COLORREF& value)
  437. {
  438.   Value = value;
  439. }
  440.  
  441. //
  442. // Set the value of color after it has been constructed.
  443. //
  444. inline TColor&
  445. TColor::operator =(const TColor& src)
  446. {
  447.   Value = src.Value;
  448.   return *this;
  449. }
  450.  
  451. //
  452. // Return the index of the palette entry.
  453. //
  454. inline int
  455. TColor::Index() const
  456. {
  457.   return (int)Value & 0xFFFF;
  458. }
  459.  
  460. //
  461. // Return the RGB color value.
  462. //
  463. inline TColor
  464. TColor::Rgb() const
  465. {
  466.   return GetValue();
  467. }
  468.  
  469. //
  470. // Return the palette index.
  471. // Assumes that the color really is a palette index
  472. //
  473. inline TColor
  474. TColor::PalIndex() const
  475. {
  476.   return (COLORREF)Index() | 0x01000000UL;
  477. }
  478.  
  479. //
  480. // Return the palette relative entry.
  481. //
  482. inline TColor
  483. TColor::PalRelative() const
  484. {
  485.   return Rgb() | 0x02000000UL;
  486. }
  487.  
  488. //
  489. // Return the flags entry.
  490. //
  491. inline uint8
  492. TColor::Flags() const
  493. {
  494.   return (uint8)(uint16)(Value >> 24);
  495. }
  496.  
  497. //
  498. // Return true if the color is a system color.
  499. //
  500. inline bool
  501. TColor::IsSysColor() const
  502. {
  503.   return (Value & 0xFF000000UL) == 0x80000000UL;
  504. }
  505.  
  506. //
  507. // Return true if the color is a special color (currently None or Transparent)
  508. //
  509. inline bool
  510. TColor::IsSpecialColor() const
  511. {
  512.   return (Value & 0xF0000000UL) == 0xF0000000UL;
  513. }
  514.  
  515. //
  516. // Construct a palette entry from the red, green, blue and flag components.
  517. //
  518. inline
  519. TPaletteEntry::TPaletteEntry(int r, int g, int b, int f)
  520. {
  521.   peRed = (uint8)(uint16)r;
  522.   peGreen = (uint8)(uint16)g;
  523.   peBlue = (uint8)(uint16)b;
  524.   peFlags = (uint8)(uint16)f;
  525. }
  526.  
  527. //
  528. // Construct a palette entry from a TColor.
  529. //
  530. inline
  531. TPaletteEntry::TPaletteEntry(const TColor& c)
  532. {
  533.   peRed = c.Red();
  534.   peGreen = c.Green();
  535.   peBlue = c.Blue();
  536.   peFlags = c.Flags();
  537. }
  538.  
  539. //
  540. // Returns true if the palette entries have the same color components.
  541. //
  542. inline bool
  543. TPaletteEntry::operator ==(COLORREF cr) const
  544. {
  545.   return peRed == GetRValue(cr) &&
  546.          peGreen == GetGValue(cr) &&
  547.          peBlue == GetBValue(cr);
  548. }
  549.  
  550. //
  551. // Construct an RGB quad from the red, green and blue components
  552. //
  553. inline
  554. TRgbQuad::TRgbQuad(int r, int g, int b)
  555. {
  556.   rgbRed = (uint8)(uint16)r;
  557.   rgbGreen = (uint8)(uint16)g;
  558.   rgbBlue = (uint8)(uint16)b;
  559.   rgbReserved = 0;
  560. }
  561.  
  562. //
  563. // Construct an RGB quad from a TColor.
  564. //
  565. inline
  566. TRgbQuad::TRgbQuad(const TColor& c)
  567. {
  568.   rgbRed = c.Red();
  569.   rgbGreen = c.Green();
  570.   rgbBlue = c.Blue();
  571.   rgbReserved = 0;
  572. }
  573.  
  574. //
  575. // Wrap an object around an existing RGBQUAD.
  576. //
  577. inline
  578. TRgbQuad::TRgbQuad(const RGBQUAD far& q)
  579. {
  580.   *(RGBQUAD*)this = q;
  581. }
  582.  
  583. //
  584. // Return true if the RGBQUAD has the same color components.
  585. //
  586. inline bool
  587. TRgbQuad::operator ==(COLORREF cr) const
  588. {
  589.   return rgbRed == GetRValue(cr) &&
  590.          rgbGreen == GetGValue(cr) &&
  591.          rgbBlue == GetBValue(cr);
  592. }
  593.  
  594. //
  595. // Construct an RGB triple from its red, green, and blue components.
  596. //
  597. inline
  598. TRgbTriple::TRgbTriple(int r, int g, int b)
  599. {
  600.   rgbtRed = (uint8)(uint16)r;
  601.   rgbtGreen = (uint8)(uint16)g;
  602.   rgbtBlue = (uint8)(uint16)b;
  603. }
  604.  
  605. //
  606. // Construct an RGB triple from a TColor.
  607. //
  608. inline
  609. TRgbTriple::TRgbTriple(const TColor& c)
  610. {
  611.   rgbtRed = c.Red();
  612.   rgbtGreen = c.Green();
  613.   rgbtBlue = c.Blue();
  614. }
  615.  
  616. //
  617. // Wrap an object around an existing RGBTRIPLE.
  618. //
  619. inline
  620. TRgbTriple::TRgbTriple(const RGBTRIPLE far& t)
  621. {
  622.   *(RGBTRIPLE*)this = t;
  623. }
  624.  
  625. //
  626. // Return true if the triple match color components.
  627. //
  628. inline bool
  629. TRgbTriple::operator ==(COLORREF cr) const
  630. {
  631.   return rgbtRed == GetRValue(cr) &&
  632.          rgbtGreen == GetGValue(cr) &&
  633.          rgbtBlue == GetBValue(cr);
  634. }
  635.  
  636. #endif  // WINSYS_COLOR_H
  637.