]> git.saurik.com Git - wxWidgets.git/blame - src/generic/paletteg.cpp
textctrl.cpp: Removed assert from CanRedo/CanUndo since the best behaviour is
[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
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
d275c7eb 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
45ef9db3 13#pragma implementation "paletteg.h"
c801d85f
KB
14#endif
15
d275c7eb
VZ
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#if defined(__BORLANDC__)
20 #pragma hdrstop
21#endif
22
23#if wxUSE_PALETTE
c801d85f 24
d275c7eb 25#include "wx/palette.h"
83624f79 26
c801d85f
KB
27//-----------------------------------------------------------------------------
28// wxPalette
29//-----------------------------------------------------------------------------
30
cb332bc6
VS
31struct wxPaletteEntry
32{
33 unsigned char red, green, blue;
34};
35
c801d85f
KB
36class wxPaletteRefData: public wxObjectRefData
37{
38 public:
8bbe427f 39
c801d85f
KB
40 wxPaletteRefData(void);
41 ~wxPaletteRefData(void);
8bbe427f 42
cb332bc6
VS
43 int m_count;
44 wxPaletteEntry *m_entries;
c801d85f
KB
45};
46
8bbe427f 47wxPaletteRefData::wxPaletteRefData()
c801d85f 48{
cb332bc6
VS
49 m_count = 0;
50 m_entries = NULL;
8fc613f1 51}
c801d85f 52
8bbe427f 53wxPaletteRefData::~wxPaletteRefData()
c801d85f 54{
cb332bc6 55 delete[] m_entries;
8fc613f1 56}
c801d85f
KB
57
58//-----------------------------------------------------------------------------
59
60#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
61
62IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
63
8bbe427f 64wxPalette::wxPalette()
c801d85f 65{
cb332bc6 66 m_refData = NULL;
8fc613f1 67}
c801d85f 68
cb332bc6 69wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
c801d85f 70{
cb332bc6 71 Create(n, red, green, blue);
8fc613f1 72}
c801d85f 73
cb332bc6 74wxPalette::wxPalette(const wxPalette& palette)
9f081f02 75 : wxGDIObject()
c801d85f 76{
cb332bc6 77 Ref(palette);
8fc613f1 78}
c801d85f 79
8bbe427f 80wxPalette::~wxPalette()
c801d85f 81{
8fc613f1 82}
c801d85f 83
cb332bc6 84wxPalette& wxPalette::operator = (const wxPalette& palette)
c801d85f 85{
8fc613f1 86 if (*this == palette) return (*this);
cb332bc6 87 Ref(palette);
8fc613f1
RR
88 return *this;
89}
c801d85f 90
cb332bc6 91bool wxPalette::operator == (const wxPalette& palette)
c801d85f 92{
8fc613f1
RR
93 return m_refData == palette.m_refData;
94}
c801d85f 95
cb332bc6 96bool wxPalette::operator != (const wxPalette& palette)
c801d85f 97{
8fc613f1
RR
98 return m_refData != palette.m_refData;
99}
c801d85f
KB
100
101bool wxPalette::Ok(void) const
102{
8fc613f1
RR
103 return (m_refData != NULL);
104}
c801d85f 105
cb332bc6
VS
106bool wxPalette::Create(int n,
107 const unsigned char *red,
d275c7eb 108 const unsigned char *green,
cb332bc6 109 const unsigned char *blue)
c801d85f 110{
cb332bc6
VS
111 UnRef();
112 m_refData = new wxPaletteRefData();
d275c7eb
VZ
113
114 M_PALETTEDATA->m_count = n;
cb332bc6
VS
115 M_PALETTEDATA->m_entries = new wxPaletteEntry[n];
116
117 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
118 for (int i = 0; i < n; i++, e++)
119 {
120 e->red = red[i];
121 e->green = green[i];
122 e->blue = blue[i];
123 }
124
125 return TRUE;
8fc613f1 126}
c801d85f 127
cb332bc6
VS
128int wxPalette::GetPixel( const unsigned char red,
129 const unsigned char green,
130 const unsigned char blue ) const
c801d85f 131{
cb332bc6
VS
132 if (!m_refData) return FALSE;
133
d275c7eb
VZ
134 int closest = 0;
135 double d,distance = 1000.0; // max. dist is 256
cb332bc6
VS
136
137 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
138 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
139 {
140 if ((d = 0.299 * abs(red - e->red) +
141 0.587 * abs(green - e->green) +
142 0.114 * abs(blue - e->blue)) < distance) {
143 distance = d;
144 closest = i;
145 }
146 }
d275c7eb 147 return closest;
8fc613f1 148}
c801d85f 149
d275c7eb 150bool wxPalette::GetRGB(int pixel,
cb332bc6 151 unsigned char *red,
d275c7eb 152 unsigned char *green,
cb332bc6 153 unsigned char *blue) const
c801d85f 154{
cb332bc6
VS
155 if (!m_refData) return FALSE;
156 if (pixel >= M_PALETTEDATA->m_count) return FALSE;
d275c7eb 157
cb332bc6
VS
158 wxPaletteEntry& p = M_PALETTEDATA->m_entries[pixel];
159 if (red) *red = p.red;
160 if (green) *green = p.green;
161 if (blue) *blue = p.blue;
162 return TRUE;
8fc613f1 163}
c801d85f 164
d275c7eb
VZ
165#endif // wxUSE_PALETTE
166
cb332bc6 167