1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Vaclav Slavik
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "palette.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/palette.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 class wxPaletteRefData
: public wxObjectRefData
33 wxPaletteRefData(void);
34 ~wxPaletteRefData(void);
40 wxPaletteRefData::wxPaletteRefData()
46 wxPaletteRefData::~wxPaletteRefData()
51 //-----------------------------------------------------------------------------
53 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
55 IMPLEMENT_DYNAMIC_CLASS(wxPalette
,wxGDIObject
)
57 wxPalette::wxPalette()
62 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
64 Create(n
, red
, green
, blue
);
67 wxPalette::wxPalette(const wxPalette
& palette
)
72 wxPalette::~wxPalette()
76 wxPalette
& wxPalette::operator = (const wxPalette
& palette
)
78 if (*this == palette
) return (*this);
83 bool wxPalette::operator == (const wxPalette
& palette
) const
85 return m_refData
== palette
.m_refData
;
88 bool wxPalette::operator != (const wxPalette
& palette
) const
90 return m_refData
!= palette
.m_refData
;
93 bool wxPalette::Ok(void) const
95 return (m_refData
!= NULL
);
98 bool wxPalette::Create(int n
,
99 const unsigned char *red
,
100 const unsigned char *green
,
101 const unsigned char *blue
)
104 m_refData
= new wxPaletteRefData();
106 M_PALETTEDATA
->m_count
= n
;
107 M_PALETTEDATA
->m_entries
= new palette_t
[n
];
109 palette_t
*e
= M_PALETTEDATA
->m_entries
;
110 for (int i
= 0; i
< n
; i
++, e
++)
121 int wxPalette::GetPixel(const unsigned char red
,
122 const unsigned char green
,
123 const unsigned char blue
) const
125 if (!m_refData
) return FALSE
;
128 double d
,distance
= 1000.0; // max. dist is 256
130 palette_t
*e
= M_PALETTEDATA
->m_entries
;
131 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
133 if ((d
= 0.299 * abs(red
- e
->red
) +
134 0.587 * abs(green
- e
->green
) +
135 0.114 * abs(blue
- e
->blue
)) < distance
) {
143 bool wxPalette::GetRGB(int pixel
,
145 unsigned char *green
,
146 unsigned char *blue
) const
148 if (!m_refData
) return FALSE
;
149 if (pixel
>= M_PALETTEDATA
->m_count
) return FALSE
;
151 palette_t
& p
= M_PALETTEDATA
->m_entries
[pixel
];
152 if (red
) *red
= p
.red
;
153 if (green
) *green
= p
.green
;
154 if (blue
) *blue
= p
.blue
;
158 int wxPalette::GetColoursCount() const
160 wxCHECK_MSG( Ok(), 0, wxT("invalid palette") );
161 return M_PALETTEDATA
->m_count
;
164 palette_t
*wxPalette::GetMGLpalette_t() const
166 wxCHECK_MSG( Ok(), NULL
, wxT("invalid palette") );
167 return M_PALETTEDATA
->m_entries
;