]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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"
18 #include "gdk/gdkprivate.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 class wxColourRefData
: public wxObjectRefData
33 GdkColormap
*m_colormap
;
39 wxColourRefData::wxColourRefData()
45 m_colormap
= (GdkColormap
*) NULL
;
49 wxColourRefData::~wxColourRefData()
54 void wxColourRefData::FreeColour()
56 // if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 );
59 //-----------------------------------------------------------------------------
61 #define M_COLDATA ((wxColourRefData *)m_refData)
63 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
65 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
71 wxColour::wxColour( char red
, char green
, char blue
)
73 m_refData
= new wxColourRefData();
74 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
75 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
76 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
77 M_COLDATA
->m_color
.pixel
= 0;
80 void wxColour::InitFromName( const wxString
&colourName
)
82 wxNode
*node
= (wxNode
*) NULL
;
83 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
85 wxColour
*col
= (wxColour
*)node
->Data();
91 m_refData
= new wxColourRefData();
92 if (!gdk_color_parse( colourName
, &M_COLDATA
->m_color
))
94 wxFAIL_MSG( "wxColour: couldn't find colour" );
96 m_refData
= (wxObjectRefData
*) NULL
;
101 wxColour::wxColour( const wxColour
& col
)
106 wxColour::wxColour( const wxColour
* col
)
108 if (col
) Ref( *col
);
111 wxColour::~wxColour()
115 wxColour
& wxColour::operator = ( const wxColour
& col
)
117 if (*this == col
) return (*this);
122 bool wxColour::operator == ( const wxColour
& col
)
124 return m_refData
== col
.m_refData
;
127 bool wxColour::operator != ( const wxColour
& col
)
129 return m_refData
!= col
.m_refData
;
132 void wxColour::Set( const unsigned char red
, const unsigned char green
, const unsigned char blue
)
135 m_refData
= new wxColourRefData();
136 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
137 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
138 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
139 M_COLDATA
->m_color
.pixel
= 0;
142 unsigned char wxColour::Red() const
146 wxFAIL_MSG( "invalid colour" );
150 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
153 unsigned char wxColour::Green() const
157 wxFAIL_MSG( "invalid colour" );
161 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
164 unsigned char wxColour::Blue() const
168 wxFAIL_MSG( "invalid colour" );
172 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
175 bool wxColour::Ok() const
177 return (m_refData
!= NULL
);
180 void wxColour::CalcPixel( GdkColormap
*cmap
)
184 if ((M_COLDATA
->m_hasPixel
) && (M_COLDATA
->m_colormap
== cmap
)) return;
185 M_COLDATA
->FreeColour();
187 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
188 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
189 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
191 GdkColor
*colors
= cmap
->colors
;
192 int max
= 3 * (65536);
195 for (int i
= 0; i
< cmap
->size
; i
++)
197 int rdiff
= (M_COLDATA
->m_color
.red
- colors
[i
].red
);
198 int gdiff
= (M_COLDATA
->m_color
.green
- colors
[i
].green
);
199 int bdiff
= (M_COLDATA
->m_color
.blue
- colors
[i
].blue
);
200 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
201 if (sum
< max
) { index
= i
; max
= sum
; }
204 M_COLDATA
->m_hasPixel
= TRUE
;
205 M_COLDATA
->m_color
.pixel
= index
;
209 M_COLDATA
->m_hasPixel
= gdk_color_alloc( cmap
, &M_COLDATA
->m_color
);
212 M_COLDATA
->m_colormap
= cmap
;
215 int wxColour::GetPixel() const
219 return M_COLDATA
->m_color
.pixel
;
222 GdkColor
*wxColour::GetColor() const
224 if (!Ok()) return (GdkColor
*) NULL
;
226 return &M_COLDATA
->m_color
;