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