]>
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 virtual ~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::Create(int n
,
67 const unsigned char *red
,
68 const unsigned char *green
,
69 const unsigned char *blue
)
72 m_refData
= new wxPaletteRefData();
74 M_PALETTEDATA
->m_count
= n
;
75 M_PALETTEDATA
->m_entries
= new palette_t
[n
];
77 palette_t
*e
= M_PALETTEDATA
->m_entries
;
78 for (int i
= 0; i
< n
; i
++, e
++)
89 int wxPalette::GetPixel(unsigned char red
,
91 unsigned char blue
) const
93 if (!m_refData
) return wxNOT_FOUND
;
96 double d
, distance
= 1000.0; // max. dist is 256
98 palette_t
*e
= M_PALETTEDATA
->m_entries
;
99 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
101 if ((d
= 0.299 * abs(red
- e
->red
) +
102 0.587 * abs(green
- e
->green
) +
103 0.114 * abs(blue
- e
->blue
)) < distance
) {
112 bool wxPalette::GetRGB(int pixel
,
114 unsigned char *green
,
115 unsigned char *blue
) const
117 if (!m_refData
) return false;
118 if (pixel
>= M_PALETTEDATA
->m_count
) return false;
120 palette_t
& p
= M_PALETTEDATA
->m_entries
[pixel
];
121 if (red
) *red
= p
.red
;
122 if (green
) *green
= p
.green
;
123 if (blue
) *blue
= p
.blue
;
127 int wxPalette::GetColoursCount() const
129 wxCHECK_MSG( Ok(), 0, wxT("invalid palette") );
130 return M_PALETTEDATA
->m_count
;
133 palette_t
*wxPalette::GetMGLpalette_t() const
135 wxCHECK_MSG( Ok(), NULL
, wxT("invalid palette") );
136 return M_PALETTEDATA
->m_entries
;