1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "colour.h"
15 #include "wx/gdicmn.h"
19 #include <gdk/gdkprivate.h>
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 class wxColourRefData
: public wxObjectRefData
34 m_colormap
= (GdkColormap
*) NULL
;
43 bool operator == (const wxColourRefData
& data
) const
45 return (m_colormap
== data
.m_colormap
&&
46 m_hasPixel
== data
.m_hasPixel
&&
47 m_color
.red
== data
.m_color
.red
&&
48 m_color
.green
== data
.m_color
.green
&&
49 m_color
.blue
== data
.m_color
.blue
&&
50 m_color
.pixel
== data
.m_color
.pixel
);
54 void AllocColour( GdkColormap
* cmap
);
57 GdkColormap
*m_colormap
;
60 friend class wxColour
;
62 // reference counter for systems with <= 8-Bit display
63 static gushort colMapAllocCounter
[ 256 ];
66 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
83 void wxColourRefData::FreeColour()
88 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
89 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
91 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
92 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
93 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
96 int idx
= m_color
.pixel
;
97 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
99 if (colMapAllocCounter
[ idx
] == 0)
100 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
105 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
107 if (m_hasPixel
&& (m_colormap
== cmap
))
113 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
114 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
116 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
117 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
118 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
121 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
122 int idx
= m_color
.pixel
;
123 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
127 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
132 //-----------------------------------------------------------------------------
134 #define M_COLDATA ((wxColourRefData *)m_refData)
136 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
138 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
140 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
142 m_refData
= new wxColourRefData();
143 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
144 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
145 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
146 M_COLDATA
->m_color
.pixel
= 0;
149 void wxColour::InitFromName( const wxString
&colourName
)
151 wxNode
*node
= (wxNode
*) NULL
;
152 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
154 wxColour
*col
= (wxColour
*)node
->Data();
156 if (col
) Ref( *col
);
160 m_refData
= new wxColourRefData();
161 if (!gdk_color_parse( colourName
.mb_str(), &M_COLDATA
->m_color
))
163 // VZ: asserts are good in general but this one is triggered by
164 // calling wxColourDatabase::FindColour() with an
165 // unrecognized colour name and this can't be avoided from the
166 // user code, so don't give it here
168 // a better solution would be to changed code in FindColour()
170 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
173 m_refData
= (wxObjectRefData
*) NULL
;
178 wxColour::~wxColour()
182 bool wxColour::operator == ( const wxColour
& col
) const
184 if (m_refData
== col
.m_refData
) return TRUE
;
186 if (!m_refData
|| !col
.m_refData
) return FALSE
;
188 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
189 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
190 if (own
->red
!= other
->red
) return FALSE
;
191 if (own
->blue
!= other
->blue
) return FALSE
;
192 if (own
->green
!= other
->green
) return FALSE
;
197 wxObjectRefData
*wxColour::CreateRefData() const
199 return new wxColourRefData
;
202 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
204 return new wxColourRefData(*(wxColourRefData
*)data
);
207 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
211 m_refData
= new wxColourRefData();
212 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
213 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
214 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
215 M_COLDATA
->m_color
.pixel
= 0;
218 unsigned char wxColour::Red() const
220 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
222 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
225 unsigned char wxColour::Green() const
227 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
229 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
232 unsigned char wxColour::Blue() const
234 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
236 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
239 void wxColour::CalcPixel( GdkColormap
*cmap
)
243 M_COLDATA
->AllocColour( cmap
);
246 int wxColour::GetPixel() const
248 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
250 return M_COLDATA
->m_color
.pixel
;
253 GdkColor
*wxColour::GetColor() const
255 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
257 return &M_COLDATA
->m_color
;