]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/palette.cpp
63540e9603ed0350627971675049ed48db43e9ec
[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 #ifdef __GNUG__
13 #pragma implementation "palette.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #if wxUSE_PALETTE
19
20 #include "wx/palette.h"
21
22 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
23
24 /*
25 * Palette
26 *
27 */
28
29 wxPaletteRefData::wxPaletteRefData()
30 {
31 m_palette = NULL ;
32 m_count = 0 ;
33 }
34
35 wxPaletteRefData::~wxPaletteRefData()
36 {
37 if (m_palette != NULL) {
38 delete[] m_palette ;
39 m_palette = NULL;
40 }
41 }
42
43 wxPalette::wxPalette()
44 {
45 }
46
47 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
48 {
49 Create(n, red, green, blue);
50 }
51
52 wxPalette::~wxPalette()
53 {
54 }
55
56 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
57 {
58 UnRef();
59
60 m_refData = new wxPaletteRefData;
61
62 M_PALETTEDATA->m_count = n ;
63 M_PALETTEDATA->m_palette = new wxColour[n] ;
64
65 for ( int i = 0 ; i < n ; ++i)
66 {
67 M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ;
68 }
69
70 return FALSE;
71 }
72
73 int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
74 {
75 if ( !m_refData )
76 return -1;
77
78 long bestdiff = 3 * 256 ;
79 long bestpos = 0 ;
80 long currentdiff ;
81
82 for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i )
83 {
84 const wxColour& col = &M_PALETTEDATA->m_palette[i] ;
85 currentdiff = abs ( col.Red() - red ) + abs( col.Green() - green ) + abs ( col.Blue() - blue ) ;
86 if ( currentdiff < bestdiff )
87 {
88 bestdiff = currentdiff ;
89 bestpos = i ;
90 if ( bestdiff == 0 )
91 break ;
92 }
93 }
94
95 return bestpos;
96 }
97
98 bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
99 {
100 if ( !m_refData )
101 return FALSE;
102
103 if (index < 0 || index >= M_PALETTEDATA->m_count)
104 return FALSE;
105
106 const wxColour& col = &M_PALETTEDATA->m_palette[index] ;
107 *red = col.Red() ;
108 *green = col.Green() ;
109 *blue = col.Blue() ;
110
111 return TRUE;
112 }
113
114 #endif
115 // wxUSE_PALETTE
116