]>
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"
15 #include "wx/gtk/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 class wxColourRefData
: public wxGDIRefData
26 wxColourRefData(guint16 red
, guint16 green
, guint16 blue
, guint16 alpha
)
39 virtual ~wxColourRefData()
45 void AllocColour( GdkColormap
* cmap
);
48 GdkColormap
*m_colormap
;
49 // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies
55 DECLARE_NO_COPY_CLASS(wxColourRefData
)
58 void wxColourRefData::FreeColour()
62 gdk_colormap_free_colors(m_colormap
, &m_color
, 1);
68 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
70 if (m_colormap
!= cmap
)
75 m_color
.green
= m_green
;
76 m_color
.blue
= m_blue
;
77 if (gdk_colormap_alloc_color(cmap
, &m_color
, FALSE
, TRUE
))
84 //-----------------------------------------------------------------------------
86 #define M_COLDATA wx_static_cast(wxColourRefData*, m_refData)
88 // GDK's values are in 0..65535 range, ours are in 0..255
91 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
93 wxColour::wxColour(const GdkColor
& gdkColor
)
95 m_refData
= new wxColourRefData(gdkColor
.red
, gdkColor
.green
, gdkColor
.blue
, 0xFFFF /*opaque alpha in 16 bit*/);
102 bool wxColour::operator == ( const wxColour
& col
) const
104 if (m_refData
== col
.m_refData
)
107 if (!m_refData
|| !col
.m_refData
)
110 wxColourRefData
* refData
= M_COLDATA
;
111 wxColourRefData
* that_refData
= wx_static_cast(wxColourRefData
*, col
.m_refData
);
112 return refData
->m_red
== that_refData
->m_red
&&
113 refData
->m_green
== that_refData
->m_green
&&
114 refData
->m_blue
== that_refData
->m_blue
&&
115 refData
->m_alpha
== that_refData
->m_alpha
;
118 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
123 m_refData
= new wxColourRefData(
124 (guint16(red
) << SHIFT
) + red
,
125 (guint16(green
) << SHIFT
) + green
,
126 (guint16(blue
) << SHIFT
) + blue
,
127 (guint16(alpha
) << SHIFT
) + alpha
);
130 unsigned char wxColour::Red() const
132 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
134 return wxByte(M_COLDATA
->m_red
>> SHIFT
);
137 unsigned char wxColour::Green() const
139 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
141 return wxByte(M_COLDATA
->m_green
>> SHIFT
);
144 unsigned char wxColour::Blue() const
146 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
148 return wxByte(M_COLDATA
->m_blue
>> SHIFT
);
151 unsigned char wxColour::Alpha() const
153 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
155 return wxByte(M_COLDATA
->m_alpha
>> SHIFT
);
158 void wxColour::CalcPixel( GdkColormap
*cmap
)
162 M_COLDATA
->AllocColour( cmap
);
165 int wxColour::GetPixel() const
167 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
169 return M_COLDATA
->m_color
.pixel
;
172 const GdkColor
*wxColour::GetColor() const
174 wxCHECK_MSG( Ok(), NULL
, wxT("invalid colour") );
176 return &M_COLDATA
->m_color
;
179 bool wxColour::FromString(const wxString
& str
)
182 if ( gdk_color_parse( wxGTK_CONV_SYS( str
), &colGDK
) )
184 *this = wxColour(colGDK
);
188 return wxColourBase::FromString(str
);