]>
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 wxObjectRefData
26 wxColourRefData(guint16 red
, guint16 green
, guint16 blue
)
41 void AllocColour( GdkColormap
* cmap
);
44 GdkColormap
*m_colormap
;
45 // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies
50 DECLARE_NO_COPY_CLASS(wxColourRefData
)
53 void wxColourRefData::FreeColour()
57 gdk_colormap_free_colors(m_colormap
, &m_color
, 1);
63 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
65 if (m_colormap
!= cmap
)
70 m_color
.green
= m_green
;
71 m_color
.blue
= m_blue
;
72 if (gdk_colormap_alloc_color(cmap
, &m_color
, FALSE
, TRUE
))
79 //-----------------------------------------------------------------------------
81 #define M_COLDATA wx_static_cast(wxColourRefData*, m_refData)
83 // GDK's values are in 0..65535 range, ours are in 0..255
86 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
88 wxColour::wxColour(const GdkColor
& gdkColor
)
90 m_refData
= new wxColourRefData(gdkColor
.red
, gdkColor
.green
, gdkColor
.blue
);
97 bool wxColour::operator == ( const wxColour
& col
) const
99 if (m_refData
== col
.m_refData
)
102 if (!m_refData
|| !col
.m_refData
)
105 wxColourRefData
* refData
= M_COLDATA
;
106 wxColourRefData
* that_refData
= wx_static_cast(wxColourRefData
*, col
.m_refData
);
107 return refData
->m_red
== that_refData
->m_red
&&
108 refData
->m_green
== that_refData
->m_green
&&
109 refData
->m_blue
== that_refData
->m_blue
;
112 void wxColour::InitWith( unsigned char red
, unsigned char green
, unsigned char blue
)
116 m_refData
= new wxColourRefData(
117 (guint16(red
) << SHIFT
) + red
,
118 (guint16(green
) << SHIFT
) + green
,
119 (guint16(blue
) << SHIFT
) + blue
);
122 unsigned char wxColour::Red() const
124 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
126 return wxByte(M_COLDATA
->m_red
>> SHIFT
);
129 unsigned char wxColour::Green() const
131 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
133 return wxByte(M_COLDATA
->m_green
>> SHIFT
);
136 unsigned char wxColour::Blue() const
138 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
140 return wxByte(M_COLDATA
->m_blue
>> SHIFT
);
143 void wxColour::CalcPixel( GdkColormap
*cmap
)
147 M_COLDATA
->AllocColour( cmap
);
150 int wxColour::GetPixel() const
152 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
154 return M_COLDATA
->m_color
.pixel
;
157 const GdkColor
*wxColour::GetColor() const
159 wxCHECK_MSG( Ok(), NULL
, wxT("invalid colour") );
161 return &M_COLDATA
->m_color
;
164 bool wxColour::FromString(const wxChar
*str
)
167 if ( gdk_color_parse( wxGTK_CONV_SYS( str
), &colGDK
) )
169 *this = wxColour(colGDK
);
173 return wxColourBase::FromString(str
);