summaryrefslogtreecommitdiff
path: root/src/xterm.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2011-06-13 00:53:14 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2011-06-13 00:53:14 -0700
commit5efd304be978e2c0b3b1ac0c39b303b8d094ab66 (patch)
tree3b6003057fd4b17346d0156b8e07bc58a6f68160 /src/xterm.c
parent578c21e6cbfcd42d2b765ff93a5340555ef91d9d (diff)
downloademacs-5efd304be978e2c0b3b1ac0c39b303b8d094ab66.tar.gz
* xterm.c (x_alloc_nearest_color_1): Use a more-precise algorithm
for nearest color, one that neither overflows nor relies on unsigned arithmetic.
Diffstat (limited to 'src/xterm.c')
-rw-r--r--src/xterm.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/xterm.c b/src/xterm.c
index 85c19ed16af..1c1c8e3f107 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -1693,16 +1693,18 @@ x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
a least-squares matching, which is what X uses for closest
color matching with StaticColor visuals. */
int nearest, i;
- unsigned long nearest_delta = ~ (unsigned long) 0;
+ int max_color_delta = (1 << (16 - 2)) - 1;
+ int max_delta = 3 * max_color_delta;
+ int nearest_delta = max_delta + 1;
int ncells;
const XColor *cells = x_color_cells (dpy, &ncells);
for (nearest = i = 0; i < ncells; ++i)
{
- long dred = (color->red >> 8) - (cells[i].red >> 8);
- long dgreen = (color->green >> 8) - (cells[i].green >> 8);
- long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
- unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
+ int dred = (color->red >> 2) - (cells[i].red >> 2);
+ int dgreen = (color->green >> 2) - (cells[i].green >> 2);
+ int dblue = (color->blue >> 2) - (cells[i].blue >> 2);
+ int delta = dred * dred + dgreen * dgreen + dblue * dblue;
if (delta < nearest_delta)
{