]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/paletteg.cpp
Borland has only lfind
[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// RCS-ID: $Id$
7// Copyright: (c) 1998 Robert Roebling and Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
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
19
20#include "wx/palette.h"
21
22//-----------------------------------------------------------------------------
23// wxPalette
24//-----------------------------------------------------------------------------
25
26struct wxPaletteEntry
27{
28 unsigned char red, green, blue;
29};
30
31class wxPaletteRefData: public wxObjectRefData
32{
33 public:
34
35 wxPaletteRefData(void);
36 virtual ~wxPaletteRefData(void);
37
38 int m_count;
39 wxPaletteEntry *m_entries;
40};
41
42wxPaletteRefData::wxPaletteRefData()
43{
44 m_count = 0;
45 m_entries = NULL;
46}
47
48wxPaletteRefData::~wxPaletteRefData()
49{
50 delete[] m_entries;
51}
52
53//-----------------------------------------------------------------------------
54
55#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
56
57IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
58
59wxPalette::wxPalette()
60{
61 m_refData = NULL;
62}
63
64wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
65{
66 Create(n, red, green, blue);
67}
68
69wxPalette::~wxPalette()
70{
71}
72
73bool wxPalette::IsOk() const
74{
75 return (m_refData != NULL);
76}
77
78int wxPalette::GetColoursCount() const
79{
80 if (m_refData)
81 return M_PALETTEDATA->m_count;
82
83 return 0;
84}
85
86bool wxPalette::Create(int n,
87 const unsigned char *red,
88 const unsigned char *green,
89 const unsigned char *blue)
90{
91 UnRef();
92 m_refData = new wxPaletteRefData();
93
94 M_PALETTEDATA->m_count = n;
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
105 return true;
106}
107
108int wxPalette::GetPixel( unsigned char red,
109 unsigned char green,
110 unsigned char blue ) const
111{
112 if (!m_refData) return wxNOT_FOUND;
113
114 int closest = 0;
115 double d,distance = 1000.0; // max. dist is 256
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 }
127 return closest;
128}
129
130bool wxPalette::GetRGB(int pixel,
131 unsigned char *red,
132 unsigned char *green,
133 unsigned char *blue) const
134{
135 if (!m_refData) return false;
136 if (pixel >= M_PALETTEDATA->m_count) return false;
137
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;
142 return true;
143}
144
145#endif // wxUSE_PALETTE