]> git.saurik.com Git - wxWidgets.git/blame - src/mac/palette.cpp
corrections for buffers
[wxWidgets.git] / src / mac / palette.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: palette.cpp
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
e40298d5 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "palette.h"
14#endif
15
fedad417
GD
16#include "wx/defs.h"
17
18#if wxUSE_PALETTE
19
e9576ca5
SC
20#include "wx/palette.h"
21
2f1ae414 22#if !USE_SHARED_LIBRARIES
e9576ca5 23IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
2f1ae414 24#endif
e9576ca5
SC
25
26/*
27 * Palette
28 *
29 */
30
31wxPaletteRefData::wxPaletteRefData()
32{
e40298d5
JS
33 m_palette = NULL ;
34 m_count = 0 ;
e9576ca5
SC
35}
36
37wxPaletteRefData::~wxPaletteRefData()
38{
f5bb2251
GD
39 if (m_palette != NULL) {
40 delete[] m_palette ;
41 m_palette = NULL;
42 }
e9576ca5
SC
43}
44
45wxPalette::wxPalette()
46{
47}
48
49wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
50{
51 Create(n, red, green, blue);
52}
53
54wxPalette::~wxPalette()
55{
56}
57
58bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
59{
e40298d5
JS
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;
e9576ca5
SC
73}
74
75int wxPalette::GetPixel(const unsigned char red, const unsigned char green, const unsigned char blue) const
76{
77 if ( !m_refData )
e40298d5
JS
78 return -1;
79
519cb848
SC
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 {
e40298d5
JS
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 }
519cb848
SC
95 }
96
97 return bestpos;
e9576ca5
SC
98}
99
100bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
101{
102 if ( !m_refData )
e40298d5
JS
103 return FALSE;
104
519cb848 105 if (index < 0 || index >= M_PALETTEDATA->m_count)
e9576ca5 106 return FALSE;
e40298d5 107
519cb848
SC
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;
e9576ca5
SC
114}
115
fedad417 116#endif
e40298d5 117// wxUSE_PALETTE
e9576ca5 118