]>
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 Colormap cm
= (Colormap
)m_colormap
;
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
))
102 int idx
= m_color
.pixel
;
103 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
105 if (colMapAllocCounter
[ idx
] == 0)
106 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
112 void wxColourRefData::AllocColour( WXColormap cmap
)
114 if (m_hasPixel
&& (m_colormap
== cmap
))
120 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
121 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
122 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
124 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
125 int idx
= m_color
.pixel
;
126 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
131 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
136 //-----------------------------------------------------------------------------
138 #define M_COLDATA ((wxColourRefData *)m_refData)
140 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
142 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
144 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
146 m_refData
= new wxColourRefData();
148 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
149 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
150 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
152 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
153 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
154 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
->Data();
166 if (col
) Ref( *col
);
170 m_refData
= new wxColourRefData();
172 M_COLDATA
->m_colormap
= wxTheApp
->GetMainColormap( wxGlobalDisplay() );
174 if (!XParseColor( wxGlobalDisplay(), (Colormap
) M_COLDATA
->m_colormap
, colourName
.mb_str(), &M_COLDATA
->m_color
))
176 // VZ: asserts are good in general but this one is triggered by
177 // calling wxColourDatabase::FindColour() with an
178 // unrecognized colour name and this can't be avoided from the
179 // user code, so don't give it here
181 // a better solution would be to changed code in FindColour()
183 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
186 m_refData
= (wxObjectRefData
*) NULL
;
191 wxColour::~wxColour()
195 bool wxColour::operator == ( const wxColour
& col
) const
197 if (m_refData
== col
.m_refData
) return TRUE
;
199 if (!m_refData
|| !col
.m_refData
) return FALSE
;
201 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
202 XColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
203 if (own
->red
!= other
->red
) return FALSE
;
204 if (own
->blue
!= other
->blue
) return FALSE
;
205 if (own
->green
!= other
->green
) return FALSE
;
210 wxObjectRefData
*wxColour::CreateRefData() const
212 return new wxColourRefData
;
215 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
217 return new wxColourRefData(*(wxColourRefData
*)data
);
220 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
224 m_refData
= new wxColourRefData();
226 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
227 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
228 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
230 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
231 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
232 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
234 M_COLDATA
->m_color
.pixel
= 0;
237 unsigned char wxColour::Red() const
239 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
242 return (unsigned char) M_COLDATA
->m_color
.red
;
244 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
248 unsigned char wxColour::Green() const
250 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
253 return (unsigned char) M_COLDATA
->m_color
.green
;
255 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
259 unsigned char wxColour::Blue() const
261 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
264 return (unsigned char) M_COLDATA
->m_color
.blue
;
266 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
270 void wxColour::CalcPixel( WXColormap cmap
)
272 wxCHECK_RET( Ok(), wxT("invalid colour") );
274 wxCHECK_RET( cmap
, wxT("invalid colormap") );
276 M_COLDATA
->AllocColour( cmap
);
279 unsigned long wxColour::GetPixel() const
281 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
283 return M_COLDATA
->m_color
.pixel
;
286 WXColor
*wxColour::GetColor() const
288 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
290 return (WXColor
*) &M_COLDATA
->m_color
;