]>
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
, wxByte alpha
= 0xff)
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 wxDECLARE_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 static_cast<wxColourRefData*>(m_refData)
88 // GDK's values are in 0..65535 range, ours are in 0..255
91 wxColour::wxColour(const GdkColor
& gdkColor
)
93 m_refData
= new wxColourRefData(gdkColor
.red
, gdkColor
.green
, gdkColor
.blue
);
100 bool wxColour::operator == ( const wxColour
& col
) const
102 if (m_refData
== col
.m_refData
)
105 if (!m_refData
|| !col
.m_refData
)
108 wxColourRefData
* refData
= M_COLDATA
;
109 wxColourRefData
* that_refData
= static_cast<wxColourRefData
*>(col
.m_refData
);
110 return refData
->m_red
== that_refData
->m_red
&&
111 refData
->m_green
== that_refData
->m_green
&&
112 refData
->m_blue
== that_refData
->m_blue
&&
113 refData
->m_alpha
== that_refData
->m_alpha
;
116 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
121 m_refData
= new wxColourRefData(
122 (guint16(red
) << SHIFT
) + red
,
123 (guint16(green
) << SHIFT
) + green
,
124 (guint16(blue
) << SHIFT
) + blue
,
128 unsigned char wxColour::Red() const
130 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
132 return wxByte(M_COLDATA
->m_red
>> SHIFT
);
135 unsigned char wxColour::Green() const
137 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
139 return wxByte(M_COLDATA
->m_green
>> SHIFT
);
142 unsigned char wxColour::Blue() const
144 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
146 return wxByte(M_COLDATA
->m_blue
>> SHIFT
);
149 unsigned char wxColour::Alpha() const
151 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
153 return M_COLDATA
->m_alpha
;
156 void wxColour::CalcPixel( GdkColormap
*cmap
)
160 M_COLDATA
->AllocColour( cmap
);
163 int wxColour::GetPixel() const
165 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
167 return M_COLDATA
->m_color
.pixel
;
170 const GdkColor
*wxColour::GetColor() const
172 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid colour") );
174 return &M_COLDATA
->m_color
;
177 bool wxColour::FromString(const wxString
& str
)
180 if ( gdk_color_parse( wxGTK_CONV_SYS( str
), &colGDK
) )
182 *this = wxColour(colGDK
);
186 return wxColourBase::FromString(str
);