]> git.saurik.com Git - wxWidgets.git/blob - src/generic/paletteg.cpp
Make wxToolBar styles consistent and add wxTB_DEFAULT_STYLE.
[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 // 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
26 struct wxPaletteEntry
27 {
28 unsigned char red, green, blue;
29 };
30
31 class wxPaletteRefData : public wxGDIRefData
32 {
33 public:
34 wxPaletteRefData();
35 wxPaletteRefData(const wxPaletteRefData& palette);
36 virtual ~wxPaletteRefData();
37
38 int m_count;
39 wxPaletteEntry *m_entries;
40 };
41
42 wxPaletteRefData::wxPaletteRefData()
43 {
44 m_count = 0;
45 m_entries = NULL;
46 }
47
48 wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData& palette)
49 {
50 m_count = palette.m_count;
51 m_entries = new wxPaletteEntry[m_count];
52 for ( int i = 0; i < m_count; i++ )
53 m_entries[i] = palette.m_entries[i];
54 }
55
56 wxPaletteRefData::~wxPaletteRefData()
57 {
58 delete[] m_entries;
59 }
60
61 //-----------------------------------------------------------------------------
62
63 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
64
65 IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
66
67 wxPalette::wxPalette()
68 {
69 m_refData = NULL;
70 }
71
72 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
73 {
74 Create(n, red, green, blue);
75 }
76
77 wxPalette::~wxPalette()
78 {
79 }
80
81 int wxPalette::GetColoursCount() const
82 {
83 if (m_refData)
84 return M_PALETTEDATA->m_count;
85
86 return 0;
87 }
88
89 bool wxPalette::Create(int n,
90 const unsigned char *red,
91 const unsigned char *green,
92 const unsigned char *blue)
93 {
94 UnRef();
95 m_refData = new wxPaletteRefData();
96
97 M_PALETTEDATA->m_count = n;
98 M_PALETTEDATA->m_entries = new wxPaletteEntry[n];
99
100 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
101 for (int i = 0; i < n; i++, e++)
102 {
103 e->red = red[i];
104 e->green = green[i];
105 e->blue = blue[i];
106 }
107
108 return true;
109 }
110
111 int wxPalette::GetPixel( unsigned char red,
112 unsigned char green,
113 unsigned char blue ) const
114 {
115 if (!m_refData) return wxNOT_FOUND;
116
117 int closest = 0;
118 double d,distance = 1000.0; // max. dist is 256
119
120 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
121 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
122 {
123 if ((d = 0.299 * abs(red - e->red) +
124 0.587 * abs(green - e->green) +
125 0.114 * abs(blue - e->blue)) < distance) {
126 distance = d;
127 closest = i;
128 }
129 }
130 return closest;
131 }
132
133 bool wxPalette::GetRGB(int pixel,
134 unsigned char *red,
135 unsigned char *green,
136 unsigned char *blue) const
137 {
138 if (!m_refData) return false;
139 if (pixel >= M_PALETTEDATA->m_count) return false;
140
141 wxPaletteEntry& p = M_PALETTEDATA->m_entries[pixel];
142 if (red) *red = p.red;
143 if (green) *green = p.green;
144 if (blue) *blue = p.blue;
145 return true;
146 }
147
148 wxGDIRefData *wxPalette::CreateGDIRefData() const
149 {
150 return new wxPaletteRefData;
151 }
152
153 wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
154 {
155 return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data));
156 }
157
158 #endif // wxUSE_PALETTE