1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxColour
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
11 Flags for wxColour -> wxString conversion (see wxColour::GetAsString).
16 wxC2S_NAME
= 1, // return colour name, when possible
17 wxC2S_CSS_SYNTAX
= 2, // return colour in rgb(r,g,b) syntax
18 wxC2S_HTML_SYNTAX
= 4 // return colour in #rrggbb syntax
23 const unsigned char wxALPHA_TRANSPARENT
= 0;
24 const unsigned char wxALPHA_OPAQUE
= 0xff;
29 A colour is an object representing a combination of Red, Green, and Blue
30 (RGB) intensity values, and is used to determine drawing colours. See the
31 entry for wxColourDatabase for how a pointer to a predefined, named colour
32 may be returned instead of creating a new colour.
34 Valid RGB values are in the range 0 to 255.
36 You can retrieve the current system colour settings with wxSystemSettings.
42 - ::wxNullColour - An empty, invalid colour.
43 - ::wxTransparentColour - Valid but fully transparent colour (new in 2.9.1).
53 @see wxColourDatabase, wxPen, wxBrush, wxColourDialog, wxSystemSettings
55 class wxColour
: public wxObject
72 The alpha value. Alpha values range from 0 (wxALPHA_TRANSPARENT) to
75 wxColour(unsigned char red
, unsigned char green
, unsigned char blue
,
76 unsigned char alpha
= wxALPHA_OPAQUE
);
82 wxColour(const wxString
& colourName
);
88 wxColour(unsigned long colRGB
);
93 wxColour(const wxColour
& colour
);
96 Returns the alpha value, on platforms where alpha is not yet supported, this
97 always returns wxALPHA_OPAQUE.
99 virtual unsigned char Alpha() const;
102 Returns the blue intensity.
104 virtual unsigned char Blue() const;
107 Converts this colour to a wxString using the given flags.
109 The supported flags are @c wxC2S_NAME, to obtain the colour name
110 (e.g. wxColour(255,0,0) == "red"), @c wxC2S_CSS_SYNTAX, to obtain
111 the colour in the "rgb(r,g,b)" or "rgba(r,g,b,a)" syntax
112 (e.g. wxColour(255,0,0,85) == "rgba(255,0,0,0.333)"), and
113 @c wxC2S_HTML_SYNTAX, to obtain the colour as "#" followed by 6
114 hexadecimal digits (e.g. wxColour(255,0,0) == "#FF0000").
116 This function never fails and always returns a non-empty string but
117 asserts if the colour has alpha channel (i.e. is non opaque) but
118 @c wxC2S_CSS_SYNTAX (which is the only one supporting alpha) is not
123 virtual wxString
GetAsString(long flags
= wxC2S_NAME
| wxC2S_CSS_SYNTAX
) const;
127 Sets the RGB or RGBA colour values from a single 32 bit value.
129 The arguments @a colRGB and @a colRGBA should be of the form 0x00BBGGRR
130 and 0xAABBGGRR respectively where @c 0xRR, @c 0xGG, @c 0xBB and @c 0xAA
131 are the values of the red, blue, green and alpha components.
133 Notice the right-to-left order of components!
135 @see GetRGB(), GetRGBA()
139 void SetRGB(wxUint32 colRGB
);
140 void SetRGBA(wxUint32 colRGBA
);
145 Gets the RGB or RGBA colour values as a single 32 bit value.
147 The returned value is of the same form as expected by SetRGB() and
150 Notice that GetRGB() returns the value with 0 as its highest byte
151 independently of the value actually returned by Alpha(). So for a fully
152 opaque colour, the return value of GetRGBA() is @c 0xFFBBGGRR while
153 that of GetRGB() is @c 0x00BBGGRR.
157 wxUint32
GetRGB() const;
158 wxUint32
GetRGBA() const;
162 Returns a pixel value which is platform-dependent.
163 On Windows, a COLORREF is returned.
164 On X, an allocated pixel value is returned.
165 If the pixel is invalid (on X, unallocated), @c -1 is returned.
167 wxIntPtr
GetPixel() const;
170 Returns the green intensity.
172 virtual unsigned char Green() const;
175 Returns @true if the colour object is valid (the colour has been initialised
178 virtual bool IsOk() const;
181 Returns the red intensity.
183 virtual unsigned char Red() const;
187 Sets the RGB intensity values using the given values (first overload),
188 extracting them from the packed long (second overload), using the given
189 string (third overload).
191 When using third form, Set() accepts: colour names (those listed in
192 wxColourDatabase), the CSS-like @c "rgb(r,g,b)" or @c "rgba(r,g,b,a)" syntax
193 (case insensitive) and the HTML-like syntax: @c "#" followed by 6 hexadecimal
194 digits for red, green, blue components.
196 Returns @true if the conversion was successful, @false otherwise.
200 void Set(unsigned char red
, unsigned char green
,
202 unsigned char alpha
= wxALPHA_OPAQUE
);
203 void Set(unsigned long RGB
);
204 bool Set(const wxString
& str
);
208 Tests the inequality of two colours by comparing individual red, green, blue
209 colours and alpha values.
211 bool operator !=(const wxColour
& colour
) const;
214 Assignment operator, using a colour name to be found in the colour database.
216 @see wxColourDatabase
218 wxColour
& operator=(const wxColour
& colour
);
221 Tests the equality of two colours by comparing individual red, green, blue
222 colours and alpha values.
224 bool operator ==(const wxColour
& colour
) const;
227 Assign 0 or 255 to rgb out parameters.
230 static void MakeMono(unsigned char* r
, unsigned char* g
, unsigned char* b
, bool on
);
233 Create a disabled (dimmed) colour from (in/out) rgb parameters.
236 static void MakeDisabled(unsigned char* r
, unsigned char* g
, unsigned char* b
, unsigned char brightness
= 255);
239 Make a disabled version of this colour.
241 This method modifies the object in place and returns the object itself.
245 wxColour
& MakeDisabled(unsigned char brightness
= 255);
248 Create a grey colour from (in/out) rgb parameters using integer arithmetic.
251 static void MakeGrey(unsigned char* r
, unsigned char* g
, unsigned char* b
);
254 Create a grey colour from (in/out) rgb parameters using floating point arithmetic.
255 Defaults to using the standard ITU-T BT.601 when converting to YUV, where every pixel equals
256 (R * @a weight_r) + (G * @a weight_g) + (B * @a weight_b).
259 static void MakeGrey(unsigned char* r
, unsigned char* g
, unsigned char* b
,
260 double weight_r
, double weight_g
, double weight_b
);
263 Blend colour, taking alpha into account.
266 static unsigned char AlphaBlend(unsigned char fg
, unsigned char bg
, double alpha
);
269 ChangeLightness() is a utility function that simply darkens
270 or lightens a color, based on the specified percentage
271 ialpha of 0 would be completely black, 200 completely white
272 an ialpha of 100 returns the same colour
275 static void ChangeLightness(unsigned char* r
, unsigned char* g
, unsigned char* b
, int ialpha
);
278 wxColour wrapper for ChangeLightness(r,g,b,ialpha).
281 wxColour
ChangeLightness(int ialpha
) const;
285 /** @name Predefined colors. */
287 wxColour wxNullColour
;
288 wxColour wxTransparentColour
;
294 wxColour
* wxLIGHT_GREY
;
301 // ============================================================================
302 // Global functions/macros
303 // ============================================================================
305 /** @addtogroup group_funcmacro_misc */
309 Converts string to a wxColour best represented by the given string. Returns
312 @see wxToString(const wxColour&)
316 bool wxFromString(const wxString
& string
, wxColour
* colour
);
319 Converts the given wxColour into a string.
321 @see wxFromString(const wxString&, wxColour*)
325 wxString
wxToString(const wxColour
& colour
);