Palette code
[wxWidgets.git] / src / os2 / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: palette.cpp
3 // Purpose: wxPalette
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include <stdio.h>
17 #include "wx/setup.h"
18 #include "wx/palette.h"
19 #endif
20
21 #define INCL_PM
22 #define INCL_GPI
23 #include <os2.h>
24
25 #include "assert.h"
26
27 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
28
29 /*
30 * Palette
31 *
32 */
33
34 wxPaletteRefData::wxPaletteRefData()
35 {
36 m_hPalette = NULLHANDLE;
37 m_hPS = NULLHANDLE;
38 } // end of wxPaletteRefData::wxPaletteRefData
39
40 wxPaletteRefData::~wxPaletteRefData()
41 {
42 if ( m_hPalette )
43 return;
44 } // end of wxPaletteRefData::~wxPaletteRefData
45
46 wxPalette::wxPalette()
47 {
48 } // end of wxPalette::wxPalette
49
50 wxPalette::wxPalette(
51 int n
52 , const unsigned char* pRed
53 , const unsigned char* pGreen
54 , const unsigned char* pBlue
55 )
56 {
57 Create( n
58 ,pRed
59 ,pGreen
60 ,pBlue
61 );
62 } // end of wxPalette::wxPalette
63
64 wxPalette::~wxPalette()
65 {
66 } // end of wxPalette::~wxPalette
67
68 bool wxPalette::FreeResource(
69 bool bForce
70 )
71 {
72 if ( M_PALETTEDATA && M_PALETTEDATA->m_hPalette)
73 {
74 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, NULLHANDLE);
75 ::GpiDeletePalette((HPAL)M_PALETTEDATA->m_hPalette);
76 }
77 return TRUE;
78 } // end of wxPalette::FreeResource
79
80 bool wxPalette::Create(
81 int n
82 , const unsigned char* pRed
83 , const unsigned char* pGreen
84 , const unsigned char* pBlue
85 )
86 {
87 PULONG pualTable;
88
89 UnRef();
90
91 m_refData = new wxPaletteRefData;
92 pualTable = new ULONG[n];
93 if (!pualTable)
94 return(FALSE);
95
96 for (int i = 0; i < n; i ++)
97 {
98 pualTable[i] = (PC_RESERVED * 16777216) + ((int)pRed[i] * 65536) + ((int)pGreen[i] * 256) + (int)pBlue[i];
99 }
100 M_PALETTEDATA->m_hPalette = (WXHPALETTE)::GpiCreatePalette( vHabmain
101 ,LCOL_PURECOLOR
102 ,LCOLF_CONSECRGB
103 ,(LONG)n
104 ,pualTable
105 );
106 delete [] pualTable;
107 return TRUE;
108 } // end of wxPalette::Create
109
110 int wxPalette::GetPixel(
111 const unsigned char cRed
112 , const unsigned char cGreen
113 , const unsigned char cBlue
114 ) const
115 {
116 bool bFound = FALSE;
117 PULONG pualTable = NULL;
118 ULONG ulNumEntries;
119 ULONG ulRGB = (PC_RESERVED * 16777216) +
120 ((int)cRed * 65536) +
121 ((int)cGreen * 256) +
122 (int)cBlue;
123
124 if (!m_refData)
125 return FALSE;
126
127 //
128 // Get number of entries first
129 //
130 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
131 ,M_PALETTEDATA->m_hPS
132 ,0 // No options
133 ,0 // No start index
134 ,0 // Force return of number entries
135 ,NULL // No array
136 );
137
138 pualTable = new ULONG[ulNumEntries];
139
140 //
141 // Now get the entries
142 //
143 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
144 ,M_PALETTEDATA->m_hPS
145 ,0 // No options
146 ,0 // start at 0
147 ,ulNumEntries // Force return of number entries
148 ,pualTable // Palette entry array with RGB values
149 );
150 //
151 // Now loop through and find the matching entry
152 //
153 for (int i = 0; i < ulNumEntries; i++)
154 {
155 if (pualTable[i] == ulRGB)
156 {
157 bFound = TRUE;
158 break;
159 }
160 }
161 if (!bFound)
162 return 0;
163 return (i + 1);
164 } // end of wxPalette::GetPixel
165
166 bool wxPalette::GetRGB(
167 int nIndex
168 , unsigned char* pRed
169 , unsigned char* pGreen
170 , unsigned char* pBlue
171 ) const
172 {
173 PULONG pualTable = NULL;
174 RGB2 vRGB;
175 ULONG ulNumEntries;
176
177 if (!m_refData)
178 return FALSE;
179
180 if (nIndex < 0 || nIndex > 255)
181 return FALSE;
182 //
183 // Get number of entries first
184 //
185 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
186 ,M_PALETTEDATA->m_hPS
187 ,0 // No options
188 ,0 // No start index
189 ,0 // Force return of number entries
190 ,NULL // No array
191 );
192
193 pualTable = new ULONG[ulNumEntries];
194
195 //
196 // Now get the entries
197 //
198 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
199 ,M_PALETTEDATA->m_hPS
200 ,0 // No options
201 ,0 // start at 0
202 ,ulNumEntries // Force return of number entries
203 ,pualTable // Palette entry array with RGB values
204 );
205
206 memcpy(&vRGB, &pualTable[nIndex], sizeof(RGB2));
207 *pBlue = vRGB.bBlue;
208 *pGreen = vRGB.bGreen;
209 *pRed = vRGB.bRed;
210 return TRUE;
211 } // end of wxPalette::GetRGB
212
213 void wxPalette::SetHPALETTE(
214 WXHPALETTE hPal
215 )
216 {
217 if ( !m_refData )
218 m_refData = new wxPaletteRefData;
219
220 M_PALETTEDATA->m_hPalette = hPal;
221 } // end of wxPalette::SetHPALETTE
222
223 void wxPalette::SetPS(
224 HPS hPS
225 )
226 {
227 if ( !m_refData )
228 m_refData = new wxPaletteRefData;
229
230 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette);
231 M_PALETTEDATA->m_hPS = hPS;
232 } // end of wxPalette::SetHPALETTE
233