]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/colour.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/colour.h"
16 #include "wx/gtk/private.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 class wxColourRefData
: public wxGDIRefData
26 wxColourRefData(const GdkRGBA
& gdkRGBA
)
29 m_gdkColor
.red
= guint16(gdkRGBA
.red
* 65535);
30 m_gdkColor
.green
= guint16(gdkRGBA
.green
* 65535);
31 m_gdkColor
.blue
= guint16(gdkRGBA
.blue
* 65535);
32 m_alpha
= wxByte(gdkRGBA
.alpha
* 255 + 0.5);
34 wxColourRefData(const GdkColor
& gdkColor
)
35 : m_gdkColor(gdkColor
)
37 m_gdkRGBA
.red
= gdkColor
.red
/ 65535.0;
38 m_gdkRGBA
.green
= gdkColor
.green
/ 65535.0;
39 m_gdkRGBA
.blue
= gdkColor
.blue
/ 65535.0;
43 wxColourRefData(guchar red
, guchar green
, guchar blue
, guchar alpha
)
45 m_gdkRGBA
.red
= red
/ 255.0;
46 m_gdkRGBA
.green
= green
/ 255.0;
47 m_gdkRGBA
.blue
= blue
/ 255.0;
48 m_gdkRGBA
.alpha
= alpha
/ 255.0;
49 m_gdkColor
.red
= (guint16(red
) << 8) + red
;
50 m_gdkColor
.green
= (guint16(green
) << 8) + green
;
51 m_gdkColor
.blue
= (guint16(blue
) << 8) + blue
;
57 wxColourRefData(guint16 red
, guint16 green
, guint16 blue
, wxByte alpha
= 0xff)
70 virtual ~wxColourRefData()
76 void AllocColour( GdkColormap
* cmap
);
79 GdkColormap
*m_colormap
;
80 // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies
87 wxDECLARE_NO_COPY_CLASS(wxColourRefData
);
91 void wxColourRefData::FreeColour()
95 gdk_colormap_free_colors(m_colormap
, &m_color
, 1);
101 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
103 if (m_colormap
!= cmap
)
108 m_color
.green
= m_green
;
109 m_color
.blue
= m_blue
;
110 if (gdk_colormap_alloc_color(cmap
, &m_color
, FALSE
, TRUE
))
118 //-----------------------------------------------------------------------------
120 #define M_COLDATA static_cast<wxColourRefData*>(m_refData)
122 // GDK's values are in 0..65535 range, ours are in 0..255
126 wxColour::wxColour(const GdkRGBA
& gdkRGBA
)
128 m_refData
= new wxColourRefData(gdkRGBA
);
131 wxColour::wxColour(const GdkColor
& gdkColor
)
133 m_refData
= new wxColourRefData(gdkColor
);
136 wxColour::wxColour(const GdkColor
& gdkColor
)
138 m_refData
= new wxColourRefData(gdkColor
.red
, gdkColor
.green
, gdkColor
.blue
);
142 wxColour::~wxColour()
146 bool wxColour::operator == ( const wxColour
& col
) const
148 if (m_refData
== col
.m_refData
)
151 if (!m_refData
|| !col
.m_refData
)
154 wxColourRefData
* refData
= M_COLDATA
;
155 wxColourRefData
* that_refData
= static_cast<wxColourRefData
*>(col
.m_refData
);
157 return refData
->m_gdkColor
.red
== that_refData
->m_gdkColor
.red
&&
158 refData
->m_gdkColor
.green
== that_refData
->m_gdkColor
.green
&&
159 refData
->m_gdkColor
.blue
== that_refData
->m_gdkColor
.blue
&&
161 return refData
->m_red
== that_refData
->m_red
&&
162 refData
->m_green
== that_refData
->m_green
&&
163 refData
->m_blue
== that_refData
->m_blue
&&
165 refData
->m_alpha
== that_refData
->m_alpha
;
168 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
174 m_refData
= new wxColourRefData(red
, green
, blue
, alpha
);
176 m_refData
= new wxColourRefData(
177 (guint16(red
) << SHIFT
) + red
,
178 (guint16(green
) << SHIFT
) + green
,
179 (guint16(blue
) << SHIFT
) + blue
,
184 unsigned char wxColour::Red() const
186 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
189 return wxByte(M_COLDATA
->m_gdkColor
.red
>> 8);
191 return wxByte(M_COLDATA
->m_red
>> SHIFT
);
195 unsigned char wxColour::Green() const
197 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
200 return wxByte(M_COLDATA
->m_gdkColor
.green
>> 8);
202 return wxByte(M_COLDATA
->m_green
>> SHIFT
);
206 unsigned char wxColour::Blue() const
208 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
211 return wxByte(M_COLDATA
->m_gdkColor
.blue
>> 8);
213 return wxByte(M_COLDATA
->m_blue
>> SHIFT
);
217 unsigned char wxColour::Alpha() const
219 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
221 return M_COLDATA
->m_alpha
;
225 void wxColour::CalcPixel( GdkColormap
*cmap
)
229 M_COLDATA
->AllocColour( cmap
);
232 int wxColour::GetPixel() const
234 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
236 return M_COLDATA
->m_color
.pixel
;
240 const GdkColor
*wxColour::GetColor() const
242 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid colour") );
245 return &M_COLDATA
->m_gdkColor
;
247 return &M_COLDATA
->m_color
;
252 wxColour::operator const GdkRGBA
*() const
254 const GdkRGBA
* c
= NULL
;
256 c
= &M_COLDATA
->m_gdkRGBA
;
261 bool wxColour::FromString(const wxString
& str
)
265 if (gdk_rgba_parse(&gdkRGBA
, wxGTK_CONV_SYS(str
)))
267 *this = wxColour(gdkRGBA
);
272 if ( gdk_color_parse( wxGTK_CONV_SYS( str
), &colGDK
) )
274 *this = wxColour(colGDK
);
279 return wxColourBase::FromString(str
);