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