]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/palette.cpp
Include wx/menuitem.h according to precompiled headers of wx/wx.h (with other minor...
[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
SC
19
20/*
21 * Palette
22 *
23 */
24
25wxPaletteRefData::wxPaletteRefData()
26{
e40298d5
JS
27 m_palette = NULL ;
28 m_count = 0 ;
e9576ca5
SC
29}
30
31wxPaletteRefData::~wxPaletteRefData()
32{
f5bb2251
GD
33 if (m_palette != NULL) {
34 delete[] m_palette ;
35 m_palette = NULL;
36 }
e9576ca5
SC
37}
38
39wxPalette::wxPalette()
40{
41}
42
43wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
44{
45 Create(n, red, green, blue);
46}
47
48wxPalette::~wxPalette()
49{
50}
51
52bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
53{
e40298d5 54 UnRef();
88ef3a57 55
e40298d5 56 m_refData = new wxPaletteRefData;
88ef3a57 57
e40298d5
JS
58 M_PALETTEDATA->m_count = n ;
59 M_PALETTEDATA->m_palette = new wxColour[n] ;
88ef3a57 60
e40298d5
JS
61 for ( int i = 0 ; i < n ; ++i)
62 {
63 M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ;
64 }
88ef3a57
WS
65
66 return false;
e9576ca5
SC
67}
68
88ef3a57 69int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
e9576ca5
SC
70{
71 if ( !m_refData )
88ef3a57
WS
72 return wxNOT_FOUND;
73
519cb848
SC
74 long bestdiff = 3 * 256 ;
75 long bestpos = 0 ;
76 long currentdiff ;
88ef3a57 77
519cb848
SC
78 for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i )
79 {
4b524c27 80 const wxColour& col = M_PALETTEDATA->m_palette[i] ;
e40298d5
JS
81 currentdiff = abs ( col.Red() - red ) + abs( col.Green() - green ) + abs ( col.Blue() - blue ) ;
82 if ( currentdiff < bestdiff )
83 {
84 bestdiff = currentdiff ;
85 bestpos = i ;
86 if ( bestdiff == 0 )
88ef3a57 87 break ;
e40298d5 88 }
519cb848 89 }
88ef3a57 90
519cb848 91 return bestpos;
e9576ca5
SC
92}
93
94bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
95{
96 if ( !m_refData )
88ef3a57
WS
97 return false;
98
519cb848 99 if (index < 0 || index >= M_PALETTEDATA->m_count)
88ef3a57
WS
100 return false;
101
4b524c27 102 const wxColour& col = M_PALETTEDATA->m_palette[index] ;
519cb848
SC
103 *red = col.Red() ;
104 *green = col.Green() ;
105 *blue = col.Blue() ;
88ef3a57
WS
106
107 return true;
e9576ca5
SC
108}
109
fedad417 110#endif
e40298d5 111// wxUSE_PALETTE