]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/palette.cpp
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(unsigned char red
,
118 unsigned char blue
) const
120 if (!m_refData
) return wxNOT_FOUND
;
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
) {
139 bool wxPalette::GetRGB(int pixel
,
141 unsigned char *green
,
142 unsigned char *blue
) const
144 if (!m_refData
) return false;
145 if (pixel
>= M_PALETTEDATA
->m_count
) return false;
147 palette_t
& p
= M_PALETTEDATA
->m_entries
[pixel
];
148 if (red
) *red
= p
.red
;
149 if (green
) *green
= p
.green
;
150 if (blue
) *blue
= p
.blue
;
154 int wxPalette::GetColoursCount() const
156 wxCHECK_MSG( Ok(), 0, wxT("invalid palette") );
157 return M_PALETTEDATA
->m_count
;
160 palette_t
*wxPalette::GetMGLpalette_t() const
162 wxCHECK_MSG( Ok(), NULL
, wxT("invalid palette") );
163 return M_PALETTEDATA
->m_entries
;