]> git.saurik.com Git - wxWidgets.git/blame - src/generic/paletteg.cpp
added xpm
[wxWidgets.git] / src / generic / paletteg.cpp
CommitLineData
c801d85f
KB
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__
45ef9db3 13#pragma implementation "paletteg.h"
c801d85f
KB
14#endif
15
16#include "wx/palette.h"
17
83624f79 18
c801d85f
KB
19//-----------------------------------------------------------------------------
20// wxPalette
21//-----------------------------------------------------------------------------
22
cb332bc6
VS
23struct wxPaletteEntry
24{
25 unsigned char red, green, blue;
26};
27
c801d85f
KB
28class wxPaletteRefData: public wxObjectRefData
29{
30 public:
8bbe427f 31
c801d85f
KB
32 wxPaletteRefData(void);
33 ~wxPaletteRefData(void);
8bbe427f 34
cb332bc6
VS
35 int m_count;
36 wxPaletteEntry *m_entries;
c801d85f
KB
37};
38
8bbe427f 39wxPaletteRefData::wxPaletteRefData()
c801d85f 40{
cb332bc6
VS
41 m_count = 0;
42 m_entries = NULL;
8fc613f1 43}
c801d85f 44
8bbe427f 45wxPaletteRefData::~wxPaletteRefData()
c801d85f 46{
cb332bc6 47 delete[] m_entries;
8fc613f1 48}
c801d85f
KB
49
50//-----------------------------------------------------------------------------
51
52#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
53
54IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
55
8bbe427f 56wxPalette::wxPalette()
c801d85f 57{
cb332bc6 58 m_refData = NULL;
8fc613f1 59}
c801d85f 60
cb332bc6 61wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
c801d85f 62{
cb332bc6 63 Create(n, red, green, blue);
8fc613f1 64}
c801d85f 65
cb332bc6 66wxPalette::wxPalette(const wxPalette& palette)
c801d85f 67{
cb332bc6 68 Ref(palette);
8fc613f1 69}
c801d85f 70
8bbe427f 71wxPalette::~wxPalette()
c801d85f 72{
8fc613f1 73}
c801d85f 74
cb332bc6 75wxPalette& wxPalette::operator = (const wxPalette& palette)
c801d85f 76{
8fc613f1 77 if (*this == palette) return (*this);
cb332bc6 78 Ref(palette);
8fc613f1
RR
79 return *this;
80}
c801d85f 81
cb332bc6 82bool wxPalette::operator == (const wxPalette& palette)
c801d85f 83{
8fc613f1
RR
84 return m_refData == palette.m_refData;
85}
c801d85f 86
cb332bc6 87bool wxPalette::operator != (const wxPalette& palette)
c801d85f 88{
8fc613f1
RR
89 return m_refData != palette.m_refData;
90}
c801d85f
KB
91
92bool wxPalette::Ok(void) const
93{
8fc613f1
RR
94 return (m_refData != NULL);
95}
c801d85f 96
cb332bc6
VS
97bool wxPalette::Create(int n,
98 const unsigned char *red,
99 const unsigned char *green,
100 const unsigned char *blue)
c801d85f 101{
cb332bc6
VS
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;
8fc613f1 117}
c801d85f 118
cb332bc6
VS
119int wxPalette::GetPixel( const unsigned char red,
120 const unsigned char green,
121 const unsigned char blue ) const
c801d85f 122{
cb332bc6
VS
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;
8fc613f1 139}
c801d85f 140
cb332bc6
VS
141bool wxPalette::GetRGB(int pixel,
142 unsigned char *red,
143 unsigned char *green,
144 unsigned char *blue) const
c801d85f 145{
cb332bc6
VS
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;
8fc613f1 154}
c801d85f 155
cb332bc6 156