]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/palette.cpp
merge of RTL changes (with many modifications) from SOC2006_RTL branch
[wxWidgets.git] / src / mgl / palette.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
88ef3a57 2// Name: src/mgl/palette.cpp
32b8ec41
VZ
3// Author: Vaclav Slavik
4// Created: 2001/03/11
5// Id: $Id$
c41c20a5 6// Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
65571936 7// Licence: wxWindows licence
32b8ec41
VZ
8/////////////////////////////////////////////////////////////////////////////
9
32b8ec41
VZ
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
25class wxPaletteRefData: public wxObjectRefData
26{
27 public:
28 wxPaletteRefData(void);
29 ~wxPaletteRefData(void);
88ef3a57 30
32b8ec41
VZ
31 int m_count;
32 palette_t *m_entries;
33};
34
35wxPaletteRefData::wxPaletteRefData()
36{
37 m_count = 0;
38 m_entries = NULL;
39}
40
41wxPaletteRefData::~wxPaletteRefData()
42{
43 delete[] m_entries;
44}
45
46//-----------------------------------------------------------------------------
47
48#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
49
50IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
51
52wxPalette::wxPalette()
53{
54 m_refData = NULL;
55}
56
57wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
58{
59 Create(n, red, green, blue);
60}
61
32b8ec41
VZ
62wxPalette::~wxPalette()
63{
64}
65
32b8ec41
VZ
66bool wxPalette::operator == (const wxPalette& palette) const
67{
68 return m_refData == palette.m_refData;
69}
70
71bool wxPalette::operator != (const wxPalette& palette) const
72{
73 return m_refData != palette.m_refData;
74}
75
76bool wxPalette::Ok(void) const
77{
78 return (m_refData != NULL);
79}
80
81bool wxPalette::Create(int n,
82 const unsigned char *red,
88ef3a57 83 const unsigned char *green,
32b8ec41
VZ
84 const unsigned char *blue)
85{
86 UnRef();
87 m_refData = new wxPaletteRefData();
88ef3a57
WS
88
89 M_PALETTEDATA->m_count = n;
32b8ec41
VZ
90 M_PALETTEDATA->m_entries = new palette_t[n];
91
92 palette_t *e = M_PALETTEDATA->m_entries;
93 for (int i = 0; i < n; i++, e++)
94 {
95 e->red = red[i];
96 e->green = green[i];
97 e->blue = blue[i];
98 e->alpha = 0;
99 }
100
88ef3a57 101 return true;
32b8ec41
VZ
102}
103
88ef3a57
WS
104int wxPalette::GetPixel(unsigned char red,
105 unsigned char green,
106 unsigned char blue) const
32b8ec41 107{
88ef3a57 108 if (!m_refData) return wxNOT_FOUND;
32b8ec41 109
88ef3a57
WS
110 int closest = 0;
111 double d, distance = 1000.0; // max. dist is 256
32b8ec41
VZ
112
113 palette_t *e = M_PALETTEDATA->m_entries;
114 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
115 {
116 if ((d = 0.299 * abs(red - e->red) +
117 0.587 * abs(green - e->green) +
118 0.114 * abs(blue - e->blue)) < distance) {
119 distance = d;
120 closest = i;
121 }
122 }
88ef3a57
WS
123
124 return closest;
32b8ec41
VZ
125}
126
88ef3a57 127bool wxPalette::GetRGB(int pixel,
32b8ec41 128 unsigned char *red,
88ef3a57 129 unsigned char *green,
32b8ec41
VZ
130 unsigned char *blue) const
131{
88ef3a57
WS
132 if (!m_refData) return false;
133 if (pixel >= M_PALETTEDATA->m_count) return false;
134
32b8ec41
VZ
135 palette_t& p = M_PALETTEDATA->m_entries[pixel];
136 if (red) *red = p.red;
137 if (green) *green = p.green;
138 if (blue) *blue = p.blue;
88ef3a57 139 return true;
32b8ec41
VZ
140}
141
142int wxPalette::GetColoursCount() const
143{
88ef3a57 144 wxCHECK_MSG( Ok(), 0, wxT("invalid palette") );
32b8ec41
VZ
145 return M_PALETTEDATA->m_count;
146}
147
148palette_t *wxPalette::GetMGLpalette_t() const
149{
88ef3a57 150 wxCHECK_MSG( Ok(), NULL, wxT("invalid palette") );
32b8ec41
VZ
151 return M_PALETTEDATA->m_entries;
152}