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