]>
Commit | Line | Data |
---|---|---|
32b8ec41 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: palette.cpp | |
3 | // Author: Vaclav Slavik | |
4 | // Created: 2001/03/11 | |
5 | // Id: $Id$ | |
8f7b34a8 | 6 | // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) |
32b8ec41 VZ |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "palette.h" | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
22 | #include "wx/palette.h" | |
23 | #include <mgraph.h> | |
24 | ||
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // wxPalette | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | class wxPaletteRefData: public wxObjectRefData | |
31 | { | |
32 | public: | |
33 | wxPaletteRefData(void); | |
34 | ~wxPaletteRefData(void); | |
35 | ||
36 | int m_count; | |
37 | palette_t *m_entries; | |
38 | }; | |
39 | ||
40 | wxPaletteRefData::wxPaletteRefData() | |
41 | { | |
42 | m_count = 0; | |
43 | m_entries = NULL; | |
44 | } | |
45 | ||
46 | wxPaletteRefData::~wxPaletteRefData() | |
47 | { | |
48 | delete[] m_entries; | |
49 | } | |
50 | ||
51 | //----------------------------------------------------------------------------- | |
52 | ||
53 | #define M_PALETTEDATA ((wxPaletteRefData *)m_refData) | |
54 | ||
55 | IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject) | |
56 | ||
57 | wxPalette::wxPalette() | |
58 | { | |
59 | m_refData = NULL; | |
60 | } | |
61 | ||
62 | wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) | |
63 | { | |
64 | Create(n, red, green, blue); | |
65 | } | |
66 | ||
67 | wxPalette::wxPalette(const wxPalette& palette) | |
68 | { | |
69 | Ref(palette); | |
70 | } | |
71 | ||
72 | wxPalette::~wxPalette() | |
73 | { | |
74 | } | |
75 | ||
76 | wxPalette& wxPalette::operator = (const wxPalette& palette) | |
77 | { | |
78 | if (*this == palette) return (*this); | |
79 | Ref(palette); | |
80 | return *this; | |
81 | } | |
82 | ||
83 | bool wxPalette::operator == (const wxPalette& palette) const | |
84 | { | |
85 | return m_refData == palette.m_refData; | |
86 | } | |
87 | ||
88 | bool wxPalette::operator != (const wxPalette& palette) const | |
89 | { | |
90 | return m_refData != palette.m_refData; | |
91 | } | |
92 | ||
93 | bool wxPalette::Ok(void) const | |
94 | { | |
95 | return (m_refData != NULL); | |
96 | } | |
97 | ||
98 | bool wxPalette::Create(int n, | |
99 | const unsigned char *red, | |
100 | const unsigned char *green, | |
101 | const unsigned char *blue) | |
102 | { | |
103 | UnRef(); | |
104 | m_refData = new wxPaletteRefData(); | |
105 | ||
106 | M_PALETTEDATA->m_count = n; | |
107 | M_PALETTEDATA->m_entries = new palette_t[n]; | |
108 | ||
109 | palette_t *e = M_PALETTEDATA->m_entries; | |
110 | for (int i = 0; i < n; i++, e++) | |
111 | { | |
112 | e->red = red[i]; | |
113 | e->green = green[i]; | |
114 | e->blue = blue[i]; | |
115 | e->alpha = 0; | |
116 | } | |
117 | ||
118 | return TRUE; | |
119 | } | |
120 | ||
121 | int wxPalette::GetPixel(const unsigned char red, | |
122 | const unsigned char green, | |
123 | const unsigned char blue) const | |
124 | { | |
125 | if (!m_refData) return FALSE; | |
126 | ||
127 | int closest = 0; | |
128 | double d,distance = 1000.0; // max. dist is 256 | |
129 | ||
130 | palette_t *e = M_PALETTEDATA->m_entries; | |
131 | for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++) | |
132 | { | |
133 | if ((d = 0.299 * abs(red - e->red) + | |
134 | 0.587 * abs(green - e->green) + | |
135 | 0.114 * abs(blue - e->blue)) < distance) { | |
136 | distance = d; | |
137 | closest = i; | |
138 | } | |
139 | } | |
140 | return closest; | |
141 | } | |
142 | ||
143 | bool wxPalette::GetRGB(int pixel, | |
144 | unsigned char *red, | |
145 | unsigned char *green, | |
146 | unsigned char *blue) const | |
147 | { | |
148 | if (!m_refData) return FALSE; | |
149 | if (pixel >= M_PALETTEDATA->m_count) return FALSE; | |
150 | ||
151 | palette_t& p = M_PALETTEDATA->m_entries[pixel]; | |
152 | if (red) *red = p.red; | |
153 | if (green) *green = p.green; | |
154 | if (blue) *blue = p.blue; | |
155 | return TRUE; | |
156 | } | |
157 | ||
158 | int wxPalette::GetColoursCount() const | |
159 | { | |
160 | wxCHECK_MSG( Ok(), 0, wxT("invalid palette") ); | |
161 | return M_PALETTEDATA->m_count; | |
162 | } | |
163 | ||
164 | palette_t *wxPalette::GetMGLpalette_t() const | |
165 | { | |
166 | wxCHECK_MSG( Ok(), NULL, wxT("invalid palette") ); | |
167 | return M_PALETTEDATA->m_entries; | |
168 | } | |
169 |