]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/paletteg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/paletteg.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if defined(__BORLANDC__)
19 #include "wx/palette.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
27 unsigned char red
, green
, blue
;
30 class wxPaletteRefData
: public wxGDIRefData
34 wxPaletteRefData(const wxPaletteRefData
& palette
);
35 virtual ~wxPaletteRefData();
38 wxPaletteEntry
*m_entries
;
41 wxPaletteRefData::wxPaletteRefData()
47 wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData
& palette
)
49 m_count
= palette
.m_count
;
50 m_entries
= new wxPaletteEntry
[m_count
];
51 for ( int i
= 0; i
< m_count
; i
++ )
52 m_entries
[i
] = palette
.m_entries
[i
];
55 wxPaletteRefData::~wxPaletteRefData()
60 //-----------------------------------------------------------------------------
62 #define M_PALETTEDATA ((wxPaletteRefData *)m_refData)
64 IMPLEMENT_DYNAMIC_CLASS(wxPalette
,wxGDIObject
)
66 wxPalette::wxPalette()
71 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
73 Create(n
, red
, green
, blue
);
76 wxPalette::~wxPalette()
80 int wxPalette::GetColoursCount() const
83 return M_PALETTEDATA
->m_count
;
88 bool wxPalette::Create(int n
,
89 const unsigned char *red
,
90 const unsigned char *green
,
91 const unsigned char *blue
)
94 m_refData
= new wxPaletteRefData();
96 M_PALETTEDATA
->m_count
= n
;
97 M_PALETTEDATA
->m_entries
= new wxPaletteEntry
[n
];
99 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
100 for (int i
= 0; i
< n
; i
++, e
++)
110 int wxPalette::GetPixel( unsigned char red
,
112 unsigned char blue
) const
114 if (!m_refData
) return wxNOT_FOUND
;
117 double d
,distance
= 1000.0; // max. dist is 256
119 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
120 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
122 if ((d
= 0.299 * abs(red
- e
->red
) +
123 0.587 * abs(green
- e
->green
) +
124 0.114 * abs(blue
- e
->blue
)) < distance
) {
132 bool wxPalette::GetRGB(int pixel
,
134 unsigned char *green
,
135 unsigned char *blue
) const
137 if (!m_refData
) return false;
138 if (pixel
>= M_PALETTEDATA
->m_count
) return false;
140 wxPaletteEntry
& p
= M_PALETTEDATA
->m_entries
[pixel
];
141 if (red
) *red
= p
.red
;
142 if (green
) *green
= p
.green
;
143 if (blue
) *blue
= p
.blue
;
147 wxGDIRefData
*wxPalette::CreateGDIRefData() const
149 return new wxPaletteRefData
;
152 wxGDIRefData
*wxPalette::CloneGDIRefData(const wxGDIRefData
*data
) const
154 return new wxPaletteRefData(*static_cast<const wxPaletteRefData
*>(data
));
157 #endif // wxUSE_PALETTE