]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/paletteg.cpp
1 /////////////////////////////////////////////////////////////////////////////
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 #include "wx/palette.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
25 unsigned char red
, green
, blue
;
28 class wxPaletteRefData
: public wxObjectRefData
32 wxPaletteRefData(void);
33 ~wxPaletteRefData(void);
36 wxPaletteEntry
*m_entries
;
39 wxPaletteRefData::wxPaletteRefData()
45 wxPaletteRefData::~wxPaletteRefData()
50 //-----------------------------------------------------------------------------
52 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
54 IMPLEMENT_DYNAMIC_CLASS(wxPalette
,wxGDIObject
)
56 wxPalette::wxPalette()
61 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
63 Create(n
, red
, green
, blue
);
66 wxPalette::wxPalette(const wxPalette
& palette
)
71 wxPalette::~wxPalette()
75 wxPalette
& wxPalette::operator = (const wxPalette
& palette
)
77 if (*this == palette
) return (*this);
82 bool wxPalette::operator == (const wxPalette
& palette
)
84 return m_refData
== palette
.m_refData
;
87 bool wxPalette::operator != (const wxPalette
& palette
)
89 return m_refData
!= palette
.m_refData
;
92 bool wxPalette::Ok(void) const
94 return (m_refData
!= NULL
);
97 bool wxPalette::Create(int n
,
98 const unsigned char *red
,
99 const unsigned char *green
,
100 const unsigned char *blue
)
103 m_refData
= new wxPaletteRefData();
105 M_PALETTEDATA
->m_count
= n
;
106 M_PALETTEDATA
->m_entries
= new wxPaletteEntry
[n
];
108 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
109 for (int i
= 0; i
< n
; i
++, e
++)
119 int wxPalette::GetPixel( const unsigned char red
,
120 const unsigned char green
,
121 const unsigned char blue
) const
123 if (!m_refData
) return FALSE
;
126 double d
,distance
= 1000.0; // max. dist is 256
128 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
129 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
131 if ((d
= 0.299 * abs(red
- e
->red
) +
132 0.587 * abs(green
- e
->green
) +
133 0.114 * abs(blue
- e
->blue
)) < distance
) {
141 bool wxPalette::GetRGB(int pixel
,
143 unsigned char *green
,
144 unsigned char *blue
) const
146 if (!m_refData
) return FALSE
;
147 if (pixel
>= M_PALETTEDATA
->m_count
) return FALSE
;
149 wxPaletteEntry
& p
= M_PALETTEDATA
->m_entries
[pixel
];
150 if (red
) *red
= p
.red
;
151 if (green
) *green
= p
.green
;
152 if (blue
) *blue
= p
.blue
;