]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: colour.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "colour.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/gdicmn.h" | |
17 | ||
47d67540 | 18 | #ifdef wxUSE_GDK_IMLIB |
1f0299c1 | 19 | #include "../gdk_imlib/gdk_imlib.h" |
c801d85f KB |
20 | #endif |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // wxColour | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | class wxColourRefData: public wxObjectRefData | |
27 | { | |
28 | public: | |
29 | ||
68dda785 VZ |
30 | wxColourRefData(); |
31 | ~wxColourRefData(); | |
32 | void FreeColour(); | |
c801d85f KB |
33 | |
34 | GdkColor m_color; | |
35 | GdkColormap *m_colormap; | |
36 | bool m_hasPixel; | |
37 | ||
38 | friend wxColour; | |
39 | }; | |
40 | ||
68dda785 | 41 | wxColourRefData::wxColourRefData() |
c801d85f KB |
42 | { |
43 | m_color.red = 0; | |
44 | m_color.green = 0; | |
45 | m_color.blue = 0; | |
46 | m_color.pixel = 0; | |
c67daf87 | 47 | m_colormap = (GdkColormap *) NULL; |
c801d85f | 48 | m_hasPixel = FALSE; |
ff7b1510 | 49 | } |
c801d85f | 50 | |
68dda785 | 51 | wxColourRefData::~wxColourRefData() |
c801d85f KB |
52 | { |
53 | FreeColour(); | |
ff7b1510 | 54 | } |
c801d85f | 55 | |
68dda785 | 56 | void wxColourRefData::FreeColour() |
c801d85f KB |
57 | { |
58 | // if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 ); | |
ff7b1510 | 59 | } |
c801d85f KB |
60 | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | #define M_COLDATA ((wxColourRefData *)m_refData) | |
64 | ||
65 | #define SHIFT (8*(sizeof(short int)-sizeof(char))) | |
66 | ||
67 | IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) | |
68 | ||
68dda785 | 69 | wxColour::wxColour() |
c801d85f | 70 | { |
ff7b1510 | 71 | } |
c801d85f KB |
72 | |
73 | wxColour::wxColour( char red, char green, char blue ) | |
74 | { | |
75 | m_refData = new wxColourRefData(); | |
76 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; | |
77 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
78 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
79 | M_COLDATA->m_color.pixel = 0; | |
ff7b1510 | 80 | } |
c801d85f | 81 | |
68dda785 | 82 | void wxColour::InitFromName( const wxString &colourName ) |
c801d85f | 83 | { |
c67daf87 | 84 | wxNode *node = (wxNode *) NULL; |
c801d85f KB |
85 | if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) ) |
86 | { | |
87 | wxColour *col = (wxColour*)node->Data(); | |
88 | UnRef(); | |
89 | if (col) Ref( *col ); | |
90 | } | |
91 | else | |
92 | { | |
93 | m_refData = new wxColourRefData(); | |
94 | if (!gdk_color_parse( colourName, &M_COLDATA->m_color )) | |
95 | { | |
b6af8d80 | 96 | wxFAIL_MSG( "wxColour: couldn't find colour" ); |
c801d85f | 97 | delete m_refData; |
c67daf87 | 98 | m_refData = (wxObjectRefData *) NULL; |
ff7b1510 RR |
99 | } |
100 | } | |
101 | } | |
c801d85f KB |
102 | |
103 | wxColour::wxColour( const wxColour& col ) | |
104 | { | |
105 | Ref( col ); | |
ff7b1510 | 106 | } |
c801d85f KB |
107 | |
108 | wxColour::wxColour( const wxColour* col ) | |
109 | { | |
110 | if (col) Ref( *col ); | |
ff7b1510 | 111 | } |
c801d85f | 112 | |
68dda785 | 113 | wxColour::~wxColour() |
c801d85f | 114 | { |
ff7b1510 | 115 | } |
c801d85f KB |
116 | |
117 | wxColour& wxColour::operator = ( const wxColour& col ) | |
118 | { | |
119 | if (*this == col) return (*this); | |
120 | Ref( col ); | |
121 | return *this; | |
ff7b1510 | 122 | } |
c801d85f | 123 | |
c801d85f KB |
124 | bool wxColour::operator == ( const wxColour& col ) |
125 | { | |
126 | return m_refData == col.m_refData; | |
ff7b1510 | 127 | } |
c801d85f KB |
128 | |
129 | bool wxColour::operator != ( const wxColour& col) | |
130 | { | |
131 | return m_refData != col.m_refData; | |
ff7b1510 | 132 | } |
c801d85f KB |
133 | |
134 | void wxColour::Set( const unsigned char red, const unsigned char green, const unsigned char blue ) | |
135 | { | |
136 | UnRef(); | |
137 | m_refData = new wxColourRefData(); | |
138 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; | |
139 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
140 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
141 | M_COLDATA->m_color.pixel = 0; | |
ff7b1510 | 142 | } |
c801d85f | 143 | |
68dda785 | 144 | unsigned char wxColour::Red() const |
c801d85f | 145 | { |
e55ad60e RR |
146 | if (!Ok()) |
147 | { | |
148 | wxFAIL_MSG( "invalid colour" ); | |
149 | return 0; | |
150 | } | |
151 | ||
c801d85f | 152 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
ff7b1510 | 153 | } |
c801d85f | 154 | |
68dda785 | 155 | unsigned char wxColour::Green() const |
c801d85f | 156 | { |
e55ad60e RR |
157 | if (!Ok()) |
158 | { | |
159 | wxFAIL_MSG( "invalid colour" ); | |
160 | return 0; | |
161 | } | |
162 | ||
c801d85f | 163 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
ff7b1510 | 164 | } |
c801d85f | 165 | |
68dda785 | 166 | unsigned char wxColour::Blue() const |
c801d85f | 167 | { |
e55ad60e RR |
168 | if (!Ok()) |
169 | { | |
170 | wxFAIL_MSG( "invalid colour" ); | |
171 | return 0; | |
172 | } | |
173 | ||
c801d85f | 174 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
ff7b1510 | 175 | } |
c801d85f | 176 | |
68dda785 | 177 | bool wxColour::Ok() const |
c801d85f | 178 | { |
e55ad60e | 179 | return (m_refData != NULL); |
ff7b1510 | 180 | } |
c801d85f KB |
181 | |
182 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
183 | { | |
184 | if (!Ok()) return; | |
185 | ||
186 | if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return; | |
187 | M_COLDATA->FreeColour(); | |
188 | ||
47d67540 | 189 | #ifdef wxUSE_GDK_IMLIB |
c801d85f KB |
190 | |
191 | int r = M_COLDATA->m_color.red >> SHIFT; | |
192 | int g = M_COLDATA->m_color.green >> SHIFT; | |
193 | int b = M_COLDATA->m_color.blue >> SHIFT; | |
194 | M_COLDATA->m_hasPixel = TRUE; | |
195 | M_COLDATA->m_color.pixel = gdk_imlib_best_color_match( &r, &g, &b ); | |
196 | ||
197 | #else | |
198 | ||
199 | M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color ); | |
200 | ||
201 | #endif | |
202 | ||
203 | M_COLDATA->m_colormap = cmap; | |
ff7b1510 | 204 | } |
c801d85f | 205 | |
68dda785 | 206 | int wxColour::GetPixel() const |
c801d85f KB |
207 | { |
208 | if (!Ok()) return 0; | |
209 | ||
210 | return M_COLDATA->m_color.pixel; | |
ff7b1510 | 211 | } |
c801d85f | 212 | |
68dda785 | 213 | GdkColor *wxColour::GetColor() const |
c801d85f | 214 | { |
c67daf87 | 215 | if (!Ok()) return (GdkColor *) NULL; |
c801d85f KB |
216 | |
217 | return &M_COLDATA->m_color; | |
ff7b1510 | 218 | } |
c801d85f KB |
219 | |
220 |