]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/palette.cpp
added wxPowerEvent; moved power functions stubs to common/powercmn.cpp
[wxWidgets.git] / src / mac / classic / palette.cpp
CommitLineData
2646f485 1/////////////////////////////////////////////////////////////////////////////
88ef3a57 2// Name: src/mac/classic/palette.cpp
2646f485
SC
3// Purpose: wxPalette
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
88ef3a57 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
18f3decb
WS
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
2646f485
SC
17
18#if wxUSE_PALETTE
19
20#include "wx/palette.h"
21
2646f485 22IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject)
2646f485
SC
23
24/*
25 * Palette
26 *
27 */
28
29wxPaletteRefData::wxPaletteRefData()
30{
31 m_palette = NULL ;
32 m_count = 0 ;
33}
34
35wxPaletteRefData::~wxPaletteRefData()
36{
37 if (m_palette != NULL) {
38 delete[] m_palette ;
39 m_palette = NULL;
40 }
41}
42
43wxPalette::wxPalette()
44{
45}
46
47wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
48{
49 Create(n, red, green, blue);
50}
51
52wxPalette::~wxPalette()
53{
54}
55
56bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
57{
58 UnRef();
88ef3a57 59
2646f485 60 m_refData = new wxPaletteRefData;
88ef3a57 61
2646f485
SC
62 M_PALETTEDATA->m_count = n ;
63 M_PALETTEDATA->m_palette = new wxColour[n] ;
88ef3a57 64
2646f485
SC
65 for ( int i = 0 ; i < n ; ++i)
66 {
67 M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ;
68 }
88ef3a57
WS
69
70 return false;
2646f485
SC
71}
72
88ef3a57 73int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const
2646f485
SC
74{
75 if ( !m_refData )
88ef3a57
WS
76 return wxNOT_FOUND;
77
2646f485
SC
78 long bestdiff = 3 * 256 ;
79 long bestpos = 0 ;
80 long currentdiff ;
88ef3a57 81
2646f485
SC
82 for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i )
83 {
84 const wxColour& col = &M_PALETTEDATA->m_palette[i] ;
85 currentdiff = abs ( col.Red() - red ) + abs( col.Green() - green ) + abs ( col.Blue() - blue ) ;
86 if ( currentdiff < bestdiff )
87 {
88 bestdiff = currentdiff ;
89 bestpos = i ;
90 if ( bestdiff == 0 )
88ef3a57 91 break ;
2646f485
SC
92 }
93 }
88ef3a57 94
2646f485
SC
95 return bestpos;
96}
97
98bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const
99{
100 if ( !m_refData )
88ef3a57
WS
101 return false;
102
2646f485 103 if (index < 0 || index >= M_PALETTEDATA->m_count)
88ef3a57
WS
104 return false;
105
2646f485
SC
106 const wxColour& col = &M_PALETTEDATA->m_palette[index] ;
107 *red = col.Red() ;
108 *green = col.Green() ;
109 *blue = col.Blue() ;
88ef3a57
WS
110
111 return true;
2646f485
SC
112}
113
114#endif
115// wxUSE_PALETTE