]>
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 /////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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_visualInfo
->m_visualType
== GrayScale
) ||
97 (wxTheApp
->m_visualInfo
->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 );
111 void wxColourRefData::AllocColour( WXColormap cmap
)
113 if (m_hasPixel
&& (m_colormap
== cmap
))
119 if ((wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
) ||
120 (wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
))
122 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
123 int idx
= m_color
.pixel
;
124 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
129 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
135 //-----------------------------------------------------------------------------
137 #define M_COLDATA ((wxColourRefData *)m_refData)
139 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
141 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
143 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
145 m_refData
= new wxColourRefData();
147 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
148 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
149 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
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
;
155 M_COLDATA
->m_color
.pixel
= 0;
158 void wxColour::InitFromName( const wxString
&colourName
)
161 if ( (wxTheColourDatabase
) && (col
= wxTheColourDatabase
->FindColourNoAdd(colourName
)) )
164 if (col
) Ref( *col
);
168 m_refData
= new wxColourRefData();
170 M_COLDATA
->m_colormap
= wxTheApp
->GetMainColormap( wxGlobalDisplay() );
172 if (!XParseColor( wxGlobalDisplay(), (Colormap
) M_COLDATA
->m_colormap
, colourName
.mb_str(), &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 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
200 XColor
*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_refData
= new wxColourRefData();
224 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
225 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
226 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
228 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
229 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
230 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
232 M_COLDATA
->m_color
.pixel
= 0;
235 unsigned char wxColour::Red() const
237 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
240 return (unsigned char) M_COLDATA
->m_color
.red
;
242 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
246 unsigned char wxColour::Green() const
248 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
251 return (unsigned char) M_COLDATA
->m_color
.green
;
253 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
257 unsigned char wxColour::Blue() const
259 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
262 return (unsigned char) M_COLDATA
->m_color
.blue
;
264 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
268 void wxColour::CalcPixel( WXColormap cmap
)
270 wxCHECK_RET( Ok(), wxT("invalid colour") );
272 wxCHECK_RET( cmap
, wxT("invalid colormap") );
274 M_COLDATA
->AllocColour( cmap
);
277 unsigned long wxColour::GetPixel() const
279 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
281 return M_COLDATA
->m_color
.pixel
;
284 WXColor
*wxColour::GetColor() const
286 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
288 return (WXColor
*) &M_COLDATA
->m_color
;