]>
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 RR |
17 | #include <gdk/gdk.h> |
18 | #include <gdk/gdkprivate.h> | |
c801d85f KB |
19 | |
20 | //----------------------------------------------------------------------------- | |
21 | // wxColour | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | class wxColourRefData: public wxObjectRefData | |
25 | { | |
26 | public: | |
8bbe427f | 27 | |
68dda785 VZ |
28 | wxColourRefData(); |
29 | ~wxColourRefData(); | |
30 | void FreeColour(); | |
8bbe427f | 31 | |
c801d85f KB |
32 | GdkColor m_color; |
33 | GdkColormap *m_colormap; | |
34 | bool m_hasPixel; | |
8bbe427f | 35 | |
c801d85f KB |
36 | friend wxColour; |
37 | }; | |
38 | ||
68dda785 | 39 | wxColourRefData::wxColourRefData() |
c801d85f | 40 | { |
1ecc4d80 RR |
41 | m_color.red = 0; |
42 | m_color.green = 0; | |
43 | m_color.blue = 0; | |
44 | m_color.pixel = 0; | |
45 | m_colormap = (GdkColormap *) NULL; | |
46 | m_hasPixel = FALSE; | |
ff7b1510 | 47 | } |
c801d85f | 48 | |
68dda785 | 49 | wxColourRefData::~wxColourRefData() |
c801d85f | 50 | { |
1ecc4d80 | 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 | 70 | |
4b5f3fe6 | 71 | wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 72 | { |
1ecc4d80 RR |
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 | } |
8bbe427f | 79 | |
68dda785 | 80 | void wxColour::InitFromName( const wxString &colourName ) |
c801d85f | 81 | { |
1ecc4d80 RR |
82 | wxNode *node = (wxNode *) NULL; |
83 | if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) ) | |
c801d85f | 84 | { |
1ecc4d80 RR |
85 | wxColour *col = (wxColour*)node->Data(); |
86 | UnRef(); | |
87 | if (col) Ref( *col ); | |
88 | } | |
89 | else | |
90 | { | |
91 | m_refData = new wxColourRefData(); | |
93c5dd39 | 92 | if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color )) |
1ecc4d80 | 93 | { |
223d09f6 KB |
94 | wxFAIL_MSG( wxT("wxColour: couldn't find colour") ); |
95 | wxPrintf( wxT("Colourname %s.\n"), WXSTRINGCAST colourName ); | |
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 | { |
1ecc4d80 | 137 | return m_refData != col.m_refData; |
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 | |
f6fcbb63 RR |
184 | GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap; |
185 | if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
186 | (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
01111366 | 187 | { |
f6fcbb63 RR |
188 | GdkColor *colors = cmap->colors; |
189 | int max = 3 * (65536); | |
190 | int index = -1; | |
8bbe427f | 191 | |
f6fcbb63 RR |
192 | for (int i = 0; i < cmap->size; i++) |
193 | { | |
194 | int rdiff = (M_COLDATA->m_color.red - colors[i].red); | |
195 | int gdiff = (M_COLDATA->m_color.green - colors[i].green); | |
196 | int bdiff = (M_COLDATA->m_color.blue - colors[i].blue); | |
197 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
198 | if (sum < max) { index = i; max = sum; } | |
199 | } | |
200 | ||
201 | M_COLDATA->m_hasPixel = TRUE; | |
202 | M_COLDATA->m_color.pixel = index; | |
203 | } | |
1ecc4d80 RR |
204 | else |
205 | { | |
206 | M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color ); | |
207 | } | |
8bbe427f | 208 | |
1ecc4d80 | 209 | M_COLDATA->m_colormap = cmap; |
ff7b1510 | 210 | } |
c801d85f | 211 | |
68dda785 | 212 | int wxColour::GetPixel() const |
c801d85f | 213 | { |
223d09f6 | 214 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 215 | |
1ecc4d80 | 216 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 217 | } |
c801d85f | 218 | |
68dda785 | 219 | GdkColor *wxColour::GetColor() const |
c801d85f | 220 | { |
223d09f6 | 221 | wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") ); |
8bbe427f | 222 | |
1ecc4d80 | 223 | return &M_COLDATA->m_color; |
ff7b1510 | 224 | } |
c801d85f KB |
225 | |
226 |