]> git.saurik.com Git - wxWidgets.git/blob - src/os2/palette.cpp
no changes
[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 int i;
154 for (i = 0; i < ulNumEntries; i++)
155 {
156 if (pualTable[i] == ulRGB)
157 {
158 bFound = TRUE;
159 break;
160 }
161 }
162 if (!bFound)
163 return 0;
164 return (i + 1);
165 } // end of wxPalette::GetPixel
166
167 bool wxPalette::GetRGB(
168 int nIndex
169 , unsigned char* pRed
170 , unsigned char* pGreen
171 , unsigned char* pBlue
172 ) const
173 {
174 PULONG pualTable = NULL;
175 RGB2 vRGB;
176 ULONG ulNumEntries;
177
178 if (!m_refData)
179 return FALSE;
180
181 if (nIndex < 0 || nIndex > 255)
182 return FALSE;
183 //
184 // Get number of entries first
185 //
186 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
187 ,M_PALETTEDATA->m_hPS
188 ,0 // No options
189 ,0 // No start index
190 ,0 // Force return of number entries
191 ,NULL // No array
192 );
193
194 pualTable = new ULONG[ulNumEntries];
195
196 //
197 // Now get the entries
198 //
199 ulNumEntries = ::GpiQueryPaletteInfo( M_PALETTEDATA->m_hPalette
200 ,M_PALETTEDATA->m_hPS
201 ,0 // No options
202 ,0 // start at 0
203 ,ulNumEntries // Force return of number entries
204 ,pualTable // Palette entry array with RGB values
205 );
206
207 memcpy(&vRGB, &pualTable[nIndex], sizeof(RGB2));
208 *pBlue = vRGB.bBlue;
209 *pGreen = vRGB.bGreen;
210 *pRed = vRGB.bRed;
211 return TRUE;
212 } // end of wxPalette::GetRGB
213
214 void wxPalette::SetHPALETTE(
215 WXHPALETTE hPal
216 )
217 {
218 if ( !m_refData )
219 m_refData = new wxPaletteRefData;
220
221 M_PALETTEDATA->m_hPalette = hPal;
222 } // end of wxPalette::SetHPALETTE
223
224 void wxPalette::SetPS(
225 HPS hPS
226 )
227 {
228 if ( !m_refData )
229 m_refData = new wxPaletteRefData;
230
231 ::GpiSelectPalette(M_PALETTEDATA->m_hPS, M_PALETTEDATA->m_hPalette);
232 M_PALETTEDATA->m_hPS = hPS;
233 } // end of wxPalette::SetHPALETTE
234