]>
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 wxGDIRefData
35 wxPaletteRefData(const wxPaletteRefData
& palette
);
36 virtual ~wxPaletteRefData();
39 wxPaletteEntry
*m_entries
;
42 wxPaletteRefData::wxPaletteRefData()
48 wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData
& palette
)
50 m_count
= palette
.m_count
;
51 m_entries
= new wxPaletteEntry
[m_count
];
52 for ( int i
= 0; i
< m_count
; i
++ )
53 m_entries
[i
] = palette
.m_entries
[i
];
56 wxPaletteRefData::~wxPaletteRefData()
61 //-----------------------------------------------------------------------------
63 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
65 IMPLEMENT_DYNAMIC_CLASS(wxPalette
,wxGDIObject
)
67 wxPalette::wxPalette()
72 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
74 Create(n
, red
, green
, blue
);
77 wxPalette::~wxPalette()
81 int wxPalette::GetColoursCount() const
84 return M_PALETTEDATA
->m_count
;
89 bool wxPalette::Create(int n
,
90 const unsigned char *red
,
91 const unsigned char *green
,
92 const unsigned char *blue
)
95 m_refData
= new wxPaletteRefData();
97 M_PALETTEDATA
->m_count
= n
;
98 M_PALETTEDATA
->m_entries
= new wxPaletteEntry
[n
];
100 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
101 for (int i
= 0; i
< n
; i
++, e
++)
111 int wxPalette::GetPixel( unsigned char red
,
113 unsigned char blue
) const
115 if (!m_refData
) return wxNOT_FOUND
;
118 double d
,distance
= 1000.0; // max. dist is 256
120 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
121 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
123 if ((d
= 0.299 * abs(red
- e
->red
) +
124 0.587 * abs(green
- e
->green
) +
125 0.114 * abs(blue
- e
->blue
)) < distance
) {
133 bool wxPalette::GetRGB(int pixel
,
135 unsigned char *green
,
136 unsigned char *blue
) const
138 if (!m_refData
) return false;
139 if (pixel
>= M_PALETTEDATA
->m_count
) return false;
141 wxPaletteEntry
& p
= M_PALETTEDATA
->m_entries
[pixel
];
142 if (red
) *red
= p
.red
;
143 if (green
) *green
= p
.green
;
144 if (blue
) *blue
= p
.blue
;
148 wxGDIRefData
*wxPalette::CreateGDIRefData() const
150 return new wxPaletteRefData
;
153 wxGDIRefData
*wxPalette::CloneGDIRefData(const wxGDIRefData
*data
) const
155 return new wxPaletteRefData(*static_cast<const wxPaletteRefData
*>(data
));
158 #endif // wxUSE_PALETTE