]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/palette.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "palette.h"
16 #include "wx/palette.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
26 unsigned char red
, green
, blue
;
29 class wxPaletteRefData
: public wxObjectRefData
33 wxPaletteRefData(void);
34 ~wxPaletteRefData(void);
37 wxPaletteEntry
*m_entries
;
39 GdkColormap
*m_colormap
;
43 wxPaletteRefData::wxPaletteRefData()
48 m_colormap
= (GdkColormap
*) NULL
;
52 wxPaletteRefData::~wxPaletteRefData()
56 if (m_colormap
) gdk_colormap_unref( m_colormap
);
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(const wxPalette
& palette
)
81 wxPalette::~wxPalette()
85 wxPalette
& wxPalette::operator = (const wxPalette
& palette
)
87 if (*this == palette
) return (*this);
92 bool wxPalette::operator == (const wxPalette
& palette
)
94 return m_refData
== palette
.m_refData
;
97 bool wxPalette::operator != (const wxPalette
& palette
)
99 return m_refData
!= palette
.m_refData
;
102 bool wxPalette::Ok(void) const
104 return (m_refData
!= NULL
);
107 bool wxPalette::Create(int n
,
108 const unsigned char *red
,
109 const unsigned char *green
,
110 const unsigned char *blue
)
113 m_refData
= new wxPaletteRefData();
115 M_PALETTEDATA
->m_count
= n
;
116 M_PALETTEDATA
->m_entries
= new wxPaletteEntry
[n
];
118 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
119 for (int i
= 0; i
< n
; i
++, e
++)
129 int wxPalette::GetPixel( const unsigned char red
,
130 const unsigned char green
,
131 const unsigned char blue
) const
133 if (!m_refData
) return FALSE
;
136 double d
,distance
= 1000.0; // max. dist is 256
138 wxPaletteEntry
*e
= M_PALETTEDATA
->m_entries
;
139 for (int i
= 0; i
< M_PALETTEDATA
->m_count
; i
++, e
++)
141 if ((d
= 0.299 * abs(red
- e
->red
) +
142 0.587 * abs(green
- e
->green
) +
143 0.114 * abs(blue
- e
->blue
)) < distance
) {
151 bool wxPalette::GetRGB(int pixel
,
153 unsigned char *green
,
154 unsigned char *blue
) const
156 if (!m_refData
) return FALSE
;
157 if (pixel
>= M_PALETTEDATA
->m_count
) return FALSE
;
159 wxPaletteEntry
& p
= M_PALETTEDATA
->m_entries
[pixel
];
160 if (red
) *red
= p
.red
;
161 if (green
) *green
= p
.green
;
162 if (blue
) *blue
= p
.blue
;