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