]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/paletteg.cpp
corrected CodeWarrior project target names and generated application names
[wxWidgets.git] / src / generic / paletteg.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: palette.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "paletteg.h"
14#endif
15
16#include "wx/palette.h"
17
18
19//-----------------------------------------------------------------------------
20// wxPalette
21//-----------------------------------------------------------------------------
22
23struct wxPaletteEntry
24{
25 unsigned char red, green, blue;
26};
27
28class wxPaletteRefData: public wxObjectRefData
29{
30 public:
31
32 wxPaletteRefData(void);
33 ~wxPaletteRefData(void);
34
35 int m_count;
36 wxPaletteEntry *m_entries;
37};
38
39wxPaletteRefData::wxPaletteRefData()
40{
41 m_count = 0;
42 m_entries = NULL;
43}
44
45wxPaletteRefData::~wxPaletteRefData()
46{
47 delete[] m_entries;
48}
49
50//-----------------------------------------------------------------------------
51
52#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
53
54IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
55
56wxPalette::wxPalette()
57{
58 m_refData = NULL;
59}
60
61wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
62{
63 Create(n, red, green, blue);
64}
65
66wxPalette::wxPalette(const wxPalette& palette)
67{
68 Ref(palette);
69}
70
71wxPalette::~wxPalette()
72{
73}
74
75wxPalette& wxPalette::operator = (const wxPalette& palette)
76{
77 if (*this == palette) return (*this);
78 Ref(palette);
79 return *this;
80}
81
82bool wxPalette::operator == (const wxPalette& palette)
83{
84 return m_refData == palette.m_refData;
85}
86
87bool wxPalette::operator != (const wxPalette& palette)
88{
89 return m_refData != palette.m_refData;
90}
91
92bool wxPalette::Ok(void) const
93{
94 return (m_refData != NULL);
95}
96
97bool wxPalette::Create(int n,
98 const unsigned char *red,
99 const unsigned char *green,
100 const unsigned char *blue)
101{
102 UnRef();
103 m_refData = new wxPaletteRefData();
104
105 M_PALETTEDATA->m_count = n;
106 M_PALETTEDATA->m_entries = new wxPaletteEntry[n];
107
108 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
109 for (int i = 0; i < n; i++, e++)
110 {
111 e->red = red[i];
112 e->green = green[i];
113 e->blue = blue[i];
114 }
115
116 return TRUE;
117}
118
119int wxPalette::GetPixel( const unsigned char red,
120 const unsigned char green,
121 const unsigned char blue ) const
122{
123 if (!m_refData) return FALSE;
124
125 int closest = 0;
126 double d,distance = 1000.0; // max. dist is 256
127
128 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
129 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
130 {
131 if ((d = 0.299 * abs(red - e->red) +
132 0.587 * abs(green - e->green) +
133 0.114 * abs(blue - e->blue)) < distance) {
134 distance = d;
135 closest = i;
136 }
137 }
138 return closest;
139}
140
141bool wxPalette::GetRGB(int pixel,
142 unsigned char *red,
143 unsigned char *green,
144 unsigned char *blue) const
145{
146 if (!m_refData) return FALSE;
147 if (pixel >= M_PALETTEDATA->m_count) return FALSE;
148
149 wxPaletteEntry& p = M_PALETTEDATA->m_entries[pixel];
150 if (red) *red = p.red;
151 if (green) *green = p.green;
152 if (blue) *blue = p.blue;
153 return TRUE;
154}
155
156