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