]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/palette.cpp
Prevent crash in wxString::Mid under STL builds (fixes #1231725).
[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 #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 if (m_palette != NULL) {
40 delete[] m_palette ;
41 m_palette = NULL;
42 }
43 }
44
45 wxPalette::wxPalette()
46 {
47 }
48
49 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
50 {
51 Create(n, red, green, blue);
52 }
53
54 wxPalette::~wxPalette()
55 {
56 }
57
58 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
59 {
60 UnRef();
61
62 m_refData = new wxPaletteRefData;
63
64 M_PALETTEDATA->m_count = n ;
65 M_PALETTEDATA->m_palette = new wxColour[n] ;
66
67 for ( int i = 0 ; i < n ; ++i)
68 {
69 M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ;
70 }
71
72 return FALSE;
73 }
74
75 int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
76 {
77 if ( !m_refData )
78 return -1;
79
80 long bestdiff = 3 * 256 ;
81 long bestpos = 0 ;
82 long currentdiff ;
83
84 for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i )
85 {
86 const wxColour& col = &M_PALETTEDATA->m_palette[i] ;
87 currentdiff = abs ( col.Red() - red ) + abs( col.Green() - green ) + abs ( col.Blue() - blue ) ;
88 if ( currentdiff < bestdiff )
89 {
90 bestdiff = currentdiff ;
91 bestpos = i ;
92 if ( bestdiff == 0 )
93 break ;
94 }
95 }
96
97 return bestpos;
98 }
99
100 bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
101 {
102 if ( !m_refData )
103 return FALSE;
104
105 if (index < 0 || index >= M_PALETTEDATA->m_count)
106 return FALSE;
107
108 const wxColour& col = &M_PALETTEDATA->m_palette[index] ;
109 *red = col.Red() ;
110 *green = col.Green() ;
111 *blue = col.Blue() ;
112
113 return TRUE;
114 }
115
116 #endif
117 // wxUSE_PALETTE
118