]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "colour.h"
15 #include "wx/gdicmn.h"
19 #include <gdk/gdkprivate.h>
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 class wxColourRefData
: public wxObjectRefData
34 GdkColormap
*m_colormap
;
37 friend class wxColour
;
40 wxColourRefData::wxColourRefData()
46 m_colormap
= (GdkColormap
*) NULL
;
50 wxColourRefData::~wxColourRefData()
55 void wxColourRefData::FreeColour()
57 // if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 );
60 //-----------------------------------------------------------------------------
62 #define M_COLDATA ((wxColourRefData *)m_refData)
64 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
66 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
72 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
74 m_refData
= new wxColourRefData();
75 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
76 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
77 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
78 M_COLDATA
->m_color
.pixel
= 0;
81 void wxColour::InitFromName( const wxString
&colourName
)
83 wxNode
*node
= (wxNode
*) NULL
;
84 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
86 wxColour
*col
= (wxColour
*)node
->Data();
92 m_refData
= new wxColourRefData();
93 if (!gdk_color_parse( colourName
.mb_str(), &M_COLDATA
->m_color
))
95 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
98 m_refData
= (wxObjectRefData
*) NULL
;
103 wxColour::wxColour( const wxColour
& col
)
108 wxColour::~wxColour()
112 wxColour
& wxColour::operator = ( const wxColour
& col
)
114 if (*this == col
) return (*this);
119 bool wxColour::operator == ( const wxColour
& col
) const
121 if (m_refData
== col
.m_refData
) return TRUE
;
123 if (!m_refData
) return FALSE
;
124 if (!col
.m_refData
) return FALSE
;
126 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
127 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
128 if (own
->red
!= other
->red
) return FALSE
;
129 if (own
->blue
!= other
->blue
) return FALSE
;
130 if (own
->green
!= other
->green
) return FALSE
;
135 bool wxColour::operator != ( const wxColour
& col
) const
137 return !(*this == col
);
140 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
143 m_refData
= new wxColourRefData();
144 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
145 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
146 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
147 M_COLDATA
->m_color
.pixel
= 0;
150 unsigned char wxColour::Red() const
152 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
154 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
157 unsigned char wxColour::Green() const
159 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
161 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
164 unsigned char wxColour::Blue() const
166 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
168 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
171 bool wxColour::Ok() const
173 return (m_refData
!= NULL
);
176 void wxColour::CalcPixel( GdkColormap
*cmap
)
180 if ((M_COLDATA
->m_hasPixel
) && (M_COLDATA
->m_colormap
== cmap
)) return;
182 M_COLDATA
->FreeColour();
185 if ((cmap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
186 (cmap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
188 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
189 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
190 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
193 GdkColor
*colors
= cmap
->colors
;
194 int max
= 3 * (65536);
197 for (int i
= 0; i
< cmap
->size
; i
++)
199 int rdiff
= (M_COLDATA
->m_color
.red
- colors
[i
].red
);
200 int gdiff
= (M_COLDATA
->m_color
.green
- colors
[i
].green
);
201 int bdiff
= (M_COLDATA
->m_color
.blue
- colors
[i
].blue
);
202 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
203 if (sum
< max
) { index
= i
; max
= sum
; }
206 M_COLDATA
->m_hasPixel
= TRUE
;
207 M_COLDATA
->m_color
.pixel
= index
;
211 M_COLDATA
->m_hasPixel
= gdk_color_alloc( cmap
, &M_COLDATA
->m_color
);
214 M_COLDATA
->m_colormap
= cmap
;
217 int wxColour::GetPixel() const
219 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
221 return M_COLDATA
->m_color
.pixel
;
224 GdkColor
*wxColour::GetColor() const
226 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
228 return &M_COLDATA
->m_color
;