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