]>
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 | ||
c801d85f KB |
31 | class 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 | 42 | wxPaletteRefData::wxPaletteRefData() |
c801d85f | 43 | { |
cb332bc6 VS |
44 | m_count = 0; |
45 | m_entries = NULL; | |
8fc613f1 | 46 | } |
c801d85f | 47 | |
8bbe427f | 48 | wxPaletteRefData::~wxPaletteRefData() |
c801d85f | 49 | { |
cb332bc6 | 50 | delete[] m_entries; |
8fc613f1 | 51 | } |
c801d85f KB |
52 | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
55 | #define M_PALETTEDATA ((wxPaletteRefData *)m_refData) | |
56 | ||
57 | IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject) | |
58 | ||
8bbe427f | 59 | wxPalette::wxPalette() |
c801d85f | 60 | { |
cb332bc6 | 61 | m_refData = NULL; |
8fc613f1 | 62 | } |
c801d85f | 63 | |
cb332bc6 | 64 | wxPalette::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 | 69 | wxPalette::~wxPalette() |
c801d85f | 70 | { |
8fc613f1 | 71 | } |
c801d85f | 72 | |
fbfb8bcc | 73 | bool wxPalette::operator == (const wxPalette& palette) const |
c801d85f | 74 | { |
8fc613f1 RR |
75 | return m_refData == palette.m_refData; |
76 | } | |
c801d85f | 77 | |
fbfb8bcc | 78 | bool wxPalette::operator != (const wxPalette& palette) const |
c801d85f | 79 | { |
8fc613f1 RR |
80 | return m_refData != palette.m_refData; |
81 | } | |
c801d85f KB |
82 | |
83 | bool wxPalette::Ok(void) const | |
84 | { | |
8fc613f1 RR |
85 | return (m_refData != NULL); |
86 | } | |
c801d85f | 87 | |
cb332bc6 VS |
88 | bool wxPalette::Create(int n, |
89 | const unsigned char *red, | |
d275c7eb | 90 | const unsigned char *green, |
cb332bc6 | 91 | const unsigned char *blue) |
c801d85f | 92 | { |
cb332bc6 VS |
93 | UnRef(); |
94 | m_refData = new wxPaletteRefData(); | |
d275c7eb VZ |
95 | |
96 | M_PALETTEDATA->m_count = n; | |
cb332bc6 VS |
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 | ||
ca65c044 | 107 | return true; |
8fc613f1 | 108 | } |
c801d85f | 109 | |
88ef3a57 WS |
110 | int wxPalette::GetPixel( unsigned char red, |
111 | unsigned char green, | |
112 | unsigned char blue ) const | |
c801d85f | 113 | { |
88ef3a57 | 114 | if (!m_refData) return wxNOT_FOUND; |
cb332bc6 | 115 | |
d275c7eb VZ |
116 | int closest = 0; |
117 | double d,distance = 1000.0; // max. dist is 256 | |
cb332bc6 VS |
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 | } | |
d275c7eb | 129 | return closest; |
8fc613f1 | 130 | } |
c801d85f | 131 | |
d275c7eb | 132 | bool wxPalette::GetRGB(int pixel, |
cb332bc6 | 133 | unsigned char *red, |
d275c7eb | 134 | unsigned char *green, |
cb332bc6 | 135 | unsigned char *blue) const |
c801d85f | 136 | { |
ca65c044 WS |
137 | if (!m_refData) return false; |
138 | if (pixel >= M_PALETTEDATA->m_count) return false; | |
d275c7eb | 139 | |
cb332bc6 VS |
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; | |
ca65c044 | 144 | return true; |
8fc613f1 | 145 | } |
c801d85f | 146 | |
d275c7eb | 147 | #endif // wxUSE_PALETTE |