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