]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
d275c7eb | 2 | // Name: src/generic/paletteg.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
d275c7eb | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
45ef9db3 | 13 | #pragma implementation "paletteg.h" |
c801d85f KB |
14 | #endif |
15 | ||
d275c7eb VZ |
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 | |
c801d85f | 24 | |
d275c7eb | 25 | #include "wx/palette.h" |
83624f79 | 26 | |
c801d85f KB |
27 | //----------------------------------------------------------------------------- |
28 | // wxPalette | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
cb332bc6 VS |
31 | struct wxPaletteEntry |
32 | { | |
33 | unsigned char red, green, blue; | |
34 | }; | |
35 | ||
c801d85f KB |
36 | class wxPaletteRefData: public wxObjectRefData |
37 | { | |
38 | public: | |
8bbe427f | 39 | |
c801d85f KB |
40 | wxPaletteRefData(void); |
41 | ~wxPaletteRefData(void); | |
8bbe427f | 42 | |
cb332bc6 VS |
43 | int m_count; |
44 | wxPaletteEntry *m_entries; | |
c801d85f KB |
45 | }; |
46 | ||
8bbe427f | 47 | wxPaletteRefData::wxPaletteRefData() |
c801d85f | 48 | { |
cb332bc6 VS |
49 | m_count = 0; |
50 | m_entries = NULL; | |
8fc613f1 | 51 | } |
c801d85f | 52 | |
8bbe427f | 53 | wxPaletteRefData::~wxPaletteRefData() |
c801d85f | 54 | { |
cb332bc6 | 55 | delete[] m_entries; |
8fc613f1 | 56 | } |
c801d85f KB |
57 | |
58 | //----------------------------------------------------------------------------- | |
59 | ||
60 | #define M_PALETTEDATA ((wxPaletteRefData *)m_refData) | |
61 | ||
62 | IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject) | |
63 | ||
8bbe427f | 64 | wxPalette::wxPalette() |
c801d85f | 65 | { |
cb332bc6 | 66 | m_refData = NULL; |
8fc613f1 | 67 | } |
c801d85f | 68 | |
cb332bc6 | 69 | wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) |
c801d85f | 70 | { |
cb332bc6 | 71 | Create(n, red, green, blue); |
8fc613f1 | 72 | } |
c801d85f | 73 | |
cb332bc6 | 74 | wxPalette::wxPalette(const wxPalette& palette) |
c801d85f | 75 | { |
cb332bc6 | 76 | Ref(palette); |
8fc613f1 | 77 | } |
c801d85f | 78 | |
8bbe427f | 79 | wxPalette::~wxPalette() |
c801d85f | 80 | { |
8fc613f1 | 81 | } |
c801d85f | 82 | |
cb332bc6 | 83 | wxPalette& wxPalette::operator = (const wxPalette& palette) |
c801d85f | 84 | { |
8fc613f1 | 85 | if (*this == palette) return (*this); |
cb332bc6 | 86 | Ref(palette); |
8fc613f1 RR |
87 | return *this; |
88 | } | |
c801d85f | 89 | |
cb332bc6 | 90 | bool wxPalette::operator == (const wxPalette& palette) |
c801d85f | 91 | { |
8fc613f1 RR |
92 | return m_refData == palette.m_refData; |
93 | } | |
c801d85f | 94 | |
cb332bc6 | 95 | bool wxPalette::operator != (const wxPalette& palette) |
c801d85f | 96 | { |
8fc613f1 RR |
97 | return m_refData != palette.m_refData; |
98 | } | |
c801d85f KB |
99 | |
100 | bool wxPalette::Ok(void) const | |
101 | { | |
8fc613f1 RR |
102 | return (m_refData != NULL); |
103 | } | |
c801d85f | 104 | |
cb332bc6 VS |
105 | bool wxPalette::Create(int n, |
106 | const unsigned char *red, | |
d275c7eb | 107 | const unsigned char *green, |
cb332bc6 | 108 | const unsigned char *blue) |
c801d85f | 109 | { |
cb332bc6 VS |
110 | UnRef(); |
111 | m_refData = new wxPaletteRefData(); | |
d275c7eb VZ |
112 | |
113 | M_PALETTEDATA->m_count = n; | |
cb332bc6 VS |
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; | |
8fc613f1 | 125 | } |
c801d85f | 126 | |
cb332bc6 VS |
127 | int wxPalette::GetPixel( const unsigned char red, |
128 | const unsigned char green, | |
129 | const unsigned char blue ) const | |
c801d85f | 130 | { |
cb332bc6 VS |
131 | if (!m_refData) return FALSE; |
132 | ||
d275c7eb VZ |
133 | int closest = 0; |
134 | double d,distance = 1000.0; // max. dist is 256 | |
cb332bc6 VS |
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 | } | |
d275c7eb | 146 | return closest; |
8fc613f1 | 147 | } |
c801d85f | 148 | |
d275c7eb | 149 | bool wxPalette::GetRGB(int pixel, |
cb332bc6 | 150 | unsigned char *red, |
d275c7eb | 151 | unsigned char *green, |
cb332bc6 | 152 | unsigned char *blue) const |
c801d85f | 153 | { |
cb332bc6 VS |
154 | if (!m_refData) return FALSE; |
155 | if (pixel >= M_PALETTEDATA->m_count) return FALSE; | |
d275c7eb | 156 | |
cb332bc6 VS |
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; | |
8fc613f1 | 162 | } |
c801d85f | 163 | |
d275c7eb VZ |
164 | #endif // wxUSE_PALETTE |
165 | ||
cb332bc6 | 166 |