]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "palette.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/palette.h"
33 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
40 wxPaletteRefData::wxPaletteRefData(void)
45 wxPaletteRefData::~wxPaletteRefData(void)
48 ::DeleteObject((HPALETTE
) m_hPalette
);
51 wxPalette::wxPalette(void)
55 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
57 Create(n
, red
, green
, blue
);
60 wxPalette::~wxPalette(void)
62 // FreeResource(TRUE);
65 bool wxPalette::FreeResource(bool WXUNUSED(force
))
67 if ( M_PALETTEDATA
&& M_PALETTEDATA
->m_hPalette
)
69 DeleteObject((HPALETTE
)M_PALETTEDATA
->m_hPalette
);
74 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
84 m_refData
= new wxPaletteRefData
;
86 NPLOGPALETTE npPal
= (NPLOGPALETTE
)LocalAlloc(LMEM_FIXED
, sizeof(LOGPALETTE
) +
87 (WORD
)n
* sizeof(PALETTEENTRY
));
91 npPal
->palVersion
= 0x300;
92 npPal
->palNumEntries
= n
;
95 for (i
= 0; i
< n
; i
++)
97 npPal
->palPalEntry
[i
].peRed
= red
[i
];
98 npPal
->palPalEntry
[i
].peGreen
= green
[i
];
99 npPal
->palPalEntry
[i
].peBlue
= blue
[i
];
100 npPal
->palPalEntry
[i
].peFlags
= 0;
102 M_PALETTEDATA
->m_hPalette
= (WXHPALETTE
) CreatePalette((LPLOGPALETTE
)npPal
);
103 LocalFree((HANDLE
)npPal
);
109 int wxPalette::GetPixel(const unsigned char red
, const unsigned char green
, const unsigned char blue
) const
114 return ::GetNearestPaletteIndex((HPALETTE
) M_PALETTEDATA
->m_hPalette
, PALETTERGB(red
, green
, blue
));
117 bool wxPalette::GetRGB(int index
, unsigned char *red
, unsigned char *green
, unsigned char *blue
) const
122 if (index
< 0 || index
> 255)
126 if (::GetPaletteEntries((HPALETTE
) M_PALETTEDATA
->m_hPalette
, index
, 1, &entry
))
129 *green
= entry
.peGreen
;
130 *blue
= entry
.peBlue
;
136 void wxPalette::SetHPALETTE(WXHPALETTE pal
)
139 m_refData
= new wxPaletteRefData
;
141 M_PALETTEDATA
->m_hPalette
= pal
;