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