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