colour.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/colour.h
  3. // Purpose: wxColourBase definition
  4. // Author: Julian Smart
  5. // Modified by: Francesco Montorsi
  6. // Created:
  7. // Copyright: Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_COLOUR_H_BASE_
  11. #define _WX_COLOUR_H_BASE_
  12. #include "wx/defs.h"
  13. #include "wx/gdiobj.h"
  14. class WXDLLIMPEXP_FWD_CORE wxColour;
  15. // A macro to define the standard wxColour constructors:
  16. //
  17. // It avoids the need to repeat these lines across all colour.h files, since
  18. // Set() is a virtual function and thus cannot be called by wxColourBase ctors
  19. #define DEFINE_STD_WXCOLOUR_CONSTRUCTORS \
  20. wxColour() { Init(); } \
  21. wxColour(ChannelType red, \
  22. ChannelType green, \
  23. ChannelType blue, \
  24. ChannelType alpha = wxALPHA_OPAQUE) \
  25. { Init(); Set(red, green, blue, alpha); } \
  26. wxColour(unsigned long colRGB) { Init(); Set(colRGB ); } \
  27. wxColour(const wxString& colourName) { Init(); Set(colourName); } \
  28. wxColour(const char *colourName) { Init(); Set(colourName); } \
  29. wxColour(const wchar_t *colourName) { Init(); Set(colourName); }
  30. // flags for wxColour -> wxString conversion (see wxColour::GetAsString)
  31. enum {
  32. wxC2S_NAME = 1, // return colour name, when possible
  33. wxC2S_CSS_SYNTAX = 2, // return colour in rgb(r,g,b) syntax
  34. wxC2S_HTML_SYNTAX = 4 // return colour in #rrggbb syntax
  35. };
  36. const unsigned char wxALPHA_TRANSPARENT = 0;
  37. const unsigned char wxALPHA_OPAQUE = 0xff;
  38. // a valid but fully transparent colour
  39. #define wxTransparentColour wxColour(0, 0, 0, wxALPHA_TRANSPARENT)
  40. #define wxTransparentColor wxTransparentColour
  41. // ----------------------------------------------------------------------------
  42. // wxVariant support
  43. // ----------------------------------------------------------------------------
  44. #if wxUSE_VARIANT
  45. #include "wx/variant.h"
  46. DECLARE_VARIANT_OBJECT_EXPORTED(wxColour,WXDLLIMPEXP_CORE)
  47. #endif
  48. //-----------------------------------------------------------------------------
  49. // wxColourBase: this class has no data members, just some functions to avoid
  50. // code redundancy in all native wxColour implementations
  51. //-----------------------------------------------------------------------------
  52. /* Transition from wxGDIObject to wxObject is incomplete. If your port does
  53. not need the wxGDIObject machinery to handle colors, please add it to the
  54. list of ports which do not need it.
  55. */
  56. #if defined( __WXMAC__ ) || defined( __WXMSW__ ) || defined( __WXPM__ ) || defined( __WXCOCOA__ )
  57. #define wxCOLOUR_IS_GDIOBJECT 0
  58. #else
  59. #define wxCOLOUR_IS_GDIOBJECT 1
  60. #endif
  61. class WXDLLIMPEXP_CORE wxColourBase : public
  62. #if wxCOLOUR_IS_GDIOBJECT
  63. wxGDIObject
  64. #else
  65. wxObject
  66. #endif
  67. {
  68. public:
  69. // type of a single colour component
  70. typedef unsigned char ChannelType;
  71. wxColourBase() {}
  72. virtual ~wxColourBase() {}
  73. // Set() functions
  74. // ---------------
  75. void Set(ChannelType red,
  76. ChannelType green,
  77. ChannelType blue,
  78. ChannelType alpha = wxALPHA_OPAQUE)
  79. { InitRGBA(red, green, blue, alpha); }
  80. // implemented in colourcmn.cpp
  81. bool Set(const wxString &str)
  82. { return FromString(str); }
  83. void Set(unsigned long colRGB)
  84. {
  85. // we don't need to know sizeof(long) here because we assume that the three
  86. // least significant bytes contain the R, G and B values
  87. Set((ChannelType)(0xFF & colRGB),
  88. (ChannelType)(0xFF & (colRGB >> 8)),
  89. (ChannelType)(0xFF & (colRGB >> 16)));
  90. }
  91. // accessors
  92. // ---------
  93. virtual ChannelType Red() const = 0;
  94. virtual ChannelType Green() const = 0;
  95. virtual ChannelType Blue() const = 0;
  96. virtual ChannelType Alpha() const
  97. { return wxALPHA_OPAQUE ; }
  98. // implemented in colourcmn.cpp
  99. virtual wxString GetAsString(long flags = wxC2S_NAME | wxC2S_CSS_SYNTAX) const;
  100. void SetRGB(wxUint32 colRGB)
  101. {
  102. Set((ChannelType)(0xFF & colRGB),
  103. (ChannelType)(0xFF & (colRGB >> 8)),
  104. (ChannelType)(0xFF & (colRGB >> 16)));
  105. }
  106. void SetRGBA(wxUint32 colRGBA)
  107. {
  108. Set((ChannelType)(0xFF & colRGBA),
  109. (ChannelType)(0xFF & (colRGBA >> 8)),
  110. (ChannelType)(0xFF & (colRGBA >> 16)),
  111. (ChannelType)(0xFF & (colRGBA >> 24)));
  112. }
  113. wxUint32 GetRGB() const
  114. { return Red() | (Green() << 8) | (Blue() << 16); }
  115. wxUint32 GetRGBA() const
  116. { return Red() | (Green() << 8) | (Blue() << 16) | (Alpha() << 24); }
  117. #if !wxCOLOUR_IS_GDIOBJECT
  118. virtual bool IsOk() const= 0;
  119. // older version, for backwards compatibility only (but not deprecated
  120. // because it's still widely used)
  121. bool Ok() const { return IsOk(); }
  122. #endif
  123. // manipulation
  124. // ------------
  125. // These methods are static because they are mostly used
  126. // within tight loops (where we don't want to instantiate wxColour's)
  127. static void MakeMono (unsigned char* r, unsigned char* g, unsigned char* b, bool on);
  128. static void MakeDisabled(unsigned char* r, unsigned char* g, unsigned char* b, unsigned char brightness = 255);
  129. static void MakeGrey (unsigned char* r, unsigned char* g, unsigned char* b); // integer version
  130. static void MakeGrey (unsigned char* r, unsigned char* g, unsigned char* b,
  131. double weight_r, double weight_g, double weight_b); // floating point version
  132. static unsigned char AlphaBlend (unsigned char fg, unsigned char bg, double alpha);
  133. static void ChangeLightness(unsigned char* r, unsigned char* g, unsigned char* b, int ialpha);
  134. wxColour ChangeLightness(int ialpha) const;
  135. wxColour& MakeDisabled(unsigned char brightness = 255);
  136. // old, deprecated
  137. // ---------------
  138. #if WXWIN_COMPATIBILITY_2_6
  139. static wxDEPRECATED( wxColour CreateByName(const wxString& name) );
  140. wxDEPRECATED( void InitFromName(const wxString& col) );
  141. #endif
  142. protected:
  143. // Some ports need Init() and while we don't, provide a stub so that the
  144. // ports which don't need it are not forced to define it
  145. void Init() { }
  146. virtual void
  147. InitRGBA(ChannelType r, ChannelType g, ChannelType b, ChannelType a) = 0;
  148. virtual bool FromString(const wxString& s);
  149. #if wxCOLOUR_IS_GDIOBJECT
  150. // wxColour doesn't use reference counted data (at least not in all ports)
  151. // so provide stubs for the functions which need to be defined if we do use
  152. // them
  153. virtual wxGDIRefData *CreateGDIRefData() const
  154. {
  155. wxFAIL_MSG( "must be overridden if used" );
  156. return NULL;
  157. }
  158. virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *WXUNUSED(data)) const
  159. {
  160. wxFAIL_MSG( "must be overridden if used" );
  161. return NULL;
  162. }
  163. #endif
  164. };
  165. // wxColour <-> wxString utilities, used by wxConfig, defined in colourcmn.cpp
  166. WXDLLIMPEXP_CORE wxString wxToString(const wxColourBase& col);
  167. WXDLLIMPEXP_CORE bool wxFromString(const wxString& str, wxColourBase* col);
  168. #if defined(__WXMSW__)
  169. #include "wx/msw/colour.h"
  170. #elif defined(__WXMOTIF__)
  171. #include "wx/motif/colour.h"
  172. #elif defined(__WXGTK20__)
  173. #include "wx/gtk/colour.h"
  174. #elif defined(__WXGTK__)
  175. #include "wx/gtk1/colour.h"
  176. #elif defined(__WXDFB__)
  177. #include "wx/generic/colour.h"
  178. #elif defined(__WXX11__)
  179. #include "wx/x11/colour.h"
  180. #elif defined(__WXMAC__)
  181. #include "wx/osx/colour.h"
  182. #elif defined(__WXCOCOA__)
  183. #include "wx/cocoa/colour.h"
  184. #elif defined(__WXPM__)
  185. #include "wx/os2/colour.h"
  186. #endif
  187. #define wxColor wxColour
  188. #endif // _WX_COLOUR_H_BASE_