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