]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/paletteg.cpp
switched to using bakefile_gen
[wxWidgets.git] / src / generic / paletteg.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/paletteg.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling and Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11
12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13#pragma implementation "paletteg.h"
14#endif
15
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
24
25#include "wx/palette.h"
26
27//-----------------------------------------------------------------------------
28// wxPalette
29//-----------------------------------------------------------------------------
30
31struct wxPaletteEntry
32{
33 unsigned char red, green, blue;
34};
35
36class wxPaletteRefData: public wxObjectRefData
37{
38 public:
39
40 wxPaletteRefData(void);
41 ~wxPaletteRefData(void);
42
43 int m_count;
44 wxPaletteEntry *m_entries;
45};
46
47wxPaletteRefData::wxPaletteRefData()
48{
49 m_count = 0;
50 m_entries = NULL;
51}
52
53wxPaletteRefData::~wxPaletteRefData()
54{
55 delete[] m_entries;
56}
57
58//-----------------------------------------------------------------------------
59
60#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
61
62IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
63
64wxPalette::wxPalette()
65{
66 m_refData = NULL;
67}
68
69wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
70{
71 Create(n, red, green, blue);
72}
73
74wxPalette::wxPalette(const wxPalette& palette)
75 : wxGDIObject()
76{
77 Ref(palette);
78}
79
80wxPalette::~wxPalette()
81{
82}
83
84wxPalette& wxPalette::operator = (const wxPalette& palette)
85{
86 if (*this == palette) return (*this);
87 Ref(palette);
88 return *this;
89}
90
91bool wxPalette::operator == (const wxPalette& palette)
92{
93 return m_refData == palette.m_refData;
94}
95
96bool wxPalette::operator != (const wxPalette& palette)
97{
98 return m_refData != palette.m_refData;
99}
100
101bool wxPalette::Ok(void) const
102{
103 return (m_refData != NULL);
104}
105
106bool wxPalette::Create(int n,
107 const unsigned char *red,
108 const unsigned char *green,
109 const unsigned char *blue)
110{
111 UnRef();
112 m_refData = new wxPaletteRefData();
113
114 M_PALETTEDATA->m_count = n;
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;
126}
127
128int wxPalette::GetPixel( const unsigned char red,
129 const unsigned char green,
130 const unsigned char blue ) const
131{
132 if (!m_refData) return FALSE;
133
134 int closest = 0;
135 double d,distance = 1000.0; // max. dist is 256
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 }
147 return closest;
148}
149
150bool wxPalette::GetRGB(int pixel,
151 unsigned char *red,
152 unsigned char *green,
153 unsigned char *blue) const
154{
155 if (!m_refData) return FALSE;
156 if (pixel >= M_PALETTEDATA->m_count) return FALSE;
157
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;
163}
164
165#endif // wxUSE_PALETTE
166
167