]> git.saurik.com Git - wxWidgets.git/blob - src/msw/palette.cpp
more 2.0b9 updates
[wxWidgets.git] / src / msw / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: palette.cpp
3 // Purpose: wxPalette
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "palette.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/setup.h"
26 #include "wx/palette.h"
27 #endif
28
29 #include <windows.h>
30
31 #include "assert.h"
32
33 #if !USE_SHARED_LIBRARIES
34 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
35 #endif
36
37 /*
38 * Palette
39 *
40 */
41
42 wxPaletteRefData::wxPaletteRefData(void)
43 {
44 m_hPalette = 0;
45 }
46
47 wxPaletteRefData::~wxPaletteRefData(void)
48 {
49 if ( m_hPalette )
50 ::DeleteObject((HPALETTE) m_hPalette);
51 }
52
53 wxPalette::wxPalette(void)
54 {
55 }
56
57 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
58 {
59 Create(n, red, green, blue);
60 }
61
62 wxPalette::~wxPalette(void)
63 {
64 // FreeResource(TRUE);
65 }
66
67 bool wxPalette::FreeResource(bool force)
68 {
69 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
70 {
71 DeleteObject((HPALETTE)M_PALETTEDATA->m_hPalette);
72 }
73 return TRUE;
74 }
75
76 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
77 {
78 UnRef();
79
80 m_refData = new wxPaletteRefData;
81
82 NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
83 (WORD)n * sizeof(PALETTEENTRY));
84 if (!npPal)
85 return(FALSE);
86
87 npPal->palVersion = 0x300;
88 npPal->palNumEntries = n;
89
90 int i;
91 for (i = 0; i < n; i ++)
92 {
93 npPal->palPalEntry[i].peRed = red[i];
94 npPal->palPalEntry[i].peGreen = green[i];
95 npPal->palPalEntry[i].peBlue = blue[i];
96 npPal->palPalEntry[i].peFlags = 0;
97 }
98 M_PALETTEDATA->m_hPalette = (WXHPALETTE) CreatePalette((LPLOGPALETTE)npPal);
99 LocalFree((HANDLE)npPal);
100 return TRUE;
101 }
102
103 int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
104 {
105 if ( !m_refData )
106 return FALSE;
107
108 return ::GetNearestPaletteIndex((HPALETTE) M_PALETTEDATA->m_hPalette, PALETTERGB(red, green, blue));
109 }
110
111 bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
112 {
113 if ( !m_refData )
114 return FALSE;
115
116 if (index < 0 || index > 255)
117 return FALSE;
118
119 PALETTEENTRY entry;
120 if (::GetPaletteEntries((HPALETTE) M_PALETTEDATA->m_hPalette, index, 1, &entry))
121 {
122 *red = entry.peRed;
123 *green = entry.peGreen;
124 *blue = entry.peBlue;
125 return TRUE;
126 } else
127 return FALSE;
128 }
129
130 void wxPalette::SetHPALETTE(WXHPALETTE pal)
131 {
132 if ( !m_refData )
133 m_refData = new wxPaletteRefData;
134
135 M_PALETTEDATA->m_hPalette = pal;
136 }
137