]>
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"
17 #include "gdk/gdkprivate.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 class wxColourRefData
: public wxObjectRefData
32 GdkColormap
*m_colormap
;
38 wxColourRefData::wxColourRefData()
44 m_colormap
= (GdkColormap
*) NULL
;
48 wxColourRefData::~wxColourRefData()
53 void wxColourRefData::FreeColour()
55 // if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 );
58 //-----------------------------------------------------------------------------
60 #define M_COLDATA ((wxColourRefData *)m_refData)
62 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
64 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
70 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
72 m_refData
= new wxColourRefData();
73 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
74 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
75 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
76 M_COLDATA
->m_color
.pixel
= 0;
79 void wxColour::InitFromName( const wxString
&colourName
)
81 wxNode
*node
= (wxNode
*) NULL
;
82 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
84 wxColour
*col
= (wxColour
*)node
->Data();
90 m_refData
= new wxColourRefData();
91 if (!gdk_color_parse( colourName
, &M_COLDATA
->m_color
))
93 wxFAIL_MSG( "wxColour: couldn't find colour" );
95 m_refData
= (wxObjectRefData
*) NULL
;
100 wxColour::wxColour( const wxColour
& col
)
105 wxColour::wxColour( const wxColour
* col
)
107 if (col
) Ref( *col
);
110 wxColour::~wxColour()
114 wxColour
& wxColour::operator = ( const wxColour
& col
)
116 if (*this == col
) return (*this);
121 bool wxColour::operator == ( const wxColour
& col
)
123 return m_refData
== col
.m_refData
;
126 bool wxColour::operator != ( const wxColour
& col
)
128 return m_refData
!= col
.m_refData
;
131 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
134 m_refData
= new wxColourRefData();
135 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
136 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
137 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
138 M_COLDATA
->m_color
.pixel
= 0;
141 unsigned char wxColour::Red() const
145 wxFAIL_MSG( "invalid colour" );
149 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
152 unsigned char wxColour::Green() const
156 wxFAIL_MSG( "invalid colour" );
160 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
163 unsigned char wxColour::Blue() const
167 wxFAIL_MSG( "invalid colour" );
171 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
174 bool wxColour::Ok() const
176 return (m_refData
!= NULL
);
179 void wxColour::CalcPixel( GdkColormap
*cmap
)
183 if ((M_COLDATA
->m_hasPixel
) && (M_COLDATA
->m_colormap
== cmap
)) return;
184 M_COLDATA
->FreeColour();
186 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
187 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
188 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
190 GdkColor
*colors
= cmap
->colors
;
191 int max
= 3 * (65536);
194 for (int i
= 0; i
< cmap
->size
; i
++)
196 int rdiff
= (M_COLDATA
->m_color
.red
- colors
[i
].red
);
197 int gdiff
= (M_COLDATA
->m_color
.green
- colors
[i
].green
);
198 int bdiff
= (M_COLDATA
->m_color
.blue
- colors
[i
].blue
);
199 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
200 if (sum
< max
) { index
= i
; max
= sum
; }
203 M_COLDATA
->m_hasPixel
= TRUE
;
204 M_COLDATA
->m_color
.pixel
= index
;
208 M_COLDATA
->m_hasPixel
= gdk_color_alloc( cmap
, &M_COLDATA
->m_color
);
211 M_COLDATA
->m_colormap
= cmap
;
214 int wxColour::GetPixel() const
218 return M_COLDATA
->m_color
.pixel
;
221 GdkColor
*wxColour::GetColor() const
223 if (!Ok()) return (GdkColor
*) NULL
;
225 return &M_COLDATA
->m_color
;