1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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"
18 IMPLEMENT_DYNAMIC_CLASS(wxPalette
, wxGDIObject
)
20 // ============================================================================
22 // ============================================================================
24 class WXDLLEXPORT wxPaletteRefData
: public wxGDIRefData
28 wxPaletteRefData(const wxPaletteRefData
& data
);
29 virtual ~wxPaletteRefData();
31 virtual bool IsOk() const { return m_count
> 0; }
37 friend class WXDLLIMPEXP_FWD_CORE wxPalette
;
39 DECLARE_NO_ASSIGN_CLASS(wxPaletteRefData
)
42 wxPaletteRefData::wxPaletteRefData()
48 wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData
& data
)
50 m_count
= data
.m_count
;
51 m_palette
= new wxColour
[m_count
];
52 for ( wxInt32 i
= 0; i
< m_count
; i
++ )
53 m_palette
[i
] = data
.m_palette
[i
];
56 wxPaletteRefData::~wxPaletteRefData()
61 // ============================================================================
63 // ============================================================================
65 wxPalette::wxPalette()
69 wxPalette::wxPalette(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
71 Create(n
, red
, green
, blue
);
74 wxPalette::~wxPalette()
78 bool wxPalette::Create(int n
, const unsigned char *red
, const unsigned char *green
, const unsigned char *blue
)
82 m_refData
= new wxPaletteRefData
;
84 M_PALETTEDATA
->m_count
= n
;
85 M_PALETTEDATA
->m_palette
= new wxColour
[n
] ;
87 for ( int i
= 0 ; i
< n
; ++i
)
89 M_PALETTEDATA
->m_palette
[i
].Set( red
[i
] , green
[i
] , blue
[i
] ) ;
95 int wxPalette::GetPixel(unsigned char red
, unsigned char green
, unsigned char blue
) const
100 long bestdiff
= 3 * 256 ;
104 for ( int i
= 0 ; i
< M_PALETTEDATA
->m_count
; ++i
)
106 const wxColour
& col
= M_PALETTEDATA
->m_palette
[i
] ;
107 currentdiff
= abs ( col
.Red() - red
) + abs( col
.Green() - green
) + abs ( col
.Blue() - blue
) ;
108 if ( currentdiff
< bestdiff
)
110 bestdiff
= currentdiff
;
120 bool wxPalette::GetRGB(int index
, unsigned char *red
, unsigned char *green
, unsigned char *blue
) const
125 if (index
< 0 || index
>= M_PALETTEDATA
->m_count
)
128 const wxColour
& col
= M_PALETTEDATA
->m_palette
[index
] ;
130 *green
= col
.Green() ;
136 int wxPalette::GetColoursCount() const
139 return M_PALETTEDATA
->m_count
;
144 wxGDIRefData
*wxPalette::CreateGDIRefData() const
146 return new wxPaletteRefData
;
149 wxGDIRefData
*wxPalette::CloneGDIRefData(const wxGDIRefData
*data
) const
151 return new wxPaletteRefData(*wx_static_cast(const wxPaletteRefData
*, data
));
154 #endif // wxUSE_PALETTE