]> git.saurik.com Git - wxWidgets.git/blame - src/generic/paletteg.cpp
Don't generate any events from wxSpinCtrl and wxSpinCtrlDouble methods.
[wxWidgets.git] / src / generic / paletteg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
d275c7eb 2// Name: src/generic/paletteg.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6aa89a22 6// Copyright: (c) 1998 Robert Roebling and Julian Smart
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
d275c7eb
VZ
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#if defined(__BORLANDC__)
14 #pragma hdrstop
15#endif
16
17#if wxUSE_PALETTE
c801d85f 18
d275c7eb 19#include "wx/palette.h"
83624f79 20
c801d85f
KB
21//-----------------------------------------------------------------------------
22// wxPalette
23//-----------------------------------------------------------------------------
24
cb332bc6
VS
25struct wxPaletteEntry
26{
27 unsigned char red, green, blue;
28};
29
8f884a0d 30class wxPaletteRefData : public wxGDIRefData
c801d85f 31{
8f884a0d
VZ
32public:
33 wxPaletteRefData();
34 wxPaletteRefData(const wxPaletteRefData& palette);
35 virtual ~wxPaletteRefData();
8bbe427f 36
cb332bc6
VS
37 int m_count;
38 wxPaletteEntry *m_entries;
c801d85f
KB
39};
40
8bbe427f 41wxPaletteRefData::wxPaletteRefData()
c801d85f 42{
cb332bc6
VS
43 m_count = 0;
44 m_entries = NULL;
8fc613f1 45}
c801d85f 46
8f884a0d
VZ
47wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData& palette)
48{
49 m_count = palette.m_count;
50 m_entries = new wxPaletteEntry[m_count];
51 for ( int i = 0; i < m_count; i++ )
52 m_entries[i] = palette.m_entries[i];
53}
54
8bbe427f 55wxPaletteRefData::~wxPaletteRefData()
c801d85f 56{
cb332bc6 57 delete[] m_entries;
8fc613f1 58}
c801d85f
KB
59
60//-----------------------------------------------------------------------------
61
62#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
63
64IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
65
8bbe427f 66wxPalette::wxPalette()
c801d85f 67{
cb332bc6 68 m_refData = NULL;
8fc613f1 69}
c801d85f 70
cb332bc6 71wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
c801d85f 72{
cb332bc6 73 Create(n, red, green, blue);
8fc613f1 74}
c801d85f 75
8bbe427f 76wxPalette::~wxPalette()
c801d85f 77{
8fc613f1 78}
c801d85f 79
d72f87f6
RR
80int wxPalette::GetColoursCount() const
81{
82 if (m_refData)
83 return M_PALETTEDATA->m_count;
03647350
VZ
84
85 return 0;
d72f87f6
RR
86}
87
cb332bc6
VS
88bool wxPalette::Create(int n,
89 const unsigned char *red,
d275c7eb 90 const unsigned char *green,
cb332bc6 91 const unsigned char *blue)
c801d85f 92{
cb332bc6
VS
93 UnRef();
94 m_refData = new wxPaletteRefData();
d275c7eb
VZ
95
96 M_PALETTEDATA->m_count = n;
cb332bc6
VS
97 M_PALETTEDATA->m_entries = new wxPaletteEntry[n];
98
99 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
100 for (int i = 0; i < n; i++, e++)
101 {
102 e->red = red[i];
103 e->green = green[i];
104 e->blue = blue[i];
105 }
106
ca65c044 107 return true;
8fc613f1 108}
c801d85f 109
88ef3a57
WS
110int wxPalette::GetPixel( unsigned char red,
111 unsigned char green,
112 unsigned char blue ) const
c801d85f 113{
88ef3a57 114 if (!m_refData) return wxNOT_FOUND;
cb332bc6 115
d275c7eb
VZ
116 int closest = 0;
117 double d,distance = 1000.0; // max. dist is 256
cb332bc6
VS
118
119 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
120 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
121 {
122 if ((d = 0.299 * abs(red - e->red) +
123 0.587 * abs(green - e->green) +
124 0.114 * abs(blue - e->blue)) < distance) {
125 distance = d;
126 closest = i;
127 }
128 }
d275c7eb 129 return closest;
8fc613f1 130}
c801d85f 131
d275c7eb 132bool wxPalette::GetRGB(int pixel,
cb332bc6 133 unsigned char *red,
d275c7eb 134 unsigned char *green,
cb332bc6 135 unsigned char *blue) const
c801d85f 136{
ca65c044
WS
137 if (!m_refData) return false;
138 if (pixel >= M_PALETTEDATA->m_count) return false;
d275c7eb 139
cb332bc6
VS
140 wxPaletteEntry& p = M_PALETTEDATA->m_entries[pixel];
141 if (red) *red = p.red;
142 if (green) *green = p.green;
143 if (blue) *blue = p.blue;
ca65c044 144 return true;
8fc613f1 145}
c801d85f 146
8f884a0d
VZ
147wxGDIRefData *wxPalette::CreateGDIRefData() const
148{
149 return new wxPaletteRefData;
150}
151
152wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
153{
5c33522f 154 return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data));
8f884a0d
VZ
155}
156
d275c7eb 157#endif // wxUSE_PALETTE