]>
Commit | Line | Data |
---|---|---|
489468fe | 1 | ///////////////////////////////////////////////////////////////////////////// |
96dabe43 | 2 | // Name: src/osx/palette.cpp |
489468fe SC |
3 | // Purpose: wxPalette |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
489468fe SC |
7 | // Copyright: (c) Stefan Csomor |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_PALETTE | |
14 | ||
15 | #include "wx/palette.h" | |
16 | #include "wx/colour.h" | |
17 | ||
18 | IMPLEMENT_DYNAMIC_CLASS(wxPalette, wxGDIObject) | |
19 | ||
20 | // ============================================================================ | |
21 | // wxPaletteRefData | |
22 | // ============================================================================ | |
23 | ||
24 | class WXDLLEXPORT wxPaletteRefData: public wxGDIRefData | |
25 | { | |
26 | public: | |
27 | wxPaletteRefData(); | |
28 | wxPaletteRefData(const wxPaletteRefData& data); | |
29 | virtual ~wxPaletteRefData(); | |
30 | ||
31 | virtual bool IsOk() const { return m_count > 0; } | |
32 | ||
33 | protected: | |
34 | wxColour* m_palette; | |
35 | wxInt32 m_count; | |
36 | ||
37 | friend class WXDLLIMPEXP_FWD_CORE wxPalette; | |
38 | ||
c0c133e1 | 39 | wxDECLARE_NO_ASSIGN_CLASS(wxPaletteRefData); |
489468fe SC |
40 | }; |
41 | ||
42 | wxPaletteRefData::wxPaletteRefData() | |
43 | { | |
44 | m_palette = NULL; | |
45 | m_count = 0; | |
46 | } | |
47 | ||
ea2807a4 | 48 | wxPaletteRefData::wxPaletteRefData(const wxPaletteRefData& data) : wxGDIRefData() |
489468fe SC |
49 | { |
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]; | |
54 | } | |
55 | ||
56 | wxPaletteRefData::~wxPaletteRefData() | |
57 | { | |
58 | delete[] m_palette; | |
59 | } | |
60 | ||
61 | // ============================================================================ | |
62 | // wxPalette | |
63 | // ============================================================================ | |
64 | ||
65 | wxPalette::wxPalette() | |
66 | { | |
67 | } | |
68 | ||
69 | wxPalette::wxPalette(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) | |
70 | { | |
71 | Create(n, red, green, blue); | |
72 | } | |
73 | ||
74 | wxPalette::~wxPalette() | |
75 | { | |
76 | } | |
77 | ||
78 | bool wxPalette::Create(int n, const unsigned char *red, const unsigned char *green, const unsigned char *blue) | |
79 | { | |
80 | UnRef(); | |
81 | ||
82 | m_refData = new wxPaletteRefData; | |
83 | ||
84 | M_PALETTEDATA->m_count = n ; | |
85 | M_PALETTEDATA->m_palette = new wxColour[n] ; | |
86 | ||
87 | for ( int i = 0 ; i < n ; ++i) | |
88 | { | |
89 | M_PALETTEDATA->m_palette[i].Set( red[i] , green[i] , blue[i] ) ; | |
90 | } | |
91 | ||
92 | return false; | |
93 | } | |
94 | ||
95 | int wxPalette::GetPixel(unsigned char red, unsigned char green, unsigned char blue) const | |
96 | { | |
97 | if ( !m_refData ) | |
98 | return wxNOT_FOUND; | |
99 | ||
100 | long bestdiff = 3 * 256 ; | |
101 | long bestpos = 0 ; | |
102 | long currentdiff ; | |
103 | ||
104 | for ( int i = 0 ; i < M_PALETTEDATA->m_count ; ++i ) | |
105 | { | |
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 ) | |
109 | { | |
110 | bestdiff = currentdiff ; | |
111 | bestpos = i ; | |
112 | if ( bestdiff == 0 ) | |
113 | break ; | |
114 | } | |
115 | } | |
116 | ||
117 | return bestpos; | |
118 | } | |
119 | ||
120 | bool wxPalette::GetRGB(int index, unsigned char *red, unsigned char *green, unsigned char *blue) const | |
121 | { | |
122 | if ( !m_refData ) | |
123 | return false; | |
124 | ||
125 | if (index < 0 || index >= M_PALETTEDATA->m_count) | |
126 | return false; | |
127 | ||
128 | const wxColour& col = M_PALETTEDATA->m_palette[index] ; | |
129 | *red = col.Red() ; | |
130 | *green = col.Green() ; | |
131 | *blue = col.Blue() ; | |
132 | ||
133 | return true; | |
134 | } | |
135 | ||
136 | int wxPalette::GetColoursCount() const | |
137 | { | |
138 | if (m_refData) | |
139 | return M_PALETTEDATA->m_count; | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | wxGDIRefData *wxPalette::CreateGDIRefData() const | |
145 | { | |
146 | return new wxPaletteRefData; | |
147 | } | |
148 | ||
149 | wxGDIRefData *wxPalette::CloneGDIRefData(const wxGDIRefData *data) const | |
150 | { | |
5c33522f | 151 | return new wxPaletteRefData(*static_cast<const wxPaletteRefData *>(data)); |
489468fe SC |
152 | } |
153 | ||
154 | #endif // wxUSE_PALETTE |