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
)
40 m_color
= data
.m_color
;
41 m_colormap
= data
.m_colormap
;
42 m_hasPixel
= data
.m_hasPixel
;
50 bool operator == (const wxColourRefData
& data
) const
52 return (m_colormap
== data
.m_colormap
&&
53 m_hasPixel
== data
.m_hasPixel
&&
54 m_color
.red
== data
.m_color
.red
&&
55 m_color
.green
== data
.m_color
.green
&&
56 m_color
.blue
== data
.m_color
.blue
&&
57 m_color
.pixel
== data
.m_color
.pixel
);
61 void AllocColour( GdkColormap
* cmap
);
64 GdkColormap
*m_colormap
;
67 friend class wxColour
;
69 // reference counter for systems with <= 8-Bit display
70 static gushort colMapAllocCounter
[ 256 ];
73 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 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
90 void wxColourRefData::FreeColour()
95 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
96 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
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
))
103 int idx
= m_color
.pixel
;
104 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
106 if (colMapAllocCounter
[ idx
] == 0)
107 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
112 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
114 if (m_hasPixel
&& (m_colormap
== cmap
))
120 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
121 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
123 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
124 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
125 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
128 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
129 int idx
= m_color
.pixel
;
130 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
134 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
139 //-----------------------------------------------------------------------------
141 #define M_COLDATA ((wxColourRefData *)m_refData)
143 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
145 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
147 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
149 m_refData
= new wxColourRefData();
150 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
151 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
152 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
153 M_COLDATA
->m_color
.pixel
= 0;
156 void wxColour::InitFromName( const wxString
&colourName
)
158 wxNode
*node
= (wxNode
*) NULL
;
159 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
161 wxColour
*col
= (wxColour
*)node
->Data();
163 if (col
) Ref( *col
);
167 m_refData
= new wxColourRefData();
169 if (!gdk_color_parse( colourName
.mb_str(), &M_COLDATA
->m_color
))
171 // VZ: asserts are good in general but this one is triggered by
172 // calling wxColourDatabase::FindColour() with an
173 // unrecognized colour name and this can't be avoided from the
174 // user code, so don't give it here
176 // a better solution would be to changed code in FindColour()
178 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
181 m_refData
= (wxObjectRefData
*) NULL
;
186 wxColour::~wxColour()
190 bool wxColour::operator == ( const wxColour
& col
) const
192 if (m_refData
== col
.m_refData
) return TRUE
;
194 if (!m_refData
|| !col
.m_refData
) return FALSE
;
196 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
197 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
198 if (own
->red
!= other
->red
) return FALSE
;
199 if (own
->blue
!= other
->blue
) return FALSE
;
200 if (own
->green
!= other
->green
) return FALSE
;
205 wxObjectRefData
*wxColour::CreateRefData() const
207 return new wxColourRefData
;
210 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
212 return new wxColourRefData(*(wxColourRefData
*)data
);
215 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
219 m_refData
= new wxColourRefData();
220 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
221 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
222 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
223 M_COLDATA
->m_color
.pixel
= 0;
226 unsigned char wxColour::Red() const
228 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
230 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
233 unsigned char wxColour::Green() const
235 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
237 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
240 unsigned char wxColour::Blue() const
242 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
244 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
247 void wxColour::CalcPixel( GdkColormap
*cmap
)
251 M_COLDATA
->AllocColour( cmap
);
254 int wxColour::GetPixel() const
256 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
258 return M_COLDATA
->m_color
.pixel
;
261 GdkColor
*wxColour::GetColor() const
263 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
265 return &M_COLDATA
->m_color
;