removed USE_SHARED_LIBRARY(IES)
[wxWidgets.git] / src / mac / 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 #ifdef __GNUG__
13 #pragma implementation "palette.h"
14 #endif
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 delete[] m_palette ;
34 }
35
36 wxPalette::wxPalette()
37 {
38 }
39
40 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
41 {
42 Create(n, red, green, blue);
43 }
44
45 wxPalette::~wxPalette()
46 {
47 }
48
49 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
50 {
51 UnRef();
52
53 m_refData = new wxPaletteRefData;
54
55 M_PALETTEDATA->m_count = n ;
56 M_PALETTEDATA->m_palette = new wxColour[n] ;
57
58 for ( int i = 0 ; i < n ; ++i)
59 {
60 M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ;
61 }
62
63 return FALSE;
64 }
65
66 int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
67 {
68 if ( !m_refData )
69 return -1;
70
71 long bestdiff = 3 * 256 ;
72 long bestpos = 0 ;
73 long currentdiff ;
74
75 for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i )
76 {
77 const wxColour& col = &M_PALETTEDATA->m_palette[i] ;
78 currentdiff = abs ( col.Red() - red ) + abs( col.Green() - green ) + abs ( col.Blue() - blue ) ;
79 if ( currentdiff < bestdiff )
80 {
81 bestdiff = currentdiff ;
82 bestpos = i ;
83 if ( bestdiff == 0 )
84 break ;
85 }
86 }
87
88 return bestpos;
89 }
90
91 bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
92 {
93 if ( !m_refData )
94 return FALSE;
95
96 if (index < 0 || index >= M_PALETTEDATA->m_count)
97 return FALSE;
98
99 const wxColour& col = &M_PALETTEDATA->m_palette[index] ;
100 *red = col.Red() ;
101 *green = col.Green() ;
102 *blue = col.Blue() ;
103
104 return TRUE;
105 }
106
107