]>
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 | ||
01111366 | 18 | #include "gdk/gdkprivate.h" |
c801d85f KB |
19 | |
20 | //----------------------------------------------------------------------------- | |
21 | // wxColour | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | class wxColourRefData: public wxObjectRefData | |
25 | { | |
26 | public: | |
27 | ||
68dda785 VZ |
28 | wxColourRefData(); |
29 | ~wxColourRefData(); | |
30 | void FreeColour(); | |
c801d85f KB |
31 | |
32 | GdkColor m_color; | |
33 | GdkColormap *m_colormap; | |
34 | bool m_hasPixel; | |
35 | ||
36 | friend wxColour; | |
37 | }; | |
38 | ||
68dda785 | 39 | wxColourRefData::wxColourRefData() |
c801d85f KB |
40 | { |
41 | m_color.red = 0; | |
42 | m_color.green = 0; | |
43 | m_color.blue = 0; | |
44 | m_color.pixel = 0; | |
c67daf87 | 45 | m_colormap = (GdkColormap *) NULL; |
c801d85f | 46 | m_hasPixel = FALSE; |
ff7b1510 | 47 | } |
c801d85f | 48 | |
68dda785 | 49 | wxColourRefData::~wxColourRefData() |
c801d85f KB |
50 | { |
51 | FreeColour(); | |
ff7b1510 | 52 | } |
c801d85f | 53 | |
68dda785 | 54 | void wxColourRefData::FreeColour() |
c801d85f KB |
55 | { |
56 | // if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 ); | |
ff7b1510 | 57 | } |
c801d85f KB |
58 | |
59 | //----------------------------------------------------------------------------- | |
60 | ||
61 | #define M_COLDATA ((wxColourRefData *)m_refData) | |
62 | ||
63 | #define SHIFT (8*(sizeof(short int)-sizeof(char))) | |
64 | ||
65 | IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) | |
66 | ||
68dda785 | 67 | wxColour::wxColour() |
c801d85f | 68 | { |
ff7b1510 | 69 | } |
c801d85f KB |
70 | |
71 | wxColour::wxColour( char red, char green, char blue ) | |
72 | { | |
73 | m_refData = new wxColourRefData(); | |
74 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; | |
75 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
76 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
77 | M_COLDATA->m_color.pixel = 0; | |
ff7b1510 | 78 | } |
c801d85f | 79 | |
68dda785 | 80 | void wxColour::InitFromName( const wxString &colourName ) |
c801d85f | 81 | { |
c67daf87 | 82 | wxNode *node = (wxNode *) NULL; |
c801d85f KB |
83 | if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) ) |
84 | { | |
85 | wxColour *col = (wxColour*)node->Data(); | |
86 | UnRef(); | |
87 | if (col) Ref( *col ); | |
88 | } | |
89 | else | |
90 | { | |
91 | m_refData = new wxColourRefData(); | |
92 | if (!gdk_color_parse( colourName, &M_COLDATA->m_color )) | |
93 | { | |
b6af8d80 | 94 | wxFAIL_MSG( "wxColour: couldn't find colour" ); |
c801d85f | 95 | delete m_refData; |
c67daf87 | 96 | m_refData = (wxObjectRefData *) NULL; |
ff7b1510 RR |
97 | } |
98 | } | |
99 | } | |
c801d85f KB |
100 | |
101 | wxColour::wxColour( const wxColour& col ) | |
102 | { | |
103 | Ref( col ); | |
ff7b1510 | 104 | } |
c801d85f KB |
105 | |
106 | wxColour::wxColour( const wxColour* col ) | |
107 | { | |
108 | if (col) Ref( *col ); | |
ff7b1510 | 109 | } |
c801d85f | 110 | |
68dda785 | 111 | wxColour::~wxColour() |
c801d85f | 112 | { |
ff7b1510 | 113 | } |
c801d85f KB |
114 | |
115 | wxColour& wxColour::operator = ( const wxColour& col ) | |
116 | { | |
117 | if (*this == col) return (*this); | |
118 | Ref( col ); | |
119 | return *this; | |
ff7b1510 | 120 | } |
c801d85f | 121 | |
c801d85f KB |
122 | bool wxColour::operator == ( const wxColour& col ) |
123 | { | |
124 | return m_refData == col.m_refData; | |
ff7b1510 | 125 | } |
c801d85f KB |
126 | |
127 | bool wxColour::operator != ( const wxColour& col) | |
128 | { | |
129 | return m_refData != col.m_refData; | |
ff7b1510 | 130 | } |
c801d85f KB |
131 | |
132 | void wxColour::Set( const unsigned char red, const unsigned char green, const unsigned char blue ) | |
133 | { | |
134 | UnRef(); | |
135 | m_refData = new wxColourRefData(); | |
136 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; | |
137 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
138 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
139 | M_COLDATA->m_color.pixel = 0; | |
ff7b1510 | 140 | } |
c801d85f | 141 | |
68dda785 | 142 | unsigned char wxColour::Red() const |
c801d85f | 143 | { |
e55ad60e RR |
144 | if (!Ok()) |
145 | { | |
146 | wxFAIL_MSG( "invalid colour" ); | |
147 | return 0; | |
148 | } | |
149 | ||
c801d85f | 150 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
ff7b1510 | 151 | } |
c801d85f | 152 | |
68dda785 | 153 | unsigned char wxColour::Green() const |
c801d85f | 154 | { |
e55ad60e RR |
155 | if (!Ok()) |
156 | { | |
157 | wxFAIL_MSG( "invalid colour" ); | |
158 | return 0; | |
159 | } | |
160 | ||
c801d85f | 161 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
ff7b1510 | 162 | } |
c801d85f | 163 | |
68dda785 | 164 | unsigned char wxColour::Blue() const |
c801d85f | 165 | { |
e55ad60e RR |
166 | if (!Ok()) |
167 | { | |
168 | wxFAIL_MSG( "invalid colour" ); | |
169 | return 0; | |
170 | } | |
171 | ||
c801d85f | 172 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
ff7b1510 | 173 | } |
c801d85f | 174 | |
68dda785 | 175 | bool wxColour::Ok() const |
c801d85f | 176 | { |
e55ad60e | 177 | return (m_refData != NULL); |
ff7b1510 | 178 | } |
c801d85f KB |
179 | |
180 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
181 | { | |
182 | if (!Ok()) return; | |
183 | ||
184 | if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return; | |
185 | M_COLDATA->FreeColour(); | |
186 | ||
01111366 RR |
187 | GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap; |
188 | if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
189 | (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
190 | { | |
191 | GdkColor *colors = cmap->colors; | |
192 | int max = 3 * (65536); | |
193 | int index = -1; | |
c801d85f | 194 | |
01111366 RR |
195 | for (int i = 0; i < cmap->size; i++) |
196 | { | |
197 | int rdiff = (M_COLDATA->m_color.red - colors[i].red); | |
198 | int gdiff = (M_COLDATA->m_color.green - colors[i].green); | |
199 | int bdiff = (M_COLDATA->m_color.blue - colors[i].blue); | |
200 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
201 | if (sum < max) { index = i; max = sum; } | |
202 | } | |
203 | ||
204 | M_COLDATA->m_hasPixel = TRUE; | |
205 | M_COLDATA->m_color.pixel = index; | |
206 | } | |
207 | else | |
208 | { | |
209 | M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color ); | |
210 | } | |
c801d85f KB |
211 | |
212 | M_COLDATA->m_colormap = cmap; | |
ff7b1510 | 213 | } |
c801d85f | 214 | |
68dda785 | 215 | int wxColour::GetPixel() const |
c801d85f KB |
216 | { |
217 | if (!Ok()) return 0; | |
218 | ||
219 | return M_COLDATA->m_color.pixel; | |
ff7b1510 | 220 | } |
c801d85f | 221 | |
68dda785 | 222 | GdkColor *wxColour::GetColor() const |
c801d85f | 223 | { |
c67daf87 | 224 | if (!Ok()) return (GdkColor *) NULL; |
c801d85f KB |
225 | |
226 | return &M_COLDATA->m_color; | |
ff7b1510 | 227 | } |
c801d85f KB |
228 | |
229 |