]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/palette.cpp
a fix for the last fix
[wxWidgets.git] / src / gtk / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: palette.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 "palette.h"
14 #endif
15
16 #include "wx/palette.h"
17
18 #include <gdk/gdk.h>
19
20 //-----------------------------------------------------------------------------
21 // wxPalette
22 //-----------------------------------------------------------------------------
23
24 struct wxPaletteEntry
25 {
26 unsigned char red, green, blue;
27 };
28
29 class wxPaletteRefData: public wxObjectRefData
30 {
31 public:
32
33 wxPaletteRefData(void);
34 ~wxPaletteRefData(void);
35
36 int m_count;
37 wxPaletteEntry *m_entries;
38 #if 0
39 GdkColormap *m_colormap;
40 #endif
41 };
42
43 wxPaletteRefData::wxPaletteRefData()
44 {
45 m_count = 0;
46 m_entries = NULL;
47 #if 0
48 m_colormap = (GdkColormap *) NULL;
49 #endif
50 }
51
52 wxPaletteRefData::~wxPaletteRefData()
53 {
54 delete[] m_entries;
55 #if 0
56 if (m_colormap) gdk_colormap_unref( m_colormap );
57 #endif
58 }
59
60 //-----------------------------------------------------------------------------
61
62 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
63
64 IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
65
66 wxPalette::wxPalette()
67 {
68 m_refData = NULL;
69 }
70
71 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
72 {
73 Create(n, red, green, blue);
74 }
75
76 wxPalette::wxPalette(const wxPalette& palette)
77 {
78 Ref(palette);
79 }
80
81 wxPalette::~wxPalette()
82 {
83 }
84
85 wxPalette& wxPalette::operator = (const wxPalette& palette)
86 {
87 if (*this == palette) return (*this);
88 Ref(palette);
89 return *this;
90 }
91
92 bool wxPalette::operator == (const wxPalette& palette)
93 {
94 return m_refData == palette.m_refData;
95 }
96
97 bool wxPalette::operator != (const wxPalette& palette)
98 {
99 return m_refData != palette.m_refData;
100 }
101
102 bool wxPalette::Ok(void) const
103 {
104 return (m_refData != NULL);
105 }
106
107 bool wxPalette::Create(int n,
108 const unsigned char *red,
109 const unsigned char *green,
110 const unsigned char *blue)
111 {
112 UnRef();
113 m_refData = new wxPaletteRefData();
114
115 M_PALETTEDATA->m_count = n;
116 M_PALETTEDATA->m_entries = new wxPaletteEntry[n];
117
118 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
119 for (int i = 0; i < n; i++, e++)
120 {
121 e->red = red[i];
122 e->green = green[i];
123 e->blue = blue[i];
124 }
125
126 return TRUE;
127 }
128
129 int wxPalette::GetPixel( const unsigned char red,
130 const unsigned char green,
131 const unsigned char blue ) const
132 {
133 if (!m_refData) return FALSE;
134
135 int closest = 0;
136 double d,distance = 1000.0; // max. dist is 256
137
138 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
139 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
140 {
141 if ((d = 0.299 * abs(red - e->red) +
142 0.587 * abs(green - e->green) +
143 0.114 * abs(blue - e->blue)) < distance) {
144 distance = d;
145 closest = i;
146 }
147 }
148 return closest;
149 }
150
151 bool wxPalette::GetRGB(int pixel,
152 unsigned char *red,
153 unsigned char *green,
154 unsigned char *blue) const
155 {
156 if (!m_refData) return FALSE;
157 if (pixel >= M_PALETTEDATA->m_count) return FALSE;
158
159 wxPaletteEntry& p = M_PALETTEDATA->m_entries[pixel];
160 if (red) *red = p.red;
161 if (green) *green = p.green;
162 if (blue) *blue = p.blue;
163 return TRUE;
164 }
165
166