]>
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"
19 #include "wx/x11/private.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 class wxColourRefData
: public wxObjectRefData
34 m_colormap
= (WXColormap
*) NULL
;
43 bool operator == (const wxColourRefData
& data
) const
45 return (m_colormap
== data
.m_colormap
&&
46 m_hasPixel
== data
.m_hasPixel
&&
47 m_color
.red
== data
.m_color
.red
&&
48 m_color
.green
== data
.m_color
.green
&&
49 m_color
.blue
== data
.m_color
.blue
&&
50 m_color
.pixel
== data
.m_color
.pixel
);
54 void AllocColour( WXColormap cmap
);
57 WXColormap m_colormap
;
60 friend class wxColour
;
62 // reference counter for systems with <= 8-Bit display
63 static unsigned short colMapAllocCounter
[ 256 ];
66 unsigned short wxColourRefData::colMapAllocCounter
[ 256 ] =
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
83 void wxColourRefData::FreeColour()
88 Colormap cm
= (Colormap
)m_colormap
;
90 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
91 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
92 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
94 int idx
= m_color
.pixel
;
95 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
97 if (colMapAllocCounter
[ idx
] == 0)
98 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
104 void wxColourRefData::AllocColour( WXColormap cmap
)
106 if (m_hasPixel
&& (m_colormap
== cmap
))
112 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
113 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
114 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
116 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
117 int idx
= m_color
.pixel
;
118 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
123 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
128 //-----------------------------------------------------------------------------
130 #define M_COLDATA ((wxColourRefData *)m_refData)
132 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
134 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
136 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
138 m_refData
= new wxColourRefData();
139 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
140 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
141 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
142 M_COLDATA
->m_color
.pixel
= 0;
145 void wxColour::InitFromName( const wxString
&colourName
)
147 wxNode
*node
= (wxNode
*) NULL
;
148 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
150 wxColour
*col
= (wxColour
*)node
->Data();
152 if (col
) Ref( *col
);
156 m_refData
= new wxColourRefData();
157 if (!XParseColor( wxGlobalDisplay(), (Colormap
) M_COLDATA
->m_colormap
, colourName
.mb_str(), &M_COLDATA
->m_color
))
159 // VZ: asserts are good in general but this one is triggered by
160 // calling wxColourDatabase::FindColour() with an
161 // unrecognized colour name and this can't be avoided from the
162 // user code, so don't give it here
164 // a better solution would be to changed code in FindColour()
166 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
169 m_refData
= (wxObjectRefData
*) NULL
;
174 wxColour::~wxColour()
178 bool wxColour::operator == ( const wxColour
& col
) const
180 if (m_refData
== col
.m_refData
) return TRUE
;
182 if (!m_refData
|| !col
.m_refData
) return FALSE
;
184 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
185 XColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
186 if (own
->red
!= other
->red
) return FALSE
;
187 if (own
->blue
!= other
->blue
) return FALSE
;
188 if (own
->green
!= other
->green
) return FALSE
;
193 wxObjectRefData
*wxColour::CreateRefData() const
195 return new wxColourRefData
;
198 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
200 return new wxColourRefData(*(wxColourRefData
*)data
);
203 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
207 m_refData
= new wxColourRefData();
208 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
209 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
210 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
211 M_COLDATA
->m_color
.pixel
= 0;
214 unsigned char wxColour::Red() const
216 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
218 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
221 unsigned char wxColour::Green() const
223 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
225 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
228 unsigned char wxColour::Blue() const
230 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
232 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
235 void wxColour::CalcPixel( WXColormap cmap
)
239 M_COLDATA
->AllocColour( cmap
);
242 unsigned long wxColour::GetPixel() const
244 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
246 return M_COLDATA
->m_color
.pixel
;
249 WXColor
*wxColour::GetColor() const
251 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
253 return (WXColor
*) &M_COLDATA
->m_color
;