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"
16 #include "wx/colour.h"
17 #include "wx/gtk/private.h"
21 #include <gdk/gdkprivate.h>
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 class wxColourRefData
: public wxObjectRefData
36 m_colormap
= (GdkColormap
*) NULL
;
40 wxColourRefData(const wxColourRefData
& data
)
43 m_color
= data
.m_color
;
44 m_colormap
= data
.m_colormap
;
45 m_hasPixel
= data
.m_hasPixel
;
53 bool operator == (const wxColourRefData
& data
) const
55 return (m_colormap
== data
.m_colormap
&&
56 m_hasPixel
== data
.m_hasPixel
&&
57 m_color
.red
== data
.m_color
.red
&&
58 m_color
.green
== data
.m_color
.green
&&
59 m_color
.blue
== data
.m_color
.blue
&&
60 m_color
.pixel
== data
.m_color
.pixel
);
64 void AllocColour( GdkColormap
* cmap
);
67 GdkColormap
*m_colormap
;
70 friend class wxColour
;
72 // reference counter for systems with <= 8-Bit display
73 static gushort colMapAllocCounter
[ 256 ];
76 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
93 void wxColourRefData::FreeColour()
98 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
99 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
101 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
102 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
103 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
106 int idx
= m_color
.pixel
;
107 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
109 if (colMapAllocCounter
[ idx
] == 0)
110 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
115 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
117 if (m_hasPixel
&& (m_colormap
== cmap
))
123 if ( (cmap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
124 (cmap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
) )
126 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
127 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
128 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
131 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
132 int idx
= m_color
.pixel
;
133 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
137 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
142 //-----------------------------------------------------------------------------
144 #define M_COLDATA ((wxColourRefData *)m_refData)
146 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
148 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
150 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
152 m_refData
= new wxColourRefData();
153 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
154 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
155 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
156 M_COLDATA
->m_color
.pixel
= 0;
159 void wxColour::InitFromName( const wxString
&colourName
)
161 wxNode
*node
= (wxNode
*) NULL
;
162 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
164 wxColour
*col
= (wxColour
*)node
->GetData();
166 if (col
) Ref( *col
);
170 m_refData
= new wxColourRefData();
172 if (!gdk_color_parse( wxGTK_CONV( colourName
), &M_COLDATA
->m_color
))
174 // VZ: asserts are good in general but this one is triggered by
175 // calling wxColourDatabase::FindColour() with an
176 // unrecognized colour name and this can't be avoided from the
177 // user code, so don't give it here
179 // a better solution would be to changed code in FindColour()
181 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
184 m_refData
= (wxObjectRefData
*) NULL
;
189 wxColour::~wxColour()
193 bool wxColour::operator == ( const wxColour
& col
) const
195 if (m_refData
== col
.m_refData
) return TRUE
;
197 if (!m_refData
|| !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 wxObjectRefData
*wxColour::CreateRefData() const
210 return new wxColourRefData
;
213 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
215 return new wxColourRefData(*(wxColourRefData
*)data
);
218 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
222 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
223 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
224 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
225 M_COLDATA
->m_color
.pixel
= 0;
227 M_COLDATA
->m_colormap
= (GdkColormap
*) NULL
;
228 M_COLDATA
->m_hasPixel
= FALSE
;
231 unsigned char wxColour::Red() const
233 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
235 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
238 unsigned char wxColour::Green() const
240 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
242 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
245 unsigned char wxColour::Blue() const
247 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
249 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
252 void wxColour::CalcPixel( GdkColormap
*cmap
)
256 M_COLDATA
->AllocColour( cmap
);
259 int wxColour::GetPixel() const
261 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
263 return M_COLDATA
->m_color
.pixel
;
266 GdkColor
*wxColour::GetColor() const
268 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
270 return &M_COLDATA
->m_color
;