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
32 void AllocColour( GdkColormap
* cmap
);
36 GdkColormap
*m_colormap
;
39 friend class wxColour
;
41 // reference counter for systems with <= 8-Bit display
42 static gushort colMapAllocCounter
[ 256 ];
45 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
62 wxColourRefData::wxColourRefData()
68 m_colormap
= (GdkColormap
*) NULL
;
72 wxColourRefData::~wxColourRefData()
77 void wxColourRefData::FreeColour()
82 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
83 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
85 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
86 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
87 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
90 int idx
= m_color
.pixel
;
91 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
93 if (colMapAllocCounter
[ idx
] == 0)
94 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
99 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
101 if (m_hasPixel
&& (m_colormap
== cmap
))
107 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
108 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
110 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
111 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
112 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
115 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
116 int idx
= m_color
.pixel
;
117 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
121 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
126 //-----------------------------------------------------------------------------
128 #define M_COLDATA ((wxColourRefData *)m_refData)
130 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
132 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
138 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
140 m_refData
= new wxColourRefData();
141 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
142 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
143 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
144 M_COLDATA
->m_color
.pixel
= 0;
147 void wxColour::InitFromName( const wxString
&colourName
)
149 wxNode
*node
= (wxNode
*) NULL
;
150 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
152 wxColour
*col
= (wxColour
*)node
->Data();
154 if (col
) Ref( *col
);
158 m_refData
= new wxColourRefData();
159 if (!gdk_color_parse( colourName
.mb_str(), &M_COLDATA
->m_color
))
161 // VZ: asserts are good in general but this one is triggered by
162 // calling wxColourDatabase::FindColour() with an
163 // unrecognized colour name and this can't be avoided from the
164 // user code, so don't give it here
166 // a better solution would be to changed code in FindColour()
168 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
171 m_refData
= (wxObjectRefData
*) NULL
;
176 wxColour::wxColour( const wxColour
& col
)
181 wxColour::~wxColour()
185 wxColour
& wxColour::operator = ( const wxColour
& col
)
187 if (*this == col
) return (*this);
192 bool wxColour::operator == ( const wxColour
& col
) const
194 if (m_refData
== col
.m_refData
) return TRUE
;
196 if (!m_refData
) return FALSE
;
197 if (!col
.m_refData
) return FALSE
;
199 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
200 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
201 if (own
->red
!= other
->red
) return FALSE
;
202 if (own
->blue
!= other
->blue
) return FALSE
;
203 if (own
->green
!= other
->green
) return FALSE
;
208 bool wxColour::operator != ( const wxColour
& col
) const
210 return !(*this == col
);
213 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
216 m_refData
= new wxColourRefData();
217 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
218 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
219 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
220 M_COLDATA
->m_color
.pixel
= 0;
223 unsigned char wxColour::Red() const
225 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
227 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
230 unsigned char wxColour::Green() const
232 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
234 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
237 unsigned char wxColour::Blue() const
239 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
241 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
244 bool wxColour::Ok() const
246 return (m_refData
!= NULL
);
249 void wxColour::CalcPixel( GdkColormap
*cmap
)
253 M_COLDATA
->AllocColour( cmap
);
256 int wxColour::GetPixel() const
258 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
260 return M_COLDATA
->m_color
.pixel
;
263 GdkColor
*wxColour::GetColor() const
265 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
267 return &M_COLDATA
->m_color
;