]>
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 | 82 | |
d72f87f6 | 83 | bool wxPalette::Ok() const |
c801d85f | 84 | { |
8fc613f1 RR |
85 | return (m_refData != NULL); |
86 | } | |
c801d85f | 87 | |
d72f87f6 RR |
88 | int wxPalette::GetColoursCount() const |
89 | { | |
90 | if (m_refData) | |
91 | return M_PALETTEDATA->m_count; | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
cb332bc6 VS |
96 | bool wxPalette::Create(int n, |
97 | const unsigned char *red, | |
d275c7eb | 98 | const unsigned char *green, |
cb332bc6 | 99 | const unsigned char *blue) |
c801d85f | 100 | { |
cb332bc6 VS |
101 | UnRef(); |
102 | m_refData = new wxPaletteRefData(); | |
d275c7eb VZ |
103 | |
104 | M_PALETTEDATA->m_count = n; | |
cb332bc6 VS |
105 | M_PALETTEDATA->m_entries = new wxPaletteEntry[n]; |
106 | ||
107 | wxPaletteEntry *e = M_PALETTEDATA->m_entries; | |
108 | for (int i = 0; i < n; i++, e++) | |
109 | { | |
110 | e->red = red[i]; | |
111 | e->green = green[i]; | |
112 | e->blue = blue[i]; | |
113 | } | |
114 | ||
ca65c044 | 115 | return true; |
8fc613f1 | 116 | } |
c801d85f | 117 | |
88ef3a57 WS |
118 | int wxPalette::GetPixel( unsigned char red, |
119 | unsigned char green, | |
120 | unsigned char blue ) const | |
c801d85f | 121 | { |
88ef3a57 | 122 | if (!m_refData) return wxNOT_FOUND; |
cb332bc6 | 123 | |
d275c7eb VZ |
124 | int closest = 0; |
125 | double d,distance = 1000.0; // max. dist is 256 | |
cb332bc6 VS |
126 | |
127 | wxPaletteEntry *e = M_PALETTEDATA->m_entries; | |
128 | for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++) | |
129 | { | |
130 | if ((d = 0.299 * abs(red - e->red) + | |
131 | 0.587 * abs(green - e->green) + | |
132 | 0.114 * abs(blue - e->blue)) < distance) { | |
133 | distance = d; | |
134 | closest = i; | |
135 | } | |
136 | } | |
d275c7eb | 137 | return closest; |
8fc613f1 | 138 | } |
c801d85f | 139 | |
d275c7eb | 140 | bool wxPalette::GetRGB(int pixel, |
cb332bc6 | 141 | unsigned char *red, |
d275c7eb | 142 | unsigned char *green, |
cb332bc6 | 143 | unsigned char *blue) const |
c801d85f | 144 | { |
ca65c044 WS |
145 | if (!m_refData) return false; |
146 | if (pixel >= M_PALETTEDATA->m_count) return false; | |
d275c7eb | 147 | |
cb332bc6 VS |
148 | wxPaletteEntry& p = M_PALETTEDATA->m_entries[pixel]; |
149 | if (red) *red = p.red; | |
150 | if (green) *green = p.green; | |
151 | if (blue) *blue = p.blue; | |
ca65c044 | 152 | return true; |
8fc613f1 | 153 | } |
c801d85f | 154 | |
d275c7eb | 155 | #endif // wxUSE_PALETTE |