]>
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 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #if defined(__BORLANDC__)
20 #include "wx/palette.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
28 unsigned char red
, green
, blue
;
31 class wxPaletteRefData
: public wxObjectRefData
35 wxPaletteRefData(void);
36 ~wxPaletteRefData(void);
39 wxPaletteEntry
*m_entries
;
42 wxPaletteRefData::wxPaletteRefData()
48 wxPaletteRefData::~wxPaletteRefData()
53 //-----------------------------------------------------------------------------
55 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
57 IMPLEMENT_DYNAMIC_CLASS(wxPalette
,wxGDIObject
)
59 wxPalette::wxPalette()
64 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
66 Create(n
, red
, green
, blue
);
69 wxPalette::wxPalette(const wxPalette
& palette
)
75 wxPalette::~wxPalette()
79 wxPalette
& wxPalette::operator = (const wxPalette
& palette
)
81 if (*this == palette
) return (*this);
86 bool wxPalette::operator == (const wxPalette
& palette
) const
88 return m_refData
== palette
.m_refData
;
91 bool wxPalette::operator != (const wxPalette
& palette
) const
93 return m_refData
!= palette
.m_refData
;
96 bool wxPalette::Ok(void) const
98 return (m_refData
!= NULL
);
101 bool wxPalette::Create(int n
,
102 const unsigned char *red
,
103 const unsigned char *green
,
104 const unsigned char *blue
)
107 m_refData
= new wxPaletteRefData();
109 M_PALETTEDATA
->m_count
= n
;
110 M_PALETTEDATA
->m_entries
= new wxPaletteEntry
[n
];
112 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
113 for (int i
= 0; i
< n
; i
++, e
++)
123 int wxPalette::GetPixel( unsigned char red
,
125 unsigned char blue
) const
127 if (!m_refData
) return wxNOT_FOUND
;
130 double d
,distance
= 1000.0; // max. dist is 256
132 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
133 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
135 if ((d
= 0.299 * abs(red
- e
->red
) +
136 0.587 * abs(green
- e
->green
) +
137 0.114 * abs(blue
- e
->blue
)) < distance
) {
145 bool wxPalette::GetRGB(int pixel
,
147 unsigned char *green
,
148 unsigned char *blue
) const
150 if (!m_refData
) return false;
151 if (pixel
>= M_PALETTEDATA
->m_count
) return false;
153 wxPaletteEntry
& p
= M_PALETTEDATA
->m_entries
[pixel
];
154 if (red
) *red
= p
.red
;
155 if (green
) *green
= p
.green
;
156 if (blue
) *blue
= p
.blue
;
160 #endif // wxUSE_PALETTE