]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxColour class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 //// TODO: make wxColour a ref-counted object,
13 //// so pixel values get shared.
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma implementation "colour.h"
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #include "wx/gdicmn.h"
23 #include "wx/colour.h"
27 #pragma message disable nosimpint
31 #pragma message enable nosimpint
34 #include "wx/motif/private.h"
36 IMPLEMENT_DYNAMIC_CLASS(wxColour
, wxObject
)
54 wxColour::wxColour(const wxColour
& col
)
59 wxColour
& wxColour::operator =(const wxColour
& col
)
62 m_green
= col
.m_green
;
64 m_isInit
= col
.m_isInit
;
65 m_pixel
= col
.m_pixel
;
69 void wxColour::InitFromName(const wxString
& name
)
71 if ( wxTheColourDatabase
)
73 wxColour col
= wxTheColourDatabase
->Find(name
);
86 wxColour
wxColour::CreateByName(const wxString
& name
)
90 Display
*dpy
= wxGlobalDisplay();
91 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
93 if ( XParseColor( dpy
, (Colormap
)colormap
, name
.mb_str(), &xcol
) )
95 col
.m_red
= xcol
.red
& 0xff;
96 col
.m_green
= xcol
.green
& 0xff;
97 col
.m_blue
= xcol
.blue
& 0xff;
105 wxColour::~wxColour()
109 void wxColour::Set(unsigned char r
, unsigned char g
, unsigned char b
)
118 // Allocate a colour, or nearest colour, using the given display.
119 // If realloc is true, ignore the existing pixel, otherwise just return
121 // Returns the old or allocated pixel.
123 // TODO: can this handle mono displays? If not, we should have an extra
124 // flag to specify whether this should be black or white by default.
126 int wxColour::AllocColour(WXDisplay
* display
, bool realloc
)
128 if ((m_pixel
!= -1) && !realloc
)
132 color
.red
= (unsigned short) Red ();
133 color
.red
|= color
.red
<< 8;
134 color
.green
= (unsigned short) Green ();
135 color
.green
|= color
.green
<< 8;
136 color
.blue
= (unsigned short) Blue ();
137 color
.blue
|= color
.blue
<< 8;
139 color
.flags
= DoRed
| DoGreen
| DoBlue
;
141 WXColormap cmap
= wxTheApp
->GetMainColormap(display
);
143 if (!XAllocColor ((Display
*) display
, (Colormap
) cmap
, &color
))
145 m_pixel
= wxGetBestMatchingPixel((Display
*) display
, &color
,(Colormap
) cmap
);
150 m_pixel
= (int) color
.pixel
;
155 /*-------------------------------------------
156 Markus Emmenegger <mege@iqe.ethz.ch>
157 Find the pixel value with an assigned color closest to the desired color
158 Used if color cell allocation fails
159 As the returned pixel value may be in use by another application,
160 the color might change anytime.
161 But in many cases, that is still better than always using black.
163 Chris Breeze <chris@hel.co.uk>
165 1) More efficient calculation of RGB distance of colour cell from
166 the desired colour. There is no need to take the sqrt of 'dist', and
167 since we are only interested in the top 8-bits of R, G and B we
168 can perform integer arithmetic.
169 2) Attempt to allocate a read-only colour when a close match is found.
170 A read-only colour will not change.
171 3) Fall back to the closest match if no read-only colours are available.
173 Possible further improvements:
174 1) Scan the lookup table and sort the colour cells in order of
176 distance from the desired colour. Then attempt to allocate a
178 colour starting from the nearest match.
179 2) Linear RGB distance is not a particularly good method of colour
181 (though it is quick). Converting the colour to HLS and then comparing
182 may give better matching.
183 -------------------------------------------*/
185 int wxGetBestMatchingPixel(Display
*display
, XColor
*desiredColor
, Colormap cmap
)
187 if (cmap
== (Colormap
) NULL
)
188 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
190 int numPixVals
= XDisplayCells(display
, DefaultScreen (display
));
191 int mindist
= 256 * 256 * 3;
192 int bestpixel
= (int) BlackPixel (display
, DefaultScreen (display
));
193 int red
= desiredColor
->red
>> 8;
194 int green
= desiredColor
->green
>> 8;
195 int blue
= desiredColor
->blue
>> 8;
196 const int threshold
= 2 * 2 * 3; // allow an error of up to 2 in R,G & B
198 for (int pixelcount
= 0; pixelcount
< numPixVals
; pixelcount
++)
200 XColor matching_color
;
201 matching_color
.pixel
= pixelcount
;
202 XQueryColor(display
,cmap
,&matching_color
);
204 int delta_red
= red
- (matching_color
.red
>> 8);
205 int delta_green
= green
- (matching_color
.green
>> 8);
206 int delta_blue
= blue
- (matching_color
.blue
>> 8);
208 int dist
= delta_red
* delta_red
+
209 delta_green
* delta_green
+
210 delta_blue
* delta_blue
;
212 if (dist
<= threshold
)
214 // try to allocate a read-only colour...
215 if (XAllocColor (display
, cmap
, &matching_color
))
217 return matching_color
.pixel
;
222 bestpixel
= pixelcount
;