]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/palette.cpp
create stock GDI objects on demand; use const with GDI objects appropriately (patch...
[wxWidgets.git] / src / mac / classic / palette.cpp
CommitLineData
2646f485 1/////////////////////////////////////////////////////////////////////////////
88ef3a57 2// Name: src/mac/classic/palette.cpp
2646f485
SC
3// Purpose: wxPalette
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
88ef3a57 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
2646f485
SC
12#include "wx/defs.h"
13
14#if wxUSE_PALETTE
15
16#include "wx/palette.h"
17
2646f485 18IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
2646f485
SC
19
20/*
21 * Palette
22 *
23 */
24
25wxPaletteRefData::wxPaletteRefData()
26{
27 m_palette = NULL ;
28 m_count = 0 ;
29}
30
31wxPaletteRefData::~wxPaletteRefData()
32{
33 if (m_palette != NULL) {
34 delete[] m_palette ;
35 m_palette = NULL;
36 }
37}
38
39wxPalette::wxPalette()
40{
41}
42
43wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
44{
45 Create(n, red, green, blue);
46}
47
48wxPalette::~wxPalette()
49{
50}
51
52bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
53{
54 UnRef();
88ef3a57 55
2646f485 56 m_refData = new wxPaletteRefData;
88ef3a57 57
2646f485
SC
58 M_PALETTEDATA->m_count = n ;
59 M_PALETTEDATA->m_palette = new wxColour[n] ;
88ef3a57 60
2646f485
SC
61 for ( int i = 0 ; i < n ; ++i)
62 {
63 M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ;
64 }
88ef3a57
WS
65
66 return false;
2646f485
SC
67}
68
88ef3a57 69int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
2646f485
SC
70{
71 if ( !m_refData )
88ef3a57
WS
72 return wxNOT_FOUND;
73
2646f485
SC
74 long bestdiff = 3 * 256 ;
75 long bestpos = 0 ;
76 long currentdiff ;
88ef3a57 77
2646f485
SC
78 for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i )
79 {
80 const wxColour& col = &M_PALETTEDATA->m_palette[i] ;
81 currentdiff = abs ( col.Red() - red ) + abs( col.Green() - green ) + abs ( col.Blue() - blue ) ;
82 if ( currentdiff < bestdiff )
83 {
84 bestdiff = currentdiff ;
85 bestpos = i ;
86 if ( bestdiff == 0 )
88ef3a57 87 break ;
2646f485
SC
88 }
89 }
88ef3a57 90
2646f485
SC
91 return bestpos;
92}
93
94bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
95{
96 if ( !m_refData )
88ef3a57
WS
97 return false;
98
2646f485 99 if (index < 0 || index >= M_PALETTEDATA->m_count)
88ef3a57
WS
100 return false;
101
2646f485
SC
102 const wxColour& col = &M_PALETTEDATA->m_palette[index] ;
103 *red = col.Red() ;
104 *green = col.Green() ;
105 *blue = col.Blue() ;
88ef3a57
WS
106
107 return true;
2646f485
SC
108}
109
110#endif
111// wxUSE_PALETTE