]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/palette.cpp
set error to GSOCK_TIMEOUT if the socket timed out (modified and extended patch 1303554)
[wxWidgets.git] / src / mgl / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 ~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(const wxPalette& palette)
63 {
64 Ref(palette);
65 }
66
67 wxPalette::~wxPalette()
68 {
69 }
70
71 wxPalette& wxPalette::operator = (const wxPalette& palette)
72 {
73 if (*this == palette) return (*this);
74 Ref(palette);
75 return *this;
76 }
77
78 bool wxPalette::operator == (const wxPalette& palette) const
79 {
80 return m_refData == palette.m_refData;
81 }
82
83 bool wxPalette::operator != (const wxPalette& palette) const
84 {
85 return m_refData != palette.m_refData;
86 }
87
88 bool wxPalette::Ok(void) const
89 {
90 return (m_refData != NULL);
91 }
92
93 bool wxPalette::Create(int n,
94 const unsigned char *red,
95 const unsigned char *green,
96 const unsigned char *blue)
97 {
98 UnRef();
99 m_refData = new wxPaletteRefData();
100
101 M_PALETTEDATA->m_count = n;
102 M_PALETTEDATA->m_entries = new palette_t[n];
103
104 palette_t *e = M_PALETTEDATA->m_entries;
105 for (int i = 0; i < n; i++, e++)
106 {
107 e->red = red[i];
108 e->green = green[i];
109 e->blue = blue[i];
110 e->alpha = 0;
111 }
112
113 return TRUE;
114 }
115
116 int wxPalette::GetPixel(const unsigned char red,
117 const unsigned char green,
118 const unsigned char blue) const
119 {
120 if (!m_refData) return FALSE;
121
122 int closest = 0;
123 double d,distance = 1000.0; // max. dist is 256
124
125 palette_t *e = M_PALETTEDATA->m_entries;
126 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
127 {
128 if ((d = 0.299 * abs(red - e->red) +
129 0.587 * abs(green - e->green) +
130 0.114 * abs(blue - e->blue)) < distance) {
131 distance = d;
132 closest = i;
133 }
134 }
135 return closest;
136 }
137
138 bool wxPalette::GetRGB(int pixel,
139 unsigned char *red,
140 unsigned char *green,
141 unsigned char *blue) const
142 {
143 if (!m_refData) return FALSE;
144 if (pixel >= M_PALETTEDATA->m_count) return FALSE;
145
146 palette_t& p = M_PALETTEDATA->m_entries[pixel];
147 if (red) *red = p.red;
148 if (green) *green = p.green;
149 if (blue) *blue = p.blue;
150 return TRUE;
151 }
152
153 int wxPalette::GetColoursCount() const
154 {
155 wxCHECK_MSG( Ok(), 0, wxT("invalid palette") );
156 return M_PALETTEDATA->m_count;
157 }
158
159 palette_t *wxPalette::GetMGLpalette_t() const
160 {
161 wxCHECK_MSG( Ok(), NULL, wxT("invalid palette") );
162 return M_PALETTEDATA->m_entries;
163 }
164