]> git.saurik.com Git - wxWidgets.git/blame - src/common/colourcmn.cpp
Committing modified version of jwiesemann's patch (see #11223):
[wxWidgets.git] / src / common / colourcmn.cpp
CommitLineData
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
30IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxColour,WXDLLEXPORT)
31#endif
40989e46
WS
32
33// ============================================================================
34// wxString <-> wxColour conversions
35// ============================================================================
36
e86d4e59 37bool wxColourBase::FromString(const wxString& str)
40989e46 38{
e86d4e59 39 if ( str.empty() )
40989e46
WS
40 return false; // invalid or empty string
41
b534968d 42 if ( wxStrnicmp(str, wxT("RGB"), 3) == 0 )
40989e46 43 {
7270921d 44 // CSS-like RGB specification
b534968d 45 // according to http://www.w3.org/TR/css3-color/#colorunits
7270921d 46 // values outside 0-255 range are allowed but should be clipped
b534968d
VZ
47 int red, green, blue,
48 alpha = wxALPHA_OPAQUE;
49 if ( str.length() > 3 && (str[3] == wxT('a') || str[3] == wxT('A')) )
50 {
51 float a;
52 // TODO: use locale-independent function
53 if ( wxSscanf(str.wx_str() + 4, wxT("( %d , %d , %d , %f )"),
54 &red, &green, &blue, &a) != 4 )
55 return false;
56
57 alpha = wxRound(a * 255);
58 }
59 else // no 'a' following "rgb"
60 {
61 if ( wxSscanf(str.wx_str() + 3, wxT("( %d , %d , %d )"),
62 &red, &green, &blue) != 3 )
63 return false;
64 }
65
66 Set((unsigned char)wxClip(red, 0, 255),
67 (unsigned char)wxClip(green, 0, 255),
68 (unsigned char)wxClip(blue, 0, 255),
69 (unsigned char)wxClip(alpha, 0, 255));
40989e46
WS
70 }
71 else if ( str[0] == wxT('#') && wxStrlen(str) == 7 )
72 {
73 // hexadecimal prefixed with # (HTML syntax)
74 unsigned long tmp;
52de37c7 75 if (wxSscanf(str.wx_str() + 1, wxT("%lx"), &tmp) != 1)
40989e46
WS
76 return false;
77
04407723
PC
78 Set((unsigned char)(tmp >> 16),
79 (unsigned char)(tmp >> 8),
80 (unsigned char)tmp);
40989e46
WS
81 }
82 else if (wxTheColourDatabase) // a colour name ?
83 {
84 // we can't do
85 // *this = wxTheColourDatabase->Find(str)
86 // because this place can be called from constructor
87 // and 'this' could not be available yet
88 wxColour clr = wxTheColourDatabase->Find(str);
81853b98
WS
89 if (clr.Ok())
90 Set((unsigned char)clr.Red(),
91 (unsigned char)clr.Green(),
92 (unsigned char)clr.Blue());
40989e46
WS
93 }
94
95 if (Ok())
96 return true;
97
98 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str);
99 return false;
100}
101
102wxString wxColourBase::GetAsString(long flags) const
103{
104 wxString colName;
105
b534968d 106 const bool isOpaque = Alpha() == wxALPHA_OPAQUE;
40989e46 107
b534968d
VZ
108 // we can't use the name format if the colour is not opaque as the alpha
109 // information would be lost
110 if ( (flags & wxC2S_NAME) && isOpaque )
40989e46 111 {
b534968d 112 colName = wxTheColourDatabase->FindName(
5c33522f 113 static_cast<const wxColour &>(*this)).MakeLower();
40989e46 114 }
b534968d
VZ
115
116 if ( colName.empty() )
40989e46 117 {
b534968d
VZ
118 const int red = Red(),
119 blue = Blue(),
120 green = Green();
121
122 if ( flags & wxC2S_CSS_SYNTAX )
123 {
124 // no name for this colour; return it in CSS syntax
125 if ( isOpaque )
126 {
127 colName.Printf(wxT("rgb(%d, %d, %d)"), red, green, blue);
128 }
129 else // use rgba() form
130 {
131 // TODO: use locale-independent function
132 colName.Printf(wxT("rgba(%d, %d, %d, %.3f)"),
133 red, green, blue, Alpha() / 255.);
134 }
135 }
136 else if ( flags & wxC2S_HTML_SYNTAX )
137 {
138 wxASSERT_MSG( isOpaque, "alpha is lost in HTML syntax" );
139
140 // no name for this colour; return it in HTML syntax
141 colName.Printf(wxT("#%02X%02X%02X"), red, green, blue);
142 }
40989e46
WS
143 }
144
b534968d 145 // this function should alway returns a non-empty string
40989e46
WS
146 wxASSERT_MSG(!colName.empty(),
147 wxT("Invalid wxColour -> wxString conversion flags"));
148
149 return colName;
150}
151
198c264d
JS
152// static
153void wxColourBase::MakeMono(unsigned char* r, unsigned char* g, unsigned char* b,
154 bool on)
155{
156 *r = *g = *b = on ? 255 : 0;
157}
158
159// static
160void wxColourBase::MakeGrey(unsigned char* r, unsigned char* g, unsigned char* b
161 /*, unsigned char brightness */
162 )
163{
164 *r = *g = *b = (wxByte)(((*b)*117UL + (*g)*601UL + (*r)*306UL) >> 10);
165}
166
167// static
168void wxColourBase::MakeGrey(unsigned char* r, unsigned char* g, unsigned char* b,
169 double weight_r, double weight_g, double weight_b)
170{
171 double luma = (*r) * weight_r + (*g) * weight_g + (*b) * weight_b;
172 *r = *g = *b = (wxByte)wxRound(luma);
173}
174
175// static
176void wxColourBase::MakeDisabled(unsigned char* r, unsigned char* g, unsigned char* b,
177 unsigned char brightness)
178{
179 //MakeGrey(r, g, b, brightness); // grey no-blend version
180 *r = AlphaBlend(*r, brightness, 0.4);
181 *g = AlphaBlend(*g, brightness, 0.4);
182 *b = AlphaBlend(*b, brightness, 0.4);
183}
184
185// AlphaBlend is used by ChangeLightness and MakeDisabled
186
187// static
188unsigned char wxColourBase::AlphaBlend(unsigned char fg, unsigned char bg,
189 double alpha)
190{
191 double result = bg + (alpha * (fg - bg));
192 result = wxMax(result, 0.0);
193 result = wxMin(result, 255.0);
194 return (unsigned char)result;
195}
196
197// ChangeLightness() is a utility function that simply darkens
198// or lightens a color, based on the specified percentage
199// ialpha of 0 would be completely black, 100 completely white
200// an ialpha of 100 returns the same colour
201
202// static
203void wxColourBase::ChangeLightness(unsigned char* r, unsigned char* g, unsigned char* b,
204 int ialpha)
205{
206 if (ialpha == 100) return;
207
208 // ialpha is 0..200 where 0 is completely black
209 // and 200 is completely white and 100 is the same
210 // convert that to normal alpha 0.0 - 1.0
211 ialpha = wxMax(ialpha, 0);
212 ialpha = wxMin(ialpha, 200);
213 double alpha = ((double)(ialpha - 100.0))/100.0;
214
215 unsigned char bg;
216 if (ialpha > 100)
217 {
218 // blend with white
219 bg = 255;
220 alpha = 1.0 - alpha; // 0 = transparent fg; 1 = opaque fg
221 }
222 else
223 {
224 // blend with black
225 bg = 0;
226 alpha = 1.0 + alpha; // 0 = transparent fg; 1 = opaque fg
227 }
228
229 *r = AlphaBlend(*r, bg, alpha);
230 *g = AlphaBlend(*g, bg, alpha);
231 *b = AlphaBlend(*b, bg, alpha);
232}
233
234wxColour wxColourBase::ChangeLightness(int ialpha) const
235{
236 wxByte r = Red();
237 wxByte g = Green();
238 wxByte b = Blue();
239 ChangeLightness(&r, &g, &b, ialpha);
240 return wxColour(r,g,b);
241}
242
7d01c54d
WS
243#if WXWIN_COMPATIBILITY_2_6
244
245// static
40989e46
WS
246wxColour wxColourBase::CreateByName(const wxString& name)
247{
248 return wxColour(name);
249}
7d01c54d
WS
250
251void wxColourBase::InitFromName(const wxString& col)
252{
253 Set(col);
254}
255
256#endif // WXWIN_COMPATIBILITY_2_6
8b51786f
VZ
257
258// wxColour <-> wxString utilities, used by wxConfig
259wxString wxToString(const wxColourBase& col)
260{
261 return col.IsOk() ? col.GetAsString(wxC2S_CSS_SYNTAX)
262 : wxString();
263}
264
265bool wxFromString(const wxString& str, wxColourBase *col)
266{
9a83f860 267 wxCHECK_MSG( col, false, wxT("NULL output parameter") );
8b51786f
VZ
268
269 if ( str.empty() )
270 {
271 *col = wxNullColour;
272 return true;
273 }
274
275 return col->Set(str);
276}
277
278