]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/palette.cpp
Use IsOk() instead of Ok()
[wxWidgets.git] / src / mac / carbon / palette.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
88ef3a57 2// Name: src/mac/carbon/palette.cpp
e9576ca5 3// Purpose: wxPalette
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
88ef3a57 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878 12#include "wx/wxprec.h"
fedad417
GD
13
14#if wxUSE_PALETTE
15
e9576ca5
SC
16#include "wx/palette.h"
17
e9576ca5 18IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
e9576ca5 19
8f884a0d
VZ
20// ============================================================================
21// wxPaletteRefData
22// ============================================================================
23
24class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData
25{
26public:
27 wxPaletteRefData();
28 wxPaletteRefData(const wxPaletteRefData& data);
29 virtual ~wxPaletteRefData();
30
31 virtual bool IsOk() const { return m_count > 0; }
32
33protected:
34 wxColour* m_palette;
35 wxInt32 m_count;
36
37 friend class WXDLLIMPEXP_FWD_CORE wxPalette;
38
39 DECLARE_NO_ASSIGN_CLASS(wxPaletteRefData)
40};
e9576ca5
SC
41
42wxPaletteRefData::wxPaletteRefData()
43{
8f884a0d
VZ
44 m_palette = NULL;
45 m_count = 0;
46}
47
48wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData& data)
49{
50 m_count = data.m_count;
51 m_palette = new wxColour[m_count];
52 for ( wxInt32 i = 0; i < m_count; i++ )
53 m_palette[i] = data.m_palette[i];
e9576ca5
SC
54}
55
56wxPaletteRefData::~wxPaletteRefData()
57{
8f884a0d 58 delete[] m_palette;
e9576ca5
SC
59}
60
8f884a0d
VZ
61// ============================================================================
62// wxPalette
63// ============================================================================
64
e9576ca5
SC
65wxPalette::wxPalette()
66{
67}
68
69wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
70{
71 Create(n, red, green, blue);
72}
73
74wxPalette::~wxPalette()
75{
76}
77
78bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
79{
e40298d5 80 UnRef();
88ef3a57 81
e40298d5 82 m_refData = new wxPaletteRefData;
88ef3a57 83
e40298d5
JS
84 M_PALETTEDATA->m_count = n ;
85 M_PALETTEDATA->m_palette = new wxColour[n] ;
88ef3a57 86
e40298d5
JS
87 for ( int i = 0 ; i < n ; ++i)
88 {
89 M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ;
90 }
88ef3a57
WS
91
92 return false;
e9576ca5
SC
93}
94
88ef3a57 95int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
e9576ca5
SC
96{
97 if ( !m_refData )
88ef3a57
WS
98 return wxNOT_FOUND;
99
519cb848
SC
100 long bestdiff = 3 * 256 ;
101 long bestpos = 0 ;
102 long currentdiff ;
88ef3a57 103
519cb848
SC
104 for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i )
105 {
4b524c27 106 const wxColour& col = M_PALETTEDATA->m_palette[i] ;
e40298d5
JS
107 currentdiff = abs ( col.Red() - red ) + abs( col.Green() - green ) + abs ( col.Blue() - blue ) ;
108 if ( currentdiff < bestdiff )
109 {
110 bestdiff = currentdiff ;
111 bestpos = i ;
112 if ( bestdiff == 0 )
88ef3a57 113 break ;
e40298d5 114 }
519cb848 115 }
88ef3a57 116
519cb848 117 return bestpos;
e9576ca5
SC
118}
119
120bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
121{
122 if ( !m_refData )
88ef3a57
WS
123 return false;
124
519cb848 125 if (index < 0 || index >= M_PALETTEDATA->m_count)
88ef3a57
WS
126 return false;
127
4b524c27 128 const wxColour& col = M_PALETTEDATA->m_palette[index] ;
519cb848
SC
129 *red = col.Red() ;
130 *green = col.Green() ;
131 *blue = col.Blue() ;
88ef3a57
WS
132
133 return true;
e9576ca5
SC
134}
135
df816ad9
RR
136int wxPalette::GetColoursCount() const
137{
138 if (m_refData)
139 return M_PALETTEDATA->m_count;
8f884a0d
VZ
140
141 return 0;
142}
143
144wxGDIRefData *wxPalette::CreateGDIRefData() const
145{
146 return new wxPaletteRefData;
df816ad9
RR
147}
148
8f884a0d
VZ
149wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
150{
151 return new wxPaletteRefData(*wx_static_cast(const wxPaletteRefData *, data));
152}
df816ad9 153
8f884a0d 154#endif // wxUSE_PALETTE