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