]>
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()
66 bool wxPalette::operator == (const wxPalette
& palette
) const
68 return m_refData
== palette
.m_refData
;
71 bool wxPalette::operator != (const wxPalette
& palette
) const
73 return m_refData
!= palette
.m_refData
;
76 bool wxPalette::Ok(void) const
78 return (m_refData
!= NULL
);
81 bool wxPalette::Create(int n
,
82 const unsigned char *red
,
83 const unsigned char *green
,
84 const unsigned char *blue
)
87 m_refData
= new wxPaletteRefData();
89 M_PALETTEDATA
->m_count
= n
;
90 M_PALETTEDATA
->m_entries
= new palette_t
[n
];
92 palette_t
*e
= M_PALETTEDATA
->m_entries
;
93 for (int i
= 0; i
< n
; i
++, e
++)
104 int wxPalette::GetPixel(unsigned char red
,
106 unsigned char blue
) const
108 if (!m_refData
) return wxNOT_FOUND
;
111 double d
, distance
= 1000.0; // max. dist is 256
113 palette_t
*e
= M_PALETTEDATA
->m_entries
;
114 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
116 if ((d
= 0.299 * abs(red
- e
->red
) +
117 0.587 * abs(green
- e
->green
) +
118 0.114 * abs(blue
- e
->blue
)) < distance
) {
127 bool wxPalette::GetRGB(int pixel
,
129 unsigned char *green
,
130 unsigned char *blue
) const
132 if (!m_refData
) return false;
133 if (pixel
>= M_PALETTEDATA
->m_count
) return false;
135 palette_t
& p
= M_PALETTEDATA
->m_entries
[pixel
];
136 if (red
) *red
= p
.red
;
137 if (green
) *green
= p
.green
;
138 if (blue
) *blue
= p
.blue
;
142 int wxPalette::GetColoursCount() const
144 wxCHECK_MSG( Ok(), 0, wxT("invalid palette") );
145 return M_PALETTEDATA
->m_count
;
148 palette_t
*wxPalette::GetMGLpalette_t() const
150 wxCHECK_MSG( Ok(), NULL
, wxT("invalid palette") );
151 return M_PALETTEDATA
->m_entries
;