]>
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, Julian Smart and Markus Holzem
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
)
79 wxPalette::~wxPalette()
83 wxPalette
& wxPalette::operator = (const wxPalette
& palette
)
85 if (*this == palette
) return (*this);
90 bool wxPalette::operator == (const wxPalette
& palette
)
92 return m_refData
== palette
.m_refData
;
95 bool wxPalette::operator != (const wxPalette
& palette
)
97 return m_refData
!= palette
.m_refData
;
100 bool wxPalette::Ok(void) const
102 return (m_refData
!= NULL
);
105 bool wxPalette::Create(int n
,
106 const unsigned char *red
,
107 const unsigned char *green
,
108 const unsigned char *blue
)
111 m_refData
= new wxPaletteRefData();
113 M_PALETTEDATA
->m_count
= n
;
114 M_PALETTEDATA
->m_entries
= new wxPaletteEntry
[n
];
116 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
117 for (int i
= 0; i
< n
; i
++, e
++)
127 int wxPalette::GetPixel( const unsigned char red
,
128 const unsigned char green
,
129 const unsigned char blue
) const
131 if (!m_refData
) return FALSE
;
134 double d
,distance
= 1000.0; // max. dist is 256
136 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
137 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
139 if ((d
= 0.299 * abs(red
- e
->red
) +
140 0.587 * abs(green
- e
->green
) +
141 0.114 * abs(blue
- e
->blue
)) < distance
) {
149 bool wxPalette::GetRGB(int pixel
,
151 unsigned char *green
,
152 unsigned char *blue
) const
154 if (!m_refData
) return FALSE
;
155 if (pixel
>= M_PALETTEDATA
->m_count
) return FALSE
;
157 wxPaletteEntry
& p
= M_PALETTEDATA
->m_entries
[pixel
];
158 if (red
) *red
= p
.red
;
159 if (green
) *green
= p
.green
;
160 if (blue
) *blue
= p
.blue
;
164 #endif // wxUSE_PALETTE