1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/palette.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/palette.h"
17 #include "wx/colour.h"
19 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
21 // ============================================================================
23 // ============================================================================
25 class WXDLLEXPORT wxPaletteRefData
: public wxGDIRefData
29 wxPaletteRefData(const wxPaletteRefData
& data
);
30 virtual ~wxPaletteRefData();
32 virtual bool IsOk() const { return m_count
> 0; }
38 friend class WXDLLIMPEXP_FWD_CORE wxPalette
;
40 DECLARE_NO_ASSIGN_CLASS(wxPaletteRefData
)
43 wxPaletteRefData::wxPaletteRefData()
49 wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData
& data
)
51 m_count
= data
.m_count
;
52 m_palette
= new wxColour
[m_count
];
53 for ( wxInt32 i
= 0; i
< m_count
; i
++ )
54 m_palette
[i
] = data
.m_palette
[i
];
57 wxPaletteRefData::~wxPaletteRefData()
62 // ============================================================================
64 // ============================================================================
66 wxPalette::wxPalette()
70 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
72 Create(n
, red
, green
, blue
);
75 wxPalette::~wxPalette()
79 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
83 m_refData
= new wxPaletteRefData
;
85 M_PALETTEDATA
->m_count
= n
;
86 M_PALETTEDATA
->m_palette
= new wxColour
[n
] ;
88 for ( int i
= 0 ; i
< n
; ++i
)
90 M_PALETTEDATA
->m_palette
[i
].Set( red
[i
] , green
[i
] , blue
[i
] ) ;
96 int wxPalette::GetPixel(unsigned char red
, unsigned char green
, unsigned char blue
) const
101 long bestdiff
= 3 * 256 ;
105 for ( int i
= 0 ; i
< M_PALETTEDATA
->m_count
; ++i
)
107 const wxColour
& col
= M_PALETTEDATA
->m_palette
[i
] ;
108 currentdiff
= abs ( col
.Red() - red
) + abs( col
.Green() - green
) + abs ( col
.Blue() - blue
) ;
109 if ( currentdiff
< bestdiff
)
111 bestdiff
= currentdiff
;
121 bool wxPalette::GetRGB(int index
, unsigned char *red
, unsigned char *green
, unsigned char *blue
) const
126 if (index
< 0 || index
>= M_PALETTEDATA
->m_count
)
129 const wxColour
& col
= M_PALETTEDATA
->m_palette
[index
] ;
131 *green
= col
.Green() ;
137 int wxPalette::GetColoursCount() const
140 return M_PALETTEDATA
->m_count
;
145 wxGDIRefData
*wxPalette::CreateGDIRefData() const
147 return new wxPaletteRefData
;
150 wxGDIRefData
*wxPalette::CloneGDIRefData(const wxGDIRefData
*data
) const
152 return new wxPaletteRefData(*wx_static_cast(const wxPaletteRefData
*, data
));
155 #endif // wxUSE_PALETTE