]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/palette.cpp
Added wxFileName::GetModificationTime()
[wxWidgets.git] / src / gtk / palette.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: palette.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "palette.h"
14#endif
15
16#include "wx/palette.h"
17
5e7e9e1b 18#include <gdk/gdk.h>
83624f79 19
c801d85f
KB
20//-----------------------------------------------------------------------------
21// wxPalette
22//-----------------------------------------------------------------------------
23
cb332bc6
VS
24struct wxPaletteEntry
25{
26 unsigned char red, green, blue;
27};
28
c801d85f
KB
29class wxPaletteRefData: public wxObjectRefData
30{
31 public:
8bbe427f 32
c801d85f
KB
33 wxPaletteRefData(void);
34 ~wxPaletteRefData(void);
8bbe427f 35
cb332bc6
VS
36 int m_count;
37 wxPaletteEntry *m_entries;
38#if 0
c801d85f 39 GdkColormap *m_colormap;
cb332bc6 40#endif
c801d85f
KB
41};
42
8bbe427f 43wxPaletteRefData::wxPaletteRefData()
c801d85f 44{
cb332bc6
VS
45 m_count = 0;
46 m_entries = NULL;
47#if 0
8fc613f1 48 m_colormap = (GdkColormap *) NULL;
cb332bc6 49#endif
8fc613f1 50}
c801d85f 51
8bbe427f 52wxPaletteRefData::~wxPaletteRefData()
c801d85f 53{
cb332bc6
VS
54 delete[] m_entries;
55#if 0
8fc613f1 56 if (m_colormap) gdk_colormap_unref( m_colormap );
cb332bc6 57#endif
8fc613f1 58}
c801d85f
KB
59
60//-----------------------------------------------------------------------------
61
62#define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
63
64IMPLEMENT_DYNAMIC_CLASS(wxPalette,wxGDIObject)
65
8bbe427f 66wxPalette::wxPalette()
c801d85f 67{
cb332bc6 68 m_refData = NULL;
8fc613f1 69}
c801d85f 70
cb332bc6 71wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue)
c801d85f 72{
cb332bc6 73 Create(n, red, green, blue);
8fc613f1 74}
c801d85f 75
cb332bc6 76wxPalette::wxPalette(const wxPalette& palette)
c801d85f 77{
cb332bc6 78 Ref(palette);
8fc613f1 79}
c801d85f 80
8bbe427f 81wxPalette::~wxPalette()
c801d85f 82{
8fc613f1 83}
c801d85f 84
cb332bc6 85wxPalette& wxPalette::operator = (const wxPalette& palette)
c801d85f 86{
8fc613f1 87 if (*this == palette) return (*this);
cb332bc6 88 Ref(palette);
8fc613f1
RR
89 return *this;
90}
c801d85f 91
cb332bc6 92bool wxPalette::operator == (const wxPalette& palette)
c801d85f 93{
8fc613f1
RR
94 return m_refData == palette.m_refData;
95}
c801d85f 96
cb332bc6 97bool wxPalette::operator != (const wxPalette& palette)
c801d85f 98{
8fc613f1
RR
99 return m_refData != palette.m_refData;
100}
c801d85f
KB
101
102bool wxPalette::Ok(void) const
103{
8fc613f1
RR
104 return (m_refData != NULL);
105}
c801d85f 106
cb332bc6
VS
107bool wxPalette::Create(int n,
108 const unsigned char *red,
109 const unsigned char *green,
110 const unsigned char *blue)
c801d85f 111{
cb332bc6
VS
112 UnRef();
113 m_refData = new wxPaletteRefData();
114
115 M_PALETTEDATA->m_count = n;
116 M_PALETTEDATA->m_entries = new wxPaletteEntry[n];
117
118 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
119 for (int i = 0; i < n; i++, e++)
120 {
121 e->red = red[i];
122 e->green = green[i];
123 e->blue = blue[i];
124 }
125
126 return TRUE;
8fc613f1 127}
c801d85f 128
cb332bc6
VS
129int wxPalette::GetPixel( const unsigned char red,
130 const unsigned char green,
131 const unsigned char blue ) const
c801d85f 132{
cb332bc6
VS
133 if (!m_refData) return FALSE;
134
135 int closest = 0;
136 double d,distance = 1000.0; // max. dist is 256
137
138 wxPaletteEntry *e = M_PALETTEDATA->m_entries;
139 for (int i = 0; i < M_PALETTEDATA->m_count; i++, e++)
140 {
141 if ((d = 0.299 * abs(red - e->red) +
142 0.587 * abs(green - e->green) +
143 0.114 * abs(blue - e->blue)) < distance) {
144 distance = d;
145 closest = i;
146 }
147 }
148 return closest;
8fc613f1 149}
c801d85f 150
cb332bc6
VS
151bool wxPalette::GetRGB(int pixel,
152 unsigned char *red,
153 unsigned char *green,
154 unsigned char *blue) const
c801d85f 155{
cb332bc6
VS
156 if (!m_refData) return FALSE;
157 if (pixel >= M_PALETTEDATA->m_count) return FALSE;
158
159 wxPaletteEntry& p = M_PALETTEDATA->m_entries[pixel];
160 if (red) *red = p.red;
161 if (green) *green = p.green;
162 if (blue) *blue = p.blue;
163 return TRUE;
8fc613f1 164}
c801d85f 165
cb332bc6 166