]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/colour.cpp
no message
[wxWidgets.git] / src / gtk1 / colour.cpp
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
18 #include "gdk/gdkprivate.h"
19
20 //-----------------------------------------------------------------------------
21 // wxColour
22 //-----------------------------------------------------------------------------
23
24 class wxColourRefData: public wxObjectRefData
25 {
26 public:
27
28 wxColourRefData();
29 ~wxColourRefData();
30 void FreeColour();
31
32 GdkColor m_color;
33 GdkColormap *m_colormap;
34 bool m_hasPixel;
35
36 friend wxColour;
37 };
38
39 wxColourRefData::wxColourRefData()
40 {
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;
47 }
48
49 wxColourRefData::~wxColourRefData()
50 {
51 FreeColour();
52 }
53
54 void wxColourRefData::FreeColour()
55 {
56 // if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 );
57 }
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
67 wxColour::wxColour()
68 {
69 }
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;
78 }
79
80 void wxColour::InitFromName( const wxString &colourName )
81 {
82 wxNode *node = (wxNode *) NULL;
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 {
94 wxFAIL_MSG( "wxColour: couldn't find colour" );
95 delete m_refData;
96 m_refData = (wxObjectRefData *) NULL;
97 }
98 }
99 }
100
101 wxColour::wxColour( const wxColour& col )
102 {
103 Ref( col );
104 }
105
106 wxColour::wxColour( const wxColour* col )
107 {
108 if (col) Ref( *col );
109 }
110
111 wxColour::~wxColour()
112 {
113 }
114
115 wxColour& wxColour::operator = ( const wxColour& col )
116 {
117 if (*this == col) return (*this);
118 Ref( col );
119 return *this;
120 }
121
122 bool wxColour::operator == ( const wxColour& col )
123 {
124 return m_refData == col.m_refData;
125 }
126
127 bool wxColour::operator != ( const wxColour& col)
128 {
129 return m_refData != col.m_refData;
130 }
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;
140 }
141
142 unsigned char wxColour::Red() const
143 {
144 if (!Ok())
145 {
146 wxFAIL_MSG( "invalid colour" );
147 return 0;
148 }
149
150 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
151 }
152
153 unsigned char wxColour::Green() const
154 {
155 if (!Ok())
156 {
157 wxFAIL_MSG( "invalid colour" );
158 return 0;
159 }
160
161 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
162 }
163
164 unsigned char wxColour::Blue() const
165 {
166 if (!Ok())
167 {
168 wxFAIL_MSG( "invalid colour" );
169 return 0;
170 }
171
172 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
173 }
174
175 bool wxColour::Ok() const
176 {
177 return (m_refData != NULL);
178 }
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
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;
194
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 }
211
212 M_COLDATA->m_colormap = cmap;
213 }
214
215 int wxColour::GetPixel() const
216 {
217 if (!Ok()) return 0;
218
219 return M_COLDATA->m_color.pixel;
220 }
221
222 GdkColor *wxColour::GetColor() const
223 {
224 if (!Ok()) return (GdkColor *) NULL;
225
226 return &M_COLDATA->m_color;
227 }
228
229