]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/paletteg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/paletteg.cpp
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "paletteg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
25 #include "wx/palette.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
33 unsigned char red
, green
, blue
;
36 class wxPaletteRefData
: public wxObjectRefData
40 wxPaletteRefData(void);
41 ~wxPaletteRefData(void);
44 wxPaletteEntry
*m_entries
;
47 wxPaletteRefData::wxPaletteRefData()
53 wxPaletteRefData::~wxPaletteRefData()
58 //-----------------------------------------------------------------------------
60 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
62 IMPLEMENT_DYNAMIC_CLASS(wxPalette
,wxGDIObject
)
64 wxPalette::wxPalette()
69 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
71 Create(n
, red
, green
, blue
);
74 wxPalette::wxPalette(const wxPalette
& palette
)
80 wxPalette::~wxPalette()
84 wxPalette
& wxPalette::operator = (const wxPalette
& palette
)
86 if (*this == palette
) return (*this);
91 bool wxPalette::operator == (const wxPalette
& palette
)
93 return m_refData
== palette
.m_refData
;
96 bool wxPalette::operator != (const wxPalette
& palette
)
98 return m_refData
!= palette
.m_refData
;
101 bool wxPalette::Ok(void) const
103 return (m_refData
!= NULL
);
106 bool wxPalette::Create(int n
,
107 const unsigned char *red
,
108 const unsigned char *green
,
109 const unsigned char *blue
)
112 m_refData
= new wxPaletteRefData();
114 M_PALETTEDATA
->m_count
= n
;
115 M_PALETTEDATA
->m_entries
= new wxPaletteEntry
[n
];
117 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
118 for (int i
= 0; i
< n
; i
++, e
++)
128 int wxPalette::GetPixel( const unsigned char red
,
129 const unsigned char green
,
130 const unsigned char blue
) const
132 if (!m_refData
) return FALSE
;
135 double d
,distance
= 1000.0; // max. dist is 256
137 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
138 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
140 if ((d
= 0.299 * abs(red
- e
->red
) +
141 0.587 * abs(green
- e
->green
) +
142 0.114 * abs(blue
- e
->blue
)) < distance
) {
150 bool wxPalette::GetRGB(int pixel
,
152 unsigned char *green
,
153 unsigned char *blue
) const
155 if (!m_refData
) return FALSE
;
156 if (pixel
>= M_PALETTEDATA
->m_count
) return FALSE
;
158 wxPaletteEntry
& p
= M_PALETTEDATA
->m_entries
[pixel
];
159 if (red
) *red
= p
.red
;
160 if (green
) *green
= p
.green
;
161 if (blue
) *blue
= p
.blue
;
165 #endif // wxUSE_PALETTE