]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/colour.cpp
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 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
18 #include "wx/gdicmn.h"
19 #include "wx/colour.h"
23 #pragma message disable nosimpint
27 #pragma message enable nosimpint
30 #include "wx/motif/private.h"
32 IMPLEMENT_DYNAMIC_CLASS(wxColour
, wxObject
)
50 wxColour::wxColour(const wxColour
& col
)
55 wxColour
& wxColour::operator =(const wxColour
& col
)
58 m_green
= col
.m_green
;
60 m_isInit
= col
.m_isInit
;
61 m_pixel
= col
.m_pixel
;
65 void wxColour::InitFromName(const wxString
& name
)
67 if ( wxTheColourDatabase
)
69 wxColour col
= wxTheColourDatabase
->Find(name
);
82 wxColour
wxColour::CreateByName(const wxString
& name
)
86 Display
*dpy
= wxGlobalDisplay();
87 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
89 if ( XParseColor( dpy
, (Colormap
)colormap
, name
.mb_str(), &xcol
) )
91 col
.m_red
= (unsigned char)(xcol
.red
& 0xff);
92 col
.m_green
= (unsigned char)(xcol
.green
& 0xff);
93 col
.m_blue
= (unsigned char)(xcol
.blue
& 0xff);
101 wxColour::~wxColour()
105 void wxColour::Set(unsigned char r
, unsigned char g
, unsigned char b
)
114 // Allocate a colour, or nearest colour, using the given display.
115 // If realloc is true, ignore the existing pixel, otherwise just return
117 // Returns the old or allocated pixel.
119 // TODO: can this handle mono displays? If not, we should have an extra
120 // flag to specify whether this should be black or white by default.
122 int wxColour::AllocColour(WXDisplay
* display
, bool realloc
)
124 if ((m_pixel
!= -1) && !realloc
)
128 color
.red
= (unsigned short) Red ();
129 color
.red
|= (unsigned short)(color
.red
<< 8);
130 color
.green
= (unsigned short) Green ();
131 color
.green
|= (unsigned short)(color
.green
<< 8);
132 color
.blue
= (unsigned short) Blue ();
133 color
.blue
|= (unsigned short)(color
.blue
<< 8);
135 color
.flags
= DoRed
| DoGreen
| DoBlue
;
137 WXColormap cmap
= wxTheApp
->GetMainColormap(display
);
139 if (!XAllocColor ((Display
*) display
, (Colormap
) cmap
, &color
))
141 m_pixel
= wxGetBestMatchingPixel((Display
*) display
, &color
,(Colormap
) cmap
);
146 m_pixel
= (int) color
.pixel
;
151 /*-------------------------------------------
152 Markus Emmenegger <mege@iqe.ethz.ch>
153 Find the pixel value with an assigned color closest to the desired color
154 Used if color cell allocation fails
155 As the returned pixel value may be in use by another application,
156 the color might change anytime.
157 But in many cases, that is still better than always using black.
159 Chris Breeze <chris@hel.co.uk>
161 1) More efficient calculation of RGB distance of colour cell from
162 the desired colour. There is no need to take the sqrt of 'dist', and
163 since we are only interested in the top 8-bits of R, G and B we
164 can perform integer arithmetic.
165 2) Attempt to allocate a read-only colour when a close match is found.
166 A read-only colour will not change.
167 3) Fall back to the closest match if no read-only colours are available.
169 Possible further improvements:
170 1) Scan the lookup table and sort the colour cells in order of
172 distance from the desired colour. Then attempt to allocate a
174 colour starting from the nearest match.
175 2) Linear RGB distance is not a particularly good method of colour
177 (though it is quick). Converting the colour to HLS and then comparing
178 may give better matching.
179 -------------------------------------------*/
181 int wxGetBestMatchingPixel(Display
*display
, XColor
*desiredColor
, Colormap cmap
)
183 if (cmap
== (Colormap
) NULL
)
184 cmap
= (Colormap
) wxTheApp
->GetMainColormap(display
);
186 int numPixVals
= XDisplayCells(display
, DefaultScreen (display
));
187 int mindist
= 256 * 256 * 3;
188 int bestpixel
= (int) BlackPixel (display
, DefaultScreen (display
));
189 int red
= desiredColor
->red
>> 8;
190 int green
= desiredColor
->green
>> 8;
191 int blue
= desiredColor
->blue
>> 8;
192 const int threshold
= 2 * 2 * 3; // allow an error of up to 2 in R,G & B
194 for (int pixelcount
= 0; pixelcount
< numPixVals
; pixelcount
++)
196 XColor matching_color
;
197 matching_color
.pixel
= pixelcount
;
198 XQueryColor(display
,cmap
,&matching_color
);
200 int delta_red
= red
- (matching_color
.red
>> 8);
201 int delta_green
= green
- (matching_color
.green
>> 8);
202 int delta_blue
= blue
- (matching_color
.blue
>> 8);
204 int dist
= delta_red
* delta_red
+
205 delta_green
* delta_green
+
206 delta_blue
* delta_blue
;
208 if (dist
<= threshold
)
210 // try to allocate a read-only colour...
211 if (XAllocColor (display
, cmap
, &matching_color
))
213 return matching_color
.pixel
;
218 bestpixel
= pixelcount
;