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
;
38 wxColourRefData(const wxColourRefData
& data
)
41 m_color
= data
.m_color
;
42 m_colormap
= data
.m_colormap
;
43 m_hasPixel
= data
.m_hasPixel
;
51 bool operator == (const wxColourRefData
& data
) const
53 return (m_colormap
== data
.m_colormap
&&
54 m_hasPixel
== data
.m_hasPixel
&&
55 m_color
.red
== data
.m_color
.red
&&
56 m_color
.green
== data
.m_color
.green
&&
57 m_color
.blue
== data
.m_color
.blue
&&
58 m_color
.pixel
== data
.m_color
.pixel
);
62 void AllocColour( GdkColormap
* cmap
);
65 GdkColormap
*m_colormap
;
68 friend class wxColour
;
70 // reference counter for systems with <= 8-Bit display
71 static gushort colMapAllocCounter
[ 256 ];
74 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 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
91 void wxColourRefData::FreeColour()
96 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
97 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
99 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
100 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
101 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
104 int idx
= m_color
.pixel
;
105 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
107 if (colMapAllocCounter
[ idx
] == 0)
108 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
113 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
115 if (m_hasPixel
&& (m_colormap
== cmap
))
121 if ( (cmap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
122 (cmap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
) )
124 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
125 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
126 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
129 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
130 int idx
= m_color
.pixel
;
131 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
135 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
140 //-----------------------------------------------------------------------------
142 #define M_COLDATA ((wxColourRefData *)m_refData)
144 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
146 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
148 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
150 m_refData
= new wxColourRefData();
151 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
152 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
153 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
154 M_COLDATA
->m_color
.pixel
= 0;
157 void wxColour::InitFromName( const wxString
&colourName
)
159 wxNode
*node
= (wxNode
*) NULL
;
160 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
162 wxColour
*col
= (wxColour
*)node
->Data();
164 if (col
) Ref( *col
);
168 m_refData
= new wxColourRefData();
170 if (!gdk_color_parse( colourName
.mb_str(), &M_COLDATA
->m_color
))
172 // VZ: asserts are good in general but this one is triggered by
173 // calling wxColourDatabase::FindColour() with an
174 // unrecognized colour name and this can't be avoided from the
175 // user code, so don't give it here
177 // a better solution would be to changed code in FindColour()
179 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
182 m_refData
= (wxObjectRefData
*) NULL
;
187 wxColour::~wxColour()
191 bool wxColour::operator == ( const wxColour
& col
) const
193 if (m_refData
== col
.m_refData
) return TRUE
;
195 if (!m_refData
|| !col
.m_refData
) return FALSE
;
197 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
198 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
199 if (own
->red
!= other
->red
) return FALSE
;
200 if (own
->blue
!= other
->blue
) return FALSE
;
201 if (own
->green
!= other
->green
) return FALSE
;
206 wxObjectRefData
*wxColour::CreateRefData() const
208 return new wxColourRefData
;
211 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
213 return new wxColourRefData(*(wxColourRefData
*)data
);
216 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
220 m_refData
= new wxColourRefData();
221 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
222 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
223 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
224 M_COLDATA
->m_color
.pixel
= 0;
227 unsigned char wxColour::Red() const
229 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
231 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
234 unsigned char wxColour::Green() const
236 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
238 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
241 unsigned char wxColour::Blue() const
243 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
245 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
248 void wxColour::CalcPixel( GdkColormap
*cmap
)
252 M_COLDATA
->AllocColour( cmap
);
255 int wxColour::GetPixel() const
257 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
259 return M_COLDATA
->m_color
.pixel
;
262 GdkColor
*wxColour::GetColor() const
264 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
266 return &M_COLDATA
->m_color
;