colour.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: colour.h
  3. // Purpose: interface of wxColour
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Flags for wxColour -> wxString conversion (see wxColour::GetAsString).
  9. @{
  10. */
  11. enum {
  12. wxC2S_NAME = 1, // return colour name, when possible
  13. wxC2S_CSS_SYNTAX = 2, // return colour in rgb(r,g,b) syntax
  14. wxC2S_HTML_SYNTAX = 4 // return colour in #rrggbb syntax
  15. };
  16. //@}
  17. const unsigned char wxALPHA_TRANSPARENT = 0;
  18. const unsigned char wxALPHA_OPAQUE = 0xff;
  19. /**
  20. @class wxColour
  21. A colour is an object representing a combination of Red, Green, and Blue
  22. (RGB) intensity values, and is used to determine drawing colours. See the
  23. entry for wxColourDatabase for how a pointer to a predefined, named colour
  24. may be returned instead of creating a new colour.
  25. Valid RGB values are in the range 0 to 255.
  26. You can retrieve the current system colour settings with wxSystemSettings.
  27. @library{wxcore}
  28. @category{gdi}
  29. @stdobjects
  30. - ::wxNullColour - An empty, invalid colour.
  31. - ::wxTransparentColour - Valid but fully transparent colour (new in 2.9.1).
  32. - ::wxBLACK
  33. - ::wxBLUE
  34. - ::wxCYAN
  35. - ::wxGREEN
  36. - ::wxYELLOW
  37. - ::wxLIGHT_GREY
  38. - ::wxRED
  39. - ::wxWHITE
  40. @see wxColourDatabase, wxPen, wxBrush, wxColourDialog, wxSystemSettings
  41. */
  42. class wxColour : public wxObject
  43. {
  44. public:
  45. /**
  46. Default constructor.
  47. */
  48. wxColour();
  49. /**
  50. @param red
  51. The red value.
  52. @param green
  53. The green value.
  54. @param blue
  55. The blue value.
  56. @param alpha
  57. The alpha value. Alpha values range from 0 (wxALPHA_TRANSPARENT) to
  58. 255 (wxALPHA_OPAQUE).
  59. */
  60. wxColour(unsigned char red, unsigned char green, unsigned char blue,
  61. unsigned char alpha = wxALPHA_OPAQUE);
  62. /**
  63. @param colourName
  64. The colour name.
  65. */
  66. wxColour(const wxString& colourName);
  67. /**
  68. @param colRGB
  69. A packed RGB value.
  70. */
  71. wxColour(unsigned long colRGB);
  72. /**
  73. Copy constructor.
  74. */
  75. wxColour(const wxColour& colour);
  76. /**
  77. Returns the alpha value, on platforms where alpha is not yet supported, this
  78. always returns wxALPHA_OPAQUE.
  79. */
  80. virtual unsigned char Alpha() const;
  81. /**
  82. Returns the blue intensity.
  83. */
  84. virtual unsigned char Blue() const;
  85. /**
  86. Converts this colour to a wxString using the given flags.
  87. The supported flags are @c wxC2S_NAME, to obtain the colour name
  88. (e.g. wxColour(255,0,0) == "red"), @c wxC2S_CSS_SYNTAX, to obtain
  89. the colour in the "rgb(r,g,b)" or "rgba(r,g,b,a)" syntax
  90. (e.g. wxColour(255,0,0,85) == "rgba(255,0,0,0.333)"), and
  91. @c wxC2S_HTML_SYNTAX, to obtain the colour as "#" followed by 6
  92. hexadecimal digits (e.g. wxColour(255,0,0) == "#FF0000").
  93. This function never fails and always returns a non-empty string but
  94. asserts if the colour has alpha channel (i.e. is non opaque) but
  95. @c wxC2S_CSS_SYNTAX (which is the only one supporting alpha) is not
  96. specified in flags.
  97. @since 2.7.0
  98. */
  99. virtual wxString GetAsString(long flags = wxC2S_NAME | wxC2S_CSS_SYNTAX) const;
  100. //@{
  101. /**
  102. Sets the RGB or RGBA colour values from a single 32 bit value.
  103. The arguments @a colRGB and @a colRGBA should be of the form 0x00BBGGRR
  104. and 0xAABBGGRR respectively where @c 0xRR, @c 0xGG, @c 0xBB and @c 0xAA
  105. are the values of the red, blue, green and alpha components.
  106. Notice the right-to-left order of components!
  107. @see GetRGB(), GetRGBA()
  108. @since 2.9.1
  109. */
  110. void SetRGB(wxUint32 colRGB);
  111. void SetRGBA(wxUint32 colRGBA);
  112. //@}
  113. //@{
  114. /**
  115. Gets the RGB or RGBA colour values as a single 32 bit value.
  116. The returned value is of the same form as expected by SetRGB() and
  117. SetRGBA().
  118. Notice that GetRGB() returns the value with 0 as its highest byte
  119. independently of the value actually returned by Alpha(). So for a fully
  120. opaque colour, the return value of GetRGBA() is @c 0xFFBBGGRR while
  121. that of GetRGB() is @c 0x00BBGGRR.
  122. @since 2.9.1
  123. */
  124. wxUint32 GetRGB() const;
  125. wxUint32 GetRGBA() const;
  126. //@}
  127. /**
  128. Returns a pixel value which is platform-dependent.
  129. On Windows, a COLORREF is returned.
  130. On X, an allocated pixel value is returned.
  131. If the pixel is invalid (on X, unallocated), @c -1 is returned.
  132. */
  133. wxIntPtr GetPixel() const;
  134. /**
  135. Returns the green intensity.
  136. */
  137. virtual unsigned char Green() const;
  138. /**
  139. Returns @true if the colour object is valid (the colour has been initialised
  140. with RGB values).
  141. */
  142. virtual bool IsOk() const;
  143. /**
  144. Returns the red intensity.
  145. */
  146. virtual unsigned char Red() const;
  147. //@{
  148. /**
  149. Sets the RGB intensity values using the given values (first overload),
  150. extracting them from the packed long (second overload), using the given
  151. string (third overload).
  152. When using third form, Set() accepts: colour names (those listed in
  153. wxColourDatabase), the CSS-like @c "rgb(r,g,b)" or @c "rgba(r,g,b,a)" syntax
  154. (case insensitive) and the HTML-like syntax: @c "#" followed by 6 hexadecimal
  155. digits for red, green, blue components.
  156. Returns @true if the conversion was successful, @false otherwise.
  157. @since 2.7.0
  158. */
  159. void Set(unsigned char red, unsigned char green,
  160. unsigned char blue,
  161. unsigned char alpha = wxALPHA_OPAQUE);
  162. void Set(unsigned long RGB);
  163. bool Set(const wxString& str);
  164. //@}
  165. /**
  166. Tests the inequality of two colours by comparing individual red, green, blue
  167. colours and alpha values.
  168. */
  169. bool operator !=(const wxColour& colour) const;
  170. /**
  171. Assignment operator, using a colour name to be found in the colour database.
  172. @see wxColourDatabase
  173. */
  174. wxColour& operator=(const wxColour& colour);
  175. /**
  176. Tests the equality of two colours by comparing individual red, green, blue
  177. colours and alpha values.
  178. */
  179. bool operator ==(const wxColour& colour) const;
  180. /**
  181. Assign 0 or 255 to rgb out parameters.
  182. @since 2.9.0
  183. */
  184. static void MakeMono(unsigned char* r, unsigned char* g, unsigned char* b, bool on);
  185. /**
  186. Create a disabled (dimmed) colour from (in/out) rgb parameters.
  187. @since 2.9.0
  188. */
  189. static void MakeDisabled(unsigned char* r, unsigned char* g, unsigned char* b, unsigned char brightness = 255);
  190. /**
  191. Make a disabled version of this colour.
  192. This method modifies the object in place and returns the object itself.
  193. @since 2.9.5
  194. */
  195. wxColour& MakeDisabled(unsigned char brightness = 255);
  196. /**
  197. Create a grey colour from (in/out) rgb parameters using integer arithmetic.
  198. @since 2.9.0
  199. */
  200. static void MakeGrey(unsigned char* r, unsigned char* g, unsigned char* b);
  201. /**
  202. Create a grey colour from (in/out) rgb parameters using floating point arithmetic.
  203. Defaults to using the standard ITU-T BT.601 when converting to YUV, where every pixel equals
  204. (R * @a weight_r) + (G * @a weight_g) + (B * @a weight_b).
  205. @since 2.9.0
  206. */
  207. static void MakeGrey(unsigned char* r, unsigned char* g, unsigned char* b,
  208. double weight_r, double weight_g, double weight_b);
  209. /**
  210. Blend colour, taking alpha into account.
  211. @since 2.9.0
  212. */
  213. static unsigned char AlphaBlend(unsigned char fg, unsigned char bg, double alpha);
  214. /**
  215. ChangeLightness() is a utility function that simply darkens
  216. or lightens a color, based on the specified percentage
  217. ialpha of 0 would be completely black, 200 completely white
  218. an ialpha of 100 returns the same colour
  219. @since 2.9.0
  220. */
  221. static void ChangeLightness(unsigned char* r, unsigned char* g, unsigned char* b, int ialpha);
  222. /**
  223. wxColour wrapper for ChangeLightness(r,g,b,ialpha).
  224. @since 2.9.0
  225. */
  226. wxColour ChangeLightness(int ialpha) const;
  227. };
  228. /** @name Predefined colors. */
  229. //@{
  230. wxColour wxNullColour;
  231. wxColour wxTransparentColour;
  232. wxColour* wxBLACK;
  233. wxColour* wxBLUE;
  234. wxColour* wxCYAN;
  235. wxColour* wxGREEN;
  236. wxColour* wxYELLOW;
  237. wxColour* wxLIGHT_GREY;
  238. wxColour* wxRED;
  239. wxColour* wxWHITE;
  240. //@}
  241. // ============================================================================
  242. // Global functions/macros
  243. // ============================================================================
  244. /** @addtogroup group_funcmacro_misc */
  245. //@{
  246. /**
  247. Converts string to a wxColour best represented by the given string. Returns
  248. @true on success.
  249. @see wxToString(const wxColour&)
  250. @header{wx/colour.h}
  251. */
  252. bool wxFromString(const wxString& string, wxColour* colour);
  253. /**
  254. Converts the given wxColour into a string.
  255. @see wxFromString(const wxString&, wxColour*)
  256. @header{wx/colour.h}
  257. */
  258. wxString wxToString(const wxColour& colour);
  259. //@}