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