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