]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/colour.cpp
The colour is called Aquamarine, not Aquaramine. No,
[wxWidgets.git] / src / gtk1 / colour.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: colour.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "colour.h"
13#endif
14
15#include "wx/gdicmn.h"
16
17#include "gdk/gdk.h"
18#include "gdk/gdkprivate.h"
19
20//-----------------------------------------------------------------------------
21// wxColour
22//-----------------------------------------------------------------------------
23
24class 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
39wxColourRefData::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
49wxColourRefData::~wxColourRefData()
50{
51 FreeColour();
52}
53
54void 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
65IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
66
67wxColour::wxColour()
68{
69}
70
71wxColour::wxColour( unsigned char red, unsigned char green, unsigned 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
80void 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.mb_str(), &M_COLDATA->m_color ))
93 {
94 wxFAIL_MSG( _T("wxColour: couldn't find colour") );
95 wxPrintf( _T("Colourname %s.\n"), WXSTRINGCAST colourName );
96
97 delete m_refData;
98 m_refData = (wxObjectRefData *) NULL;
99 }
100 }
101}
102
103wxColour::wxColour( const wxColour& col )
104{
105 Ref( col );
106}
107
108wxColour::~wxColour()
109{
110}
111
112wxColour& wxColour::operator = ( const wxColour& col )
113{
114 if (*this == col) return (*this);
115 Ref( col );
116 return *this;
117}
118
119bool wxColour::operator == ( const wxColour& col ) const
120{
121 return m_refData == col.m_refData;
122}
123
124bool wxColour::operator != ( const wxColour& col) const
125{
126 return m_refData != col.m_refData;
127}
128
129void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
130{
131 UnRef();
132 m_refData = new wxColourRefData();
133 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
134 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
135 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
136 M_COLDATA->m_color.pixel = 0;
137}
138
139unsigned char wxColour::Red() const
140{
141 wxCHECK_MSG( Ok(), 0, _T("invalid colour") );
142
143 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
144}
145
146unsigned char wxColour::Green() const
147{
148 wxCHECK_MSG( Ok(), 0, _T("invalid colour") );
149
150 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
151}
152
153unsigned char wxColour::Blue() const
154{
155 wxCHECK_MSG( Ok(), 0, _T("invalid colour") );
156
157 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
158}
159
160bool wxColour::Ok() const
161{
162 return (m_refData != NULL);
163}
164
165void wxColour::CalcPixel( GdkColormap *cmap )
166{
167 if (!Ok()) return;
168
169 if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return;
170
171 M_COLDATA->FreeColour();
172
173 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
174 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
175 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
176 {
177 GdkColor *colors = cmap->colors;
178 int max = 3 * (65536);
179 int index = -1;
180
181 for (int i = 0; i < cmap->size; i++)
182 {
183 int rdiff = (M_COLDATA->m_color.red - colors[i].red);
184 int gdiff = (M_COLDATA->m_color.green - colors[i].green);
185 int bdiff = (M_COLDATA->m_color.blue - colors[i].blue);
186 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
187 if (sum < max) { index = i; max = sum; }
188 }
189
190 M_COLDATA->m_hasPixel = TRUE;
191 M_COLDATA->m_color.pixel = index;
192 }
193 else
194 {
195 M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color );
196 }
197
198 M_COLDATA->m_colormap = cmap;
199}
200
201int wxColour::GetPixel() const
202{
203 wxCHECK_MSG( Ok(), 0, _T("invalid colour") );
204
205 return M_COLDATA->m_color.pixel;
206}
207
208GdkColor *wxColour::GetColor() const
209{
210 wxCHECK_MSG( Ok(), (GdkColor *) NULL, _T("invalid colour") );
211
212 return &M_COLDATA->m_color;
213}
214
215