]>
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"
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( unsigned char red
, unsigned char green
, unsigned 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" );
95 printf( "Colourname %s.\n", WXSTRINGCAST colourName
);
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 return m_refData
== col
.m_refData
;
124 bool wxColour::operator != ( const wxColour
& col
) const
126 return m_refData
!= col
.m_refData
;
129 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
132 m_refData
= new wxColourRefData();
133 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
134 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
135 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
136 M_COLDATA
->m_color
.pixel
= 0;
139 unsigned char wxColour::Red() const
141 wxCHECK_MSG( Ok(), 0, "invalid colour" );
143 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
146 unsigned char wxColour::Green() const
148 wxCHECK_MSG( Ok(), 0, "invalid colour" );
150 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
153 unsigned char wxColour::Blue() const
155 wxCHECK_MSG( Ok(), 0, "invalid colour" );
157 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
160 bool wxColour::Ok() const
162 return (m_refData
!= NULL
);
165 void wxColour::CalcPixel( GdkColormap
*cmap
)
169 if ((M_COLDATA
->m_hasPixel
) && (M_COLDATA
->m_colormap
== cmap
)) return;
171 M_COLDATA
->FreeColour();
173 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
174 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
175 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
177 GdkColor
*colors
= cmap
->colors
;
178 int max
= 3 * (65536);
181 for (int i
= 0; i
< cmap
->size
; i
++)
183 int rdiff
= (M_COLDATA
->m_color
.red
- colors
[i
].red
);
184 int gdiff
= (M_COLDATA
->m_color
.green
- colors
[i
].green
);
185 int bdiff
= (M_COLDATA
->m_color
.blue
- colors
[i
].blue
);
186 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
187 if (sum
< max
) { index
= i
; max
= sum
; }
190 M_COLDATA
->m_hasPixel
= TRUE
;
191 M_COLDATA
->m_color
.pixel
= index
;
195 M_COLDATA
->m_hasPixel
= gdk_color_alloc( cmap
, &M_COLDATA
->m_color
);
198 M_COLDATA
->m_colormap
= cmap
;
201 int wxColour::GetPixel() const
203 wxCHECK_MSG( Ok(), 0, "invalid colour" );
205 return M_COLDATA
->m_color
.pixel
;
208 GdkColor
*wxColour::GetColor() const
210 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, "invalid colour" );
212 return &M_COLDATA
->m_color
;