TDE graphics utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kpcolor.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  13. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifndef __kp_color_h__
  24. #define __kp_color_h__
  25. #include <tqcolor.h>
  26. class TQDataStream;
  27. //
  28. // kpColor is an object-oriented abstraction of TQRgb, with the additional
  29. // restriction of following the KolourPaint convention of only supporting
  30. // totally transparent and totally opaque colors. It also provides better
  31. // error handling, reporting (noisy kdError()'s) and recovery.
  32. //
  33. // In general, you should pass around kpColor objects instead of TQRgb
  34. // and TQColor. Only convert an opaque kpColor to a TQColor (using toTQColor())
  35. // if you need to draw something onscreen. Constructing a kpColor object
  36. // from TQColor is probably wrong since onscreen representations of color
  37. // are not guaranteed to be faithful (due to TQColor color allocation).
  38. //
  39. class kpColor
  40. {
  41. public:
  42. kpColor ();
  43. kpColor (int red, int green, int blue, bool isTransparent = false);
  44. kpColor (const TQRgb &rgba);
  45. kpColor (const kpColor &rhs);
  46. friend TQDataStream &operator<< (TQDataStream &stream, const kpColor &color);
  47. friend TQDataStream &operator>> (TQDataStream &stream, kpColor &color);
  48. kpColor &operator= (const kpColor &rhs);
  49. bool operator== (const kpColor &rhs) const;
  50. bool operator!= (const kpColor &rhs) const;
  51. static int processSimilarity (double colorSimilarity);
  52. static const int Exact; // "isSimilarTo (rhs, kpColor::Exact)" == "== rhs"
  53. // Usage: isSimilarTo (rhs, kpColor::processSimilarity (.1)) checks for
  54. // Color Similarity within 10%
  55. bool isSimilarTo (const kpColor &rhs, int processedSimilarity) const;
  56. ~kpColor ();
  57. static const kpColor invalid;
  58. static const kpColor transparent;
  59. bool isValid () const;
  60. int red () const;
  61. int green () const;
  62. int blue () const;
  63. int alpha () const;
  64. bool isTransparent () const;
  65. bool isOpaque () const;
  66. // Cast operators will most likely result in careless conversions so
  67. // use explicit functions instead:
  68. TQRgb toTQRgb () const;
  69. // (only valid if isOpaque())
  70. // (const TQColor & return results in fewer color reallocations)
  71. const TQColor &toTQColor () const;
  72. TQColor maskColor () const;
  73. private:
  74. bool m_rgbaIsValid;
  75. TQRgb m_rgba;
  76. mutable bool m_colorCacheIsValid;
  77. mutable TQColor m_colorCache;
  78. };
  79. #endif // __kp_color_h__