]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxColour class
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "colour.h"
17 #include "wx/gdicmn.h"
20 #include "wx/x11/private.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 class wxColourRefData
: public wxObjectRefData
35 m_colormap
= (WXColormap
*) NULL
;
44 bool operator == (const wxColourRefData
& data
) const
46 return (m_colormap
== data
.m_colormap
&&
47 m_hasPixel
== data
.m_hasPixel
&&
48 m_color
.red
== data
.m_color
.red
&&
49 m_color
.green
== data
.m_color
.green
&&
50 m_color
.blue
== data
.m_color
.blue
&&
51 m_color
.pixel
== data
.m_color
.pixel
);
55 void AllocColour( WXColormap cmap
);
58 WXColormap m_colormap
;
61 friend class wxColour
;
63 // reference counter for systems with <= 8-Bit display
64 static unsigned short colMapAllocCounter
[ 256 ];
67 unsigned short wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
84 void wxColourRefData::FreeColour()
89 Colormap cm
= (Colormap
)m_colormap
;
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
))
95 int idx
= m_color
.pixel
;
96 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
98 if (colMapAllocCounter
[ idx
] == 0)
99 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
105 void wxColourRefData::AllocColour( WXColormap cmap
)
107 if (m_hasPixel
&& (m_colormap
== cmap
))
113 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
114 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
115 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
117 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
118 int idx
= m_color
.pixel
;
119 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
124 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
129 //-----------------------------------------------------------------------------
131 #define M_COLDATA ((wxColourRefData *)m_refData)
133 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
135 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
137 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
139 m_refData
= new wxColourRefData();
141 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
142 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
143 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
145 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
146 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
147 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
149 M_COLDATA
->m_color
.pixel
= 0;
152 void wxColour::InitFromName( const wxString
&colourName
)
154 wxNode
*node
= (wxNode
*) NULL
;
155 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
157 wxColour
*col
= (wxColour
*)node
->Data();
159 if (col
) Ref( *col
);
163 m_refData
= new wxColourRefData();
165 M_COLDATA
->m_colormap
= wxTheApp
->GetMainColormap( wxGlobalDisplay() );
167 if (!XParseColor( wxGlobalDisplay(), (Colormap
) M_COLDATA
->m_colormap
, colourName
.mb_str(), &M_COLDATA
->m_color
))
169 // VZ: asserts are good in general but this one is triggered by
170 // calling wxColourDatabase::FindColour() with an
171 // unrecognized colour name and this can't be avoided from the
172 // user code, so don't give it here
174 // a better solution would be to changed code in FindColour()
176 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
179 m_refData
= (wxObjectRefData
*) NULL
;
184 wxColour::~wxColour()
188 bool wxColour::operator == ( const wxColour
& col
) const
190 if (m_refData
== col
.m_refData
) return TRUE
;
192 if (!m_refData
|| !col
.m_refData
) return FALSE
;
194 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
195 XColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
196 if (own
->red
!= other
->red
) return FALSE
;
197 if (own
->blue
!= other
->blue
) return FALSE
;
198 if (own
->green
!= other
->green
) return FALSE
;
203 wxObjectRefData
*wxColour::CreateRefData() const
205 return new wxColourRefData
;
208 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
210 return new wxColourRefData(*(wxColourRefData
*)data
);
213 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
217 m_refData
= new wxColourRefData();
219 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
220 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
221 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
223 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
224 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
225 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
227 M_COLDATA
->m_color
.pixel
= 0;
230 unsigned char wxColour::Red() const
232 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
235 return (unsigned char) M_COLDATA
->m_color
.red
;
237 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
241 unsigned char wxColour::Green() const
243 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
246 return (unsigned char) M_COLDATA
->m_color
.green
;
248 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
252 unsigned char wxColour::Blue() const
254 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
257 return (unsigned char) M_COLDATA
->m_color
.blue
;
259 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
263 void wxColour::CalcPixel( WXColormap cmap
)
265 wxCHECK_RET( Ok(), wxT("invalid colour") );
267 wxCHECK_RET( cmap
, wxT("invalid colormap") );
269 M_COLDATA
->AllocColour( cmap
);
272 unsigned long wxColour::GetPixel() const
274 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
276 return M_COLDATA
->m_color
.pixel
;
279 WXColor
*wxColour::GetColor() const
281 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
283 return (WXColor
*) &M_COLDATA
->m_color
;