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();
162 if (!gdk_color_parse( colourName
.mb_str(), &M_COLDATA
->m_color
))
164 // VZ: asserts are good in general but this one is triggered by
165 // calling wxColourDatabase::FindColour() with an
166 // unrecognized colour name and this can't be avoided from the
167 // user code, so don't give it here
169 // a better solution would be to changed code in FindColour()
171 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
174 m_refData
= (wxObjectRefData
*) NULL
;
179 wxColour::~wxColour()
183 bool wxColour::operator == ( const wxColour
& col
) const
185 if (m_refData
== col
.m_refData
) return TRUE
;
187 if (!m_refData
|| !col
.m_refData
) return FALSE
;
189 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
190 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
191 if (own
->red
!= other
->red
) return FALSE
;
192 if (own
->blue
!= other
->blue
) return FALSE
;
193 if (own
->green
!= other
->green
) return FALSE
;
198 wxObjectRefData
*wxColour::CreateRefData() const
200 return new wxColourRefData
;
203 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
205 return new wxColourRefData(*(wxColourRefData
*)data
);
208 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
212 m_refData
= new wxColourRefData();
213 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
214 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
215 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
216 M_COLDATA
->m_color
.pixel
= 0;
219 unsigned char wxColour::Red() const
221 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
223 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
226 unsigned char wxColour::Green() const
228 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
230 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
233 unsigned char wxColour::Blue() const
235 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
237 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
240 void wxColour::CalcPixel( GdkColormap
*cmap
)
244 M_COLDATA
->AllocColour( cmap
);
247 int wxColour::GetPixel() const
249 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
251 return M_COLDATA
->m_color
.pixel
;
254 GdkColor
*wxColour::GetColor() const
256 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
258 return &M_COLDATA
->m_color
;