]>
Commit | Line | Data |
---|---|---|
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 |
26 | struct wxPaletteEntry |
27 | { | |
28 | unsigned char red, green, blue; | |
29 | }; | |
30 | ||
8f884a0d | 31 | class wxPaletteRefData : public wxGDIRefData |
c801d85f | 32 | { |
8f884a0d VZ |
33 | public: |
34 | wxPaletteRefData(); | |
35 | wxPaletteRefData(const wxPaletteRefData& palette); | |
36 | virtual ~wxPaletteRefData(); | |
8bbe427f | 37 | |
cb332bc6 VS |
38 | int m_count; |
39 | wxPaletteEntry *m_entries; | |
c801d85f KB |
40 | }; |
41 | ||
8bbe427f | 42 | wxPaletteRefData::wxPaletteRefData() |
c801d85f | 43 | { |
cb332bc6 VS |
44 | m_count = 0; |
45 | m_entries = NULL; | |
8fc613f1 | 46 | } |
c801d85f | 47 | |
8f884a0d VZ |
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 | ||
8bbe427f | 56 | wxPaletteRefData::~wxPaletteRefData() |
c801d85f | 57 | { |
cb332bc6 | 58 | delete[] m_entries; |
8fc613f1 | 59 | } |
c801d85f KB |
60 | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | #define M_PALETTEDATA ((wxPaletteRefData *)m_refData) | |
64 | ||
65 | IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject) | |
66 | ||
8bbe427f | 67 | wxPalette::wxPalette() |
c801d85f | 68 | { |
cb332bc6 | 69 | m_refData = NULL; |
8fc613f1 | 70 | } |
c801d85f | 71 | |
cb332bc6 | 72 | wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) |
c801d85f | 73 | { |
cb332bc6 | 74 | Create(n, red, green, blue); |
8fc613f1 | 75 | } |
c801d85f | 76 | |
8bbe427f | 77 | wxPalette::~wxPalette() |
c801d85f | 78 | { |
8fc613f1 | 79 | } |
c801d85f | 80 | |
d72f87f6 RR |
81 | int wxPalette::GetColoursCount() const |
82 | { | |
83 | if (m_refData) | |
84 | return M_PALETTEDATA->m_count; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
cb332bc6 VS |
89 | bool wxPalette::Create(int n, |
90 | const unsigned char *red, | |
d275c7eb | 91 | const unsigned char *green, |
cb332bc6 | 92 | const unsigned char *blue) |
c801d85f | 93 | { |
cb332bc6 VS |
94 | UnRef(); |
95 | m_refData = new wxPaletteRefData(); | |
d275c7eb VZ |
96 | |
97 | M_PALETTEDATA->m_count = n; | |
cb332bc6 VS |
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 | ||
ca65c044 | 108 | return true; |
8fc613f1 | 109 | } |
c801d85f | 110 | |
88ef3a57 WS |
111 | int wxPalette::GetPixel( unsigned char red, |
112 | unsigned char green, | |
113 | unsigned char blue ) const | |
c801d85f | 114 | { |
88ef3a57 | 115 | if (!m_refData) return wxNOT_FOUND; |
cb332bc6 | 116 | |
d275c7eb VZ |
117 | int closest = 0; |
118 | double d,distance = 1000.0; // max. dist is 256 | |
cb332bc6 VS |
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 | } | |
d275c7eb | 130 | return closest; |
8fc613f1 | 131 | } |
c801d85f | 132 | |
d275c7eb | 133 | bool wxPalette::GetRGB(int pixel, |
cb332bc6 | 134 | unsigned char *red, |
d275c7eb | 135 | unsigned char *green, |
cb332bc6 | 136 | unsigned char *blue) const |
c801d85f | 137 | { |
ca65c044 WS |
138 | if (!m_refData) return false; |
139 | if (pixel >= M_PALETTEDATA->m_count) return false; | |
d275c7eb | 140 | |
cb332bc6 VS |
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; | |
ca65c044 | 145 | return true; |
8fc613f1 | 146 | } |
c801d85f | 147 | |
8f884a0d VZ |
148 | wxGDIRefData *wxPalette::CreateGDIRefData() const |
149 | { | |
150 | return new wxPaletteRefData; | |
151 | } | |
152 | ||
153 | wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const | |
154 | { | |
5c33522f | 155 | return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data)); |
8f884a0d VZ |
156 | } |
157 | ||
d275c7eb | 158 | #endif // wxUSE_PALETTE |