]>
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
;
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( WXColormap cmap
);
65 WXColormap m_colormap
;
68 friend class wxColour
;
70 // reference counter for systems with <= 8-Bit display
71 static unsigned short colMapAllocCounter
[ 256 ];
74 unsigned short 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 ((wxTheApp
->m_visualType
== GrayScale
) ||
97 (wxTheApp
->m_visualType
== PseudoColor
))
99 int idx
= m_color
.pixel
;
100 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
102 if (colMapAllocCounter
[ idx
] == 0)
104 unsigned long pixel
= m_color
.pixel
;
105 XFreeColors( wxGlobalDisplay(), (Colormap
) m_colormap
, &pixel
, 1, 0 );
110 void wxColourRefData::AllocColour( WXColormap cmap
)
112 if (m_hasPixel
&& (m_colormap
== cmap
))
117 if ((wxTheApp
->m_visualType
== GrayScale
) ||
118 (wxTheApp
->m_visualType
== PseudoColor
))
120 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
121 int idx
= m_color
.pixel
;
122 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
126 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) 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();
144 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
145 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
146 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
148 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
149 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
150 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
152 M_COLDATA
->m_color
.pixel
= 0;
155 void wxColour::InitFromName( const wxString
&colourName
)
157 wxNode
*node
= (wxNode
*) NULL
;
158 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
160 wxColour
*col
= (wxColour
*)node
->Data();
162 if (col
) Ref( *col
);
166 m_refData
= new wxColourRefData();
168 M_COLDATA
->m_colormap
= wxTheApp
->GetMainColormap( wxGlobalDisplay() );
170 if (!XParseColor( wxGlobalDisplay(), (Colormap
) M_COLDATA
->m_colormap
, 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 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
198 XColor
*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();
222 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
223 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
224 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
226 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
227 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
228 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
230 M_COLDATA
->m_color
.pixel
= 0;
233 unsigned char wxColour::Red() const
235 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
238 return (unsigned char) M_COLDATA
->m_color
.red
;
240 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
244 unsigned char wxColour::Green() const
246 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
249 return (unsigned char) M_COLDATA
->m_color
.green
;
251 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
255 unsigned char wxColour::Blue() const
257 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
260 return (unsigned char) M_COLDATA
->m_color
.blue
;
262 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
266 void wxColour::CalcPixel( WXColormap cmap
)
268 wxCHECK_RET( Ok(), wxT("invalid colour") );
270 wxCHECK_RET( cmap
, wxT("invalid colormap") );
272 M_COLDATA
->AllocColour( cmap
);
275 unsigned long wxColour::GetPixel() const
277 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
279 return M_COLDATA
->m_color
.pixel
;
282 WXColor
*wxColour::GetColor() const
284 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
286 return (WXColor
*) &M_COLDATA
->m_color
;