]> git.saurik.com Git - wxWidgets.git/blame - src/generic/paletteg.cpp
fixing the usage of hishape
[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
88ef3a57 6// RCS-ID: $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 35 wxPaletteRefData(void);
d3c7fc99 36 virtual ~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
8bbe427f 69wxPalette::~wxPalette()
c801d85f 70{
8fc613f1 71}
c801d85f 72
b7cacb43 73bool wxPalette::IsOk() const
c801d85f 74{
8fc613f1
RR
75 return (m_refData != NULL);
76}
c801d85f 77
d72f87f6
RR
78int wxPalette::GetColoursCount() const
79{
80 if (m_refData)
81 return M_PALETTEDATA->m_count;
82
83 return 0;
84}
85
cb332bc6
VS
86bool wxPalette::Create(int n,
87 const unsigned char *red,
d275c7eb 88 const unsigned char *green,
cb332bc6 89 const unsigned char *blue)
c801d85f 90{
cb332bc6
VS
91 UnRef();
92 m_refData = new wxPaletteRefData();
d275c7eb
VZ
93
94 M_PALETTEDATA->m_count = n;
cb332bc6
VS
95 M_PALETTEDATA->m_entries = new wxPaletteEntry[n];
96
97 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
98 for (int i = 0; i < n; i++, e++)
99 {
100 e->red = red[i];
101 e->green = green[i];
102 e->blue = blue[i];
103 }
104
ca65c044 105 return true;
8fc613f1 106}
c801d85f 107
88ef3a57
WS
108int wxPalette::GetPixel( unsigned char red,
109 unsigned char green,
110 unsigned char blue ) const
c801d85f 111{
88ef3a57 112 if (!m_refData) return wxNOT_FOUND;
cb332bc6 113
d275c7eb
VZ
114 int closest = 0;
115 double d,distance = 1000.0; // max. dist is 256
cb332bc6
VS
116
117 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
118 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
119 {
120 if ((d = 0.299 * abs(red - e->red) +
121 0.587 * abs(green - e->green) +
122 0.114 * abs(blue - e->blue)) < distance) {
123 distance = d;
124 closest = i;
125 }
126 }
d275c7eb 127 return closest;
8fc613f1 128}
c801d85f 129
d275c7eb 130bool wxPalette::GetRGB(int pixel,
cb332bc6 131 unsigned char *red,
d275c7eb 132 unsigned char *green,
cb332bc6 133 unsigned char *blue) const
c801d85f 134{
ca65c044
WS
135 if (!m_refData) return false;
136 if (pixel >= M_PALETTEDATA->m_count) return false;
d275c7eb 137
cb332bc6
VS
138 wxPaletteEntry& p = M_PALETTEDATA->m_entries[pixel];
139 if (red) *red = p.red;
140 if (green) *green = p.green;
141 if (blue) *blue = p.blue;
ca65c044 142 return true;
8fc613f1 143}
c801d85f 144
d275c7eb 145#endif // wxUSE_PALETTE