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