diff options
| author | Paul Eggert | 2011-06-13 00:53:14 -0700 |
|---|---|---|
| committer | Paul Eggert | 2011-06-13 00:53:14 -0700 |
| commit | 5efd304be978e2c0b3b1ac0c39b303b8d094ab66 (patch) | |
| tree | 3b6003057fd4b17346d0156b8e07bc58a6f68160 /src | |
| parent | 578c21e6cbfcd42d2b765ff93a5340555ef91d9d (diff) | |
| download | emacs-5efd304be978e2c0b3b1ac0c39b303b8d094ab66.tar.gz emacs-5efd304be978e2c0b3b1ac0c39b303b8d094ab66.zip | |
* 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')
| -rw-r--r-- | src/ChangeLog | 4 | ||||
| -rw-r--r-- | src/xterm.c | 12 |
2 files changed, 11 insertions, 5 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index ff6945d8656..125028297d1 100644 --- a/src/ChangeLog +++ b/src/ChangeLog | |||
| @@ -1,5 +1,9 @@ | |||
| 1 | 2011-06-13 Paul Eggert <eggert@cs.ucla.edu> | 1 | 2011-06-13 Paul Eggert <eggert@cs.ucla.edu> |
| 2 | 2 | ||
| 3 | * xterm.c (x_alloc_nearest_color_1): Use a more-precise algorithm | ||
| 4 | for nearest color, one that neither overflows nor relies on unsigned | ||
| 5 | arithmetic. | ||
| 6 | |||
| 3 | Remove unnecessary casts. | 7 | Remove unnecessary casts. |
| 4 | * xterm.c (x_term_init): | 8 | * xterm.c (x_term_init): |
| 5 | * xfns.c (x_set_border_pixel): | 9 | * xfns.c (x_set_border_pixel): |
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) | |||
| 1693 | a least-squares matching, which is what X uses for closest | 1693 | a least-squares matching, which is what X uses for closest |
| 1694 | color matching with StaticColor visuals. */ | 1694 | color matching with StaticColor visuals. */ |
| 1695 | int nearest, i; | 1695 | int nearest, i; |
| 1696 | unsigned long nearest_delta = ~ (unsigned long) 0; | 1696 | int max_color_delta = (1 << (16 - 2)) - 1; |
| 1697 | int max_delta = 3 * max_color_delta; | ||
| 1698 | int nearest_delta = max_delta + 1; | ||
| 1697 | int ncells; | 1699 | int ncells; |
| 1698 | const XColor *cells = x_color_cells (dpy, &ncells); | 1700 | const XColor *cells = x_color_cells (dpy, &ncells); |
| 1699 | 1701 | ||
| 1700 | for (nearest = i = 0; i < ncells; ++i) | 1702 | for (nearest = i = 0; i < ncells; ++i) |
| 1701 | { | 1703 | { |
| 1702 | long dred = (color->red >> 8) - (cells[i].red >> 8); | 1704 | int dred = (color->red >> 2) - (cells[i].red >> 2); |
| 1703 | long dgreen = (color->green >> 8) - (cells[i].green >> 8); | 1705 | int dgreen = (color->green >> 2) - (cells[i].green >> 2); |
| 1704 | long dblue = (color->blue >> 8) - (cells[i].blue >> 8); | 1706 | int dblue = (color->blue >> 2) - (cells[i].blue >> 2); |
| 1705 | unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue; | 1707 | int delta = dred * dred + dgreen * dgreen + dblue * dblue; |
| 1706 | 1708 | ||
| 1707 | if (delta < nearest_delta) | 1709 | if (delta < nearest_delta) |
| 1708 | { | 1710 | { |