1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/gdicmn.h"
19 #include "wx/gtk1/private.h"
23 #include <gdk/gdkprivate.h>
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 class wxColourRefData
: public wxGDIRefData
42 wxColourRefData(const wxColourRefData
& data
)
44 m_color
= data
.m_color
;
45 m_colormap
= data
.m_colormap
;
46 m_hasPixel
= data
.m_hasPixel
;
49 virtual ~wxColourRefData()
54 bool operator == (const wxColourRefData
& data
) const
56 return (m_colormap
== data
.m_colormap
&&
57 m_hasPixel
== data
.m_hasPixel
&&
58 m_color
.red
== data
.m_color
.red
&&
59 m_color
.green
== data
.m_color
.green
&&
60 m_color
.blue
== data
.m_color
.blue
&&
61 m_color
.pixel
== data
.m_color
.pixel
);
65 void AllocColour( GdkColormap
* cmap
);
68 GdkColormap
*m_colormap
;
71 friend class wxColour
;
73 // reference counter for systems with <= 8-Bit display
74 static gushort colMapAllocCounter
[ 256 ];
77 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
94 void wxColourRefData::FreeColour()
98 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
99 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
100 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
102 int idx
= m_color
.pixel
;
103 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
105 if (colMapAllocCounter
[ idx
] == 0)
106 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
111 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
113 if (m_hasPixel
&& (m_colormap
== cmap
))
118 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
119 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
120 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
122 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
123 int idx
= m_color
.pixel
;
124 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
128 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
133 //-----------------------------------------------------------------------------
135 #define M_COLDATA ((wxColourRefData *)m_refData)
137 // GDK's values are in 0..65535 range, our are in 0..255
140 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
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 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
155 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
156 return own
->red
== other
->red
&&
157 own
->blue
== other
->blue
&&
158 own
->green
== other
->green
;
161 wxGDIRefData
*wxColour::CreateGDIRefData() const
163 return new wxColourRefData
;
166 wxGDIRefData
*wxColour::CloneGDIRefData(const wxGDIRefData
*data
) const
168 return new wxColourRefData(*(wxColourRefData
*)data
);
171 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
172 unsigned char WXUNUSED(alpha
))
176 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
177 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
178 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
179 M_COLDATA
->m_color
.pixel
= 0;
181 M_COLDATA
->m_colormap
= NULL
;
182 M_COLDATA
->m_hasPixel
= false;
185 unsigned char wxColour::Red() const
187 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
189 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
192 unsigned char wxColour::Green() const
194 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
196 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
199 unsigned char wxColour::Blue() const
201 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
203 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
206 void wxColour::CalcPixel( GdkColormap
*cmap
)
210 M_COLDATA
->AllocColour( cmap
);
213 int wxColour::GetPixel() const
215 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
217 return M_COLDATA
->m_color
.pixel
;
220 GdkColor
*wxColour::GetColor() const
222 wxCHECK_MSG( Ok(), NULL
, wxT("invalid colour") );
224 return &M_COLDATA
->m_color
;
227 bool wxColour::FromString(const wxString
& str
)
230 if ( gdk_color_parse( wxGTK_CONV(str
), &colGDK
) )
234 m_refData
= new wxColourRefData
;
235 M_COLDATA
->m_color
= colGDK
;
239 return wxColourBase::FromString(str
);