]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #include "wx/palette.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 class wxPaletteRefData
: public wxObjectRefData
28 wxPaletteRefData(void);
29 ~wxPaletteRefData(void);
35 wxPaletteRefData::wxPaletteRefData()
41 wxPaletteRefData::~wxPaletteRefData()
46 //-----------------------------------------------------------------------------
48 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
50 IMPLEMENT_DYNAMIC_CLASS(wxPalette
,wxGDIObject
)
52 wxPalette::wxPalette()
57 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
59 Create(n
, red
, green
, blue
);
62 wxPalette::wxPalette(const wxPalette
& palette
)
67 wxPalette::~wxPalette()
71 wxPalette
& wxPalette::operator = (const wxPalette
& palette
)
73 if (*this == palette
) return (*this);
78 bool wxPalette::operator == (const wxPalette
& palette
) const
80 return m_refData
== palette
.m_refData
;
83 bool wxPalette::operator != (const wxPalette
& palette
) const
85 return m_refData
!= palette
.m_refData
;
88 bool wxPalette::Ok(void) const
90 return (m_refData
!= NULL
);
93 bool wxPalette::Create(int n
,
94 const unsigned char *red
,
95 const unsigned char *green
,
96 const unsigned char *blue
)
99 m_refData
= new wxPaletteRefData();
101 M_PALETTEDATA
->m_count
= n
;
102 M_PALETTEDATA
->m_entries
= new palette_t
[n
];
104 palette_t
*e
= M_PALETTEDATA
->m_entries
;
105 for (int i
= 0; i
< n
; i
++, e
++)
116 int wxPalette::GetPixel(const unsigned char red
,
117 const unsigned char green
,
118 const unsigned char blue
) const
120 if (!m_refData
) return FALSE
;
123 double d
,distance
= 1000.0; // max. dist is 256
125 palette_t
*e
= M_PALETTEDATA
->m_entries
;
126 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
128 if ((d
= 0.299 * abs(red
- e
->red
) +
129 0.587 * abs(green
- e
->green
) +
130 0.114 * abs(blue
- e
->blue
)) < distance
) {
138 bool wxPalette::GetRGB(int pixel
,
140 unsigned char *green
,
141 unsigned char *blue
) const
143 if (!m_refData
) return FALSE
;
144 if (pixel
>= M_PALETTEDATA
->m_count
) return FALSE
;
146 palette_t
& p
= M_PALETTEDATA
->m_entries
[pixel
];
147 if (red
) *red
= p
.red
;
148 if (green
) *green
= p
.green
;
149 if (blue
) *blue
= p
.blue
;
153 int wxPalette::GetColoursCount() const
155 wxCHECK_MSG( Ok(), 0, wxT("invalid palette") );
156 return M_PALETTEDATA
->m_count
;
159 palette_t
*wxPalette::GetMGLpalette_t() const
161 wxCHECK_MSG( Ok(), NULL
, wxT("invalid palette") );
162 return M_PALETTEDATA
->m_entries
;