]>
Commit | Line | Data |
---|---|---|
40989e46 WS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/colourcmn.cpp | |
3 | // Purpose: wxColourBase implementation | |
4 | // Author: Francesco Montorsi | |
5 | // Modified by: | |
6 | // Created: 20/4/2006 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Francesco Montorsi | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | ||
13 | // For compilers that support precompilation, includes "wx.h". | |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #include "wx/colour.h" | |
21 | ||
e4db172a WS |
22 | #ifndef WX_PRECOMP |
23 | #include "wx/log.h" | |
7270921d | 24 | #include "wx/utils.h" |
dd05139a | 25 | #include "wx/gdicmn.h" |
193d0c93 | 26 | #include "wx/wxcrtvararg.h" |
e4db172a | 27 | #endif |
6b5d2431 | 28 | |
6f5d7825 RR |
29 | #if wxUSE_VARIANT |
30 | IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxColour,WXDLLEXPORT) | |
31 | #endif | |
40989e46 | 32 | |
28953245 SC |
33 | |
34 | // ---------------------------------------------------------------------------- | |
35 | // XTI | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | #if wxUSE_EXTENDED_RTTI | |
39 | ||
40 | #include <string.h> | |
41 | ||
42 | template<> void wxStringReadValue(const wxString &s, wxColour &data ) | |
43 | { | |
44 | if ( !data.Set(s) ) | |
45 | { | |
46 | wxLogError(_("String To Colour : Incorrect colour specification : %s"), | |
47 | s.c_str() ); | |
48 | data = wxNullColour; | |
49 | } | |
50 | } | |
51 | ||
52 | template<> void wxStringWriteValue(wxString &s, const wxColour &data ) | |
53 | { | |
54 | s = data.GetAsString(wxC2S_HTML_SYNTAX); | |
55 | } | |
56 | ||
57 | wxTO_STRING_IMP( wxColour ) | |
58 | wxFROM_STRING_IMP( wxColour ) | |
59 | ||
60 | wxIMPLEMENT_DYNAMIC_CLASS_WITH_COPY_AND_STREAMERS_XTI( wxColour, wxObject, \ | |
61 | "wx/colour.h", &wxTO_STRING( wxColour ), &wxFROM_STRING( wxColour )) | |
e765d7ee | 62 | //WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxColour>) |
28953245 SC |
63 | wxBEGIN_PROPERTIES_TABLE(wxColour) |
64 | wxREADONLY_PROPERTY( Red, unsigned char, Red, wxEMPTY_PARAMETER_VALUE, \ | |
65 | 0 /*flags*/, wxT("Helpstring"), wxT("group")) | |
66 | wxREADONLY_PROPERTY( Green, unsigned char, Green, wxEMPTY_PARAMETER_VALUE, \ | |
67 | 0 /*flags*/, wxT("Helpstring"), wxT("group")) | |
68 | wxREADONLY_PROPERTY( Blue, unsigned char, Blue, wxEMPTY_PARAMETER_VALUE, \ | |
69 | 0 /*flags*/, wxT("Helpstring"), wxT("group")) | |
70 | wxEND_PROPERTIES_TABLE() | |
71 | ||
72 | wxDIRECT_CONSTRUCTOR_3( wxColour, unsigned char, Red, \ | |
73 | unsigned char, Green, unsigned char, Blue ) | |
74 | ||
75 | wxEMPTY_HANDLERS_TABLE(wxColour) | |
76 | #else | |
77 | ||
78 | #if wxCOLOUR_IS_GDIOBJECT | |
79 | wxIMPLEMENT_DYNAMIC_CLASS(wxColour, wxGDIObject) | |
80 | #else | |
81 | wxIMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject) | |
82 | #endif | |
83 | ||
84 | #endif | |
85 | ||
40989e46 WS |
86 | // ============================================================================ |
87 | // wxString <-> wxColour conversions | |
88 | // ============================================================================ | |
89 | ||
e86d4e59 | 90 | bool wxColourBase::FromString(const wxString& str) |
40989e46 | 91 | { |
e86d4e59 | 92 | if ( str.empty() ) |
40989e46 WS |
93 | return false; // invalid or empty string |
94 | ||
b534968d | 95 | if ( wxStrnicmp(str, wxT("RGB"), 3) == 0 ) |
40989e46 | 96 | { |
7270921d | 97 | // CSS-like RGB specification |
b534968d | 98 | // according to http://www.w3.org/TR/css3-color/#colorunits |
7270921d | 99 | // values outside 0-255 range are allowed but should be clipped |
b534968d VZ |
100 | int red, green, blue, |
101 | alpha = wxALPHA_OPAQUE; | |
102 | if ( str.length() > 3 && (str[3] == wxT('a') || str[3] == wxT('A')) ) | |
103 | { | |
104 | float a; | |
105 | // TODO: use locale-independent function | |
106 | if ( wxSscanf(str.wx_str() + 4, wxT("( %d , %d , %d , %f )"), | |
107 | &red, &green, &blue, &a) != 4 ) | |
108 | return false; | |
109 | ||
110 | alpha = wxRound(a * 255); | |
111 | } | |
112 | else // no 'a' following "rgb" | |
113 | { | |
114 | if ( wxSscanf(str.wx_str() + 3, wxT("( %d , %d , %d )"), | |
115 | &red, &green, &blue) != 3 ) | |
116 | return false; | |
117 | } | |
118 | ||
119 | Set((unsigned char)wxClip(red, 0, 255), | |
120 | (unsigned char)wxClip(green, 0, 255), | |
121 | (unsigned char)wxClip(blue, 0, 255), | |
122 | (unsigned char)wxClip(alpha, 0, 255)); | |
40989e46 WS |
123 | } |
124 | else if ( str[0] == wxT('#') && wxStrlen(str) == 7 ) | |
125 | { | |
126 | // hexadecimal prefixed with # (HTML syntax) | |
127 | unsigned long tmp; | |
52de37c7 | 128 | if (wxSscanf(str.wx_str() + 1, wxT("%lx"), &tmp) != 1) |
40989e46 WS |
129 | return false; |
130 | ||
04407723 PC |
131 | Set((unsigned char)(tmp >> 16), |
132 | (unsigned char)(tmp >> 8), | |
133 | (unsigned char)tmp); | |
40989e46 WS |
134 | } |
135 | else if (wxTheColourDatabase) // a colour name ? | |
136 | { | |
137 | // we can't do | |
138 | // *this = wxTheColourDatabase->Find(str) | |
139 | // because this place can be called from constructor | |
140 | // and 'this' could not be available yet | |
141 | wxColour clr = wxTheColourDatabase->Find(str); | |
81853b98 WS |
142 | if (clr.Ok()) |
143 | Set((unsigned char)clr.Red(), | |
144 | (unsigned char)clr.Green(), | |
145 | (unsigned char)clr.Blue()); | |
40989e46 WS |
146 | } |
147 | ||
148 | if (Ok()) | |
149 | return true; | |
150 | ||
151 | wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str); | |
152 | return false; | |
153 | } | |
154 | ||
155 | wxString wxColourBase::GetAsString(long flags) const | |
156 | { | |
157 | wxString colName; | |
158 | ||
b534968d | 159 | const bool isOpaque = Alpha() == wxALPHA_OPAQUE; |
40989e46 | 160 | |
b534968d VZ |
161 | // we can't use the name format if the colour is not opaque as the alpha |
162 | // information would be lost | |
163 | if ( (flags & wxC2S_NAME) && isOpaque ) | |
40989e46 | 164 | { |
b534968d | 165 | colName = wxTheColourDatabase->FindName( |
5c33522f | 166 | static_cast<const wxColour &>(*this)).MakeLower(); |
40989e46 | 167 | } |
b534968d VZ |
168 | |
169 | if ( colName.empty() ) | |
40989e46 | 170 | { |
b534968d VZ |
171 | const int red = Red(), |
172 | blue = Blue(), | |
173 | green = Green(); | |
174 | ||
175 | if ( flags & wxC2S_CSS_SYNTAX ) | |
176 | { | |
177 | // no name for this colour; return it in CSS syntax | |
178 | if ( isOpaque ) | |
179 | { | |
180 | colName.Printf(wxT("rgb(%d, %d, %d)"), red, green, blue); | |
181 | } | |
182 | else // use rgba() form | |
183 | { | |
184 | // TODO: use locale-independent function | |
185 | colName.Printf(wxT("rgba(%d, %d, %d, %.3f)"), | |
186 | red, green, blue, Alpha() / 255.); | |
187 | } | |
188 | } | |
189 | else if ( flags & wxC2S_HTML_SYNTAX ) | |
190 | { | |
191 | wxASSERT_MSG( isOpaque, "alpha is lost in HTML syntax" ); | |
192 | ||
193 | // no name for this colour; return it in HTML syntax | |
194 | colName.Printf(wxT("#%02X%02X%02X"), red, green, blue); | |
195 | } | |
40989e46 WS |
196 | } |
197 | ||
b534968d | 198 | // this function should alway returns a non-empty string |
40989e46 WS |
199 | wxASSERT_MSG(!colName.empty(), |
200 | wxT("Invalid wxColour -> wxString conversion flags")); | |
201 | ||
202 | return colName; | |
203 | } | |
204 | ||
198c264d | 205 | // static |
ce00f59b | 206 | void wxColourBase::MakeMono(unsigned char* r, unsigned char* g, unsigned char* b, |
198c264d JS |
207 | bool on) |
208 | { | |
209 | *r = *g = *b = on ? 255 : 0; | |
210 | } | |
211 | ||
212 | // static | |
213 | void wxColourBase::MakeGrey(unsigned char* r, unsigned char* g, unsigned char* b | |
214 | /*, unsigned char brightness */ | |
215 | ) | |
216 | { | |
217 | *r = *g = *b = (wxByte)(((*b)*117UL + (*g)*601UL + (*r)*306UL) >> 10); | |
218 | } | |
219 | ||
220 | // static | |
221 | void wxColourBase::MakeGrey(unsigned char* r, unsigned char* g, unsigned char* b, | |
222 | double weight_r, double weight_g, double weight_b) | |
223 | { | |
224 | double luma = (*r) * weight_r + (*g) * weight_g + (*b) * weight_b; | |
225 | *r = *g = *b = (wxByte)wxRound(luma); | |
226 | } | |
227 | ||
228 | // static | |
ce00f59b | 229 | void wxColourBase::MakeDisabled(unsigned char* r, unsigned char* g, unsigned char* b, |
198c264d JS |
230 | unsigned char brightness) |
231 | { | |
232 | //MakeGrey(r, g, b, brightness); // grey no-blend version | |
233 | *r = AlphaBlend(*r, brightness, 0.4); | |
234 | *g = AlphaBlend(*g, brightness, 0.4); | |
235 | *b = AlphaBlend(*b, brightness, 0.4); | |
236 | } | |
237 | ||
238 | // AlphaBlend is used by ChangeLightness and MakeDisabled | |
239 | ||
240 | // static | |
ce00f59b | 241 | unsigned char wxColourBase::AlphaBlend(unsigned char fg, unsigned char bg, |
198c264d JS |
242 | double alpha) |
243 | { | |
244 | double result = bg + (alpha * (fg - bg)); | |
245 | result = wxMax(result, 0.0); | |
246 | result = wxMin(result, 255.0); | |
247 | return (unsigned char)result; | |
248 | } | |
249 | ||
250 | // ChangeLightness() is a utility function that simply darkens | |
251 | // or lightens a color, based on the specified percentage | |
252 | // ialpha of 0 would be completely black, 100 completely white | |
253 | // an ialpha of 100 returns the same colour | |
254 | ||
255 | // static | |
256 | void wxColourBase::ChangeLightness(unsigned char* r, unsigned char* g, unsigned char* b, | |
257 | int ialpha) | |
258 | { | |
259 | if (ialpha == 100) return; | |
260 | ||
261 | // ialpha is 0..200 where 0 is completely black | |
262 | // and 200 is completely white and 100 is the same | |
263 | // convert that to normal alpha 0.0 - 1.0 | |
264 | ialpha = wxMax(ialpha, 0); | |
265 | ialpha = wxMin(ialpha, 200); | |
266 | double alpha = ((double)(ialpha - 100.0))/100.0; | |
267 | ||
268 | unsigned char bg; | |
269 | if (ialpha > 100) | |
270 | { | |
271 | // blend with white | |
272 | bg = 255; | |
273 | alpha = 1.0 - alpha; // 0 = transparent fg; 1 = opaque fg | |
274 | } | |
275 | else | |
276 | { | |
277 | // blend with black | |
278 | bg = 0; | |
279 | alpha = 1.0 + alpha; // 0 = transparent fg; 1 = opaque fg | |
280 | } | |
281 | ||
282 | *r = AlphaBlend(*r, bg, alpha); | |
283 | *g = AlphaBlend(*g, bg, alpha); | |
284 | *b = AlphaBlend(*b, bg, alpha); | |
285 | } | |
286 | ||
287 | wxColour wxColourBase::ChangeLightness(int ialpha) const | |
288 | { | |
289 | wxByte r = Red(); | |
290 | wxByte g = Green(); | |
291 | wxByte b = Blue(); | |
292 | ChangeLightness(&r, &g, &b, ialpha); | |
293 | return wxColour(r,g,b); | |
294 | } | |
295 | ||
7d01c54d WS |
296 | #if WXWIN_COMPATIBILITY_2_6 |
297 | ||
298 | // static | |
40989e46 WS |
299 | wxColour wxColourBase::CreateByName(const wxString& name) |
300 | { | |
301 | return wxColour(name); | |
302 | } | |
7d01c54d WS |
303 | |
304 | void wxColourBase::InitFromName(const wxString& col) | |
305 | { | |
306 | Set(col); | |
307 | } | |
308 | ||
309 | #endif // WXWIN_COMPATIBILITY_2_6 | |
8b51786f VZ |
310 | |
311 | // wxColour <-> wxString utilities, used by wxConfig | |
312 | wxString wxToString(const wxColourBase& col) | |
313 | { | |
314 | return col.IsOk() ? col.GetAsString(wxC2S_CSS_SYNTAX) | |
315 | : wxString(); | |
316 | } | |
317 | ||
318 | bool wxFromString(const wxString& str, wxColourBase *col) | |
319 | { | |
9a83f860 | 320 | wxCHECK_MSG( col, false, wxT("NULL output parameter") ); |
8b51786f VZ |
321 | |
322 | if ( str.empty() ) | |
323 | { | |
324 | *col = wxNullColour; | |
325 | return true; | |
326 | } | |
327 | ||
328 | return col->Set(str); | |
329 | } | |
330 | ||
331 |