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