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