]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/palette.cpp
use wx-style header and commets; fix indentation to be 4 spaces; move Doxygen comment...
[wxWidgets.git] / src / mac / carbon / palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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 #include "wx/wxprec.h"
13
14 #if wxUSE_PALETTE
15
16 #include "wx/palette.h"
17 #include "wx/colour.h"
18
19 IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
20
21 // ============================================================================
22 // wxPaletteRefData
23 // ============================================================================
24
25 class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData
26 {
27 public:
28 wxPaletteRefData();
29 wxPaletteRefData(const wxPaletteRefData& data);
30 virtual ~wxPaletteRefData();
31
32 virtual bool IsOk() const { return m_count > 0; }
33
34 protected:
35 wxColour* m_palette;
36 wxInt32 m_count;
37
38 friend class WXDLLIMPEXP_FWD_CORE wxPalette;
39
40 DECLARE_NO_ASSIGN_CLASS(wxPaletteRefData)
41 };
42
43 wxPaletteRefData::wxPaletteRefData()
44 {
45 m_palette = NULL;
46 m_count = 0;
47 }
48
49 wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData& data)
50 {
51 m_count = data.m_count;
52 m_palette = new wxColour[m_count];
53 for ( wxInt32 i = 0; i < m_count; i++ )
54 m_palette[i] = data.m_palette[i];
55 }
56
57 wxPaletteRefData::~wxPaletteRefData()
58 {
59 delete[] m_palette;
60 }
61
62 // ============================================================================
63 // wxPalette
64 // ============================================================================
65
66 wxPalette::wxPalette()
67 {
68 }
69
70 wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
71 {
72 Create(n, red, green, blue);
73 }
74
75 wxPalette::~wxPalette()
76 {
77 }
78
79 bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
80 {
81 UnRef();
82
83 m_refData = new wxPaletteRefData;
84
85 M_PALETTEDATA->m_count = n ;
86 M_PALETTEDATA->m_palette = new wxColour[n] ;
87
88 for ( int i = 0 ; i < n ; ++i)
89 {
90 M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ;
91 }
92
93 return false;
94 }
95
96 int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
97 {
98 if ( !m_refData )
99 return wxNOT_FOUND;
100
101 long bestdiff = 3 * 256 ;
102 long bestpos = 0 ;
103 long currentdiff ;
104
105 for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i )
106 {
107 const wxColour& col = M_PALETTEDATA->m_palette[i] ;
108 currentdiff = abs ( col.Red() - red ) + abs( col.Green() - green ) + abs ( col.Blue() - blue ) ;
109 if ( currentdiff < bestdiff )
110 {
111 bestdiff = currentdiff ;
112 bestpos = i ;
113 if ( bestdiff == 0 )
114 break ;
115 }
116 }
117
118 return bestpos;
119 }
120
121 bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
122 {
123 if ( !m_refData )
124 return false;
125
126 if (index < 0 || index >= M_PALETTEDATA->m_count)
127 return false;
128
129 const wxColour& col = M_PALETTEDATA->m_palette[index] ;
130 *red = col.Red() ;
131 *green = col.Green() ;
132 *blue = col.Blue() ;
133
134 return true;
135 }
136
137 int wxPalette::GetColoursCount() const
138 {
139 if (m_refData)
140 return M_PALETTEDATA->m_count;
141
142 return 0;
143 }
144
145 wxGDIRefData *wxPalette::CreateGDIRefData() const
146 {
147 return new wxPaletteRefData;
148 }
149
150 wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const
151 {
152 return new wxPaletteRefData(*wx_static_cast(const wxPaletteRefData *, data));
153 }
154
155 #endif // wxUSE_PALETTE