]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "colour.h"
16 #include "wx/gdicmn.h"
19 #include "../gdk_imlib/gdk_imlib.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 class wxColourRefData
: public wxObjectRefData
30 wxColourRefData(void);
31 ~wxColourRefData(void);
32 void FreeColour(void);
35 GdkColormap
*m_colormap
;
41 wxColourRefData::wxColourRefData(void)
47 m_colormap
= (GdkColormap
*) NULL
;
51 wxColourRefData::~wxColourRefData(void)
56 void wxColourRefData::FreeColour(void)
58 // if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 );
61 //-----------------------------------------------------------------------------
63 #define M_COLDATA ((wxColourRefData *)m_refData)
65 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
67 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
69 wxColour::wxColour(void)
73 wxColour::wxColour( char red
, char green
, char blue
)
75 m_refData
= new wxColourRefData();
76 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
77 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
78 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
79 M_COLDATA
->m_color
.pixel
= 0;
82 wxColour::wxColour( const wxString
&colourName
)
84 wxNode
*node
= (wxNode
*) NULL
;
85 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
87 wxColour
*col
= (wxColour
*)node
->Data();
93 m_refData
= new wxColourRefData();
94 if (!gdk_color_parse( colourName
, &M_COLDATA
->m_color
))
96 wxFAIL_MSG( "wxColour: couldn't find colour" );
98 m_refData
= (wxObjectRefData
*) NULL
;
103 wxColour::wxColour( const wxColour
& col
)
108 wxColour::wxColour( const wxColour
* col
)
110 if (col
) Ref( *col
);
113 wxColour::~wxColour(void)
117 wxColour
& wxColour::operator = ( const wxColour
& col
)
119 if (*this == col
) return (*this);
124 wxColour
& wxColour::operator = ( const wxString
& colourName
)
127 wxNode
*node
= (wxNode
*) NULL
;
128 if ((wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
130 wxColour
*col
= (wxColour
*)node
->Data();
131 if (col
) Ref( *col
);
135 m_refData
= new wxColourRefData();
136 if (!gdk_color_parse( colourName
, &M_COLDATA
->m_color
))
138 wxFAIL_MSG( "wxColour: couldn't find colour" );
140 m_refData
= (wxObjectRefData
*) NULL
;
146 bool wxColour::operator == ( const wxColour
& col
)
148 return m_refData
== col
.m_refData
;
151 bool wxColour::operator != ( const wxColour
& col
)
153 return m_refData
!= col
.m_refData
;
156 void wxColour::Set( const unsigned char red
, const unsigned char green
, const unsigned char blue
)
159 m_refData
= new wxColourRefData();
160 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
161 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
162 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
163 M_COLDATA
->m_color
.pixel
= 0;
166 unsigned char wxColour::Red(void) const
170 wxFAIL_MSG( "invalid colour" );
174 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
177 unsigned char wxColour::Green(void) const
181 wxFAIL_MSG( "invalid colour" );
185 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
188 unsigned char wxColour::Blue(void) const
192 wxFAIL_MSG( "invalid colour" );
196 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
199 bool wxColour::Ok(void) const
201 return (m_refData
!= NULL
);
204 void wxColour::CalcPixel( GdkColormap
*cmap
)
208 if ((M_COLDATA
->m_hasPixel
) && (M_COLDATA
->m_colormap
== cmap
)) return;
209 M_COLDATA
->FreeColour();
213 int r
= M_COLDATA
->m_color
.red
>> SHIFT
;
214 int g
= M_COLDATA
->m_color
.green
>> SHIFT
;
215 int b
= M_COLDATA
->m_color
.blue
>> SHIFT
;
216 M_COLDATA
->m_hasPixel
= TRUE
;
217 M_COLDATA
->m_color
.pixel
= gdk_imlib_best_color_match( &r
, &g
, &b
);
221 M_COLDATA
->m_hasPixel
= gdk_color_alloc( cmap
, &M_COLDATA
->m_color
);
225 M_COLDATA
->m_colormap
= cmap
;
228 int wxColour::GetPixel(void)
232 return M_COLDATA
->m_color
.pixel
;
235 GdkColor
*wxColour::GetColor(void)
237 if (!Ok()) return (GdkColor
*) NULL
;
239 return &M_COLDATA
->m_color
;