diff options
| author | Po Lu | 2022-02-04 15:38:07 +0800 |
|---|---|---|
| committer | Po Lu | 2022-02-04 15:38:07 +0800 |
| commit | 3da5dc66eae96ce3c62c875a4b0aa0838010bcd4 (patch) | |
| tree | 8c695d9d80f003ede5836997e6be459c71259940 /src | |
| parent | 8e2d9193ef2700bb93313525cf6e232619860711 (diff) | |
| download | emacs-3da5dc66eae96ce3c62c875a4b0aa0838010bcd4.tar.gz emacs-3da5dc66eae96ce3c62c875a4b0aa0838010bcd4.zip | |
Fix bit rot in the color allocation code
* src/xterm.c (x_alloc_nearest_color_1): Reintroduce an older
version of the code that would try to allocate a "compromise
delta".
Diffstat (limited to 'src')
| -rw-r--r-- | src/xterm.c | 64 |
1 files changed, 43 insertions, 21 deletions
diff --git a/src/xterm.c b/src/xterm.c index 2e09c454b24..874baa20f8c 100644 --- a/src/xterm.c +++ b/src/xterm.c | |||
| @@ -3083,34 +3083,56 @@ x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color) | |||
| 3083 | if (rc == 0) | 3083 | if (rc == 0) |
| 3084 | { | 3084 | { |
| 3085 | /* If we got to this point, the colormap is full, so we're going | 3085 | /* If we got to this point, the colormap is full, so we're going |
| 3086 | to try to get the next closest color. The algorithm used is | 3086 | to try and get the next closest color. The algorithm used is |
| 3087 | a least-squares matching, which is what X uses for closest | 3087 | a least-squares matching, which is what X uses for closest |
| 3088 | color matching with StaticColor visuals. */ | 3088 | color matching with StaticColor visuals. */ |
| 3089 | int nearest, i; | ||
| 3090 | int max_color_delta = 255; | ||
| 3091 | int max_delta = 3 * max_color_delta; | ||
| 3092 | int nearest_delta = max_delta + 1; | ||
| 3093 | int ncells; | ||
| 3094 | const XColor *cells = x_color_cells (dpy, &ncells); | ||
| 3095 | |||
| 3096 | for (nearest = i = 0; i < ncells; ++i) | ||
| 3097 | { | ||
| 3098 | int dred = (color->red >> 8) - (cells[i].red >> 8); | ||
| 3099 | int dgreen = (color->green >> 8) - (cells[i].green >> 8); | ||
| 3100 | int dblue = (color->blue >> 8) - (cells[i].blue >> 8); | ||
| 3101 | int delta = dred * dred + dgreen * dgreen + dblue * dblue; | ||
| 3102 | 3089 | ||
| 3103 | if (delta < nearest_delta) | 3090 | const XColor *cells; |
| 3091 | int no_cells; | ||
| 3092 | int nearest; | ||
| 3093 | long nearest_delta, trial_delta; | ||
| 3094 | int x; | ||
| 3095 | Status status; | ||
| 3096 | |||
| 3097 | cells = x_color_cells (dpy, &no_cells); | ||
| 3098 | |||
| 3099 | XQueryColors (dpy, cmap, cells, no_cells); | ||
| 3100 | nearest = 0; | ||
| 3101 | /* I'm assuming CSE so I'm not going to condense this. */ | ||
| 3102 | nearest_delta = ((((color->red >> 8) - (cells[0].red >> 8)) | ||
| 3103 | * ((color->red >> 8) - (cells[0].red >> 8))) | ||
| 3104 | + (((color->green >> 8) - (cells[0].green >> 8)) | ||
| 3105 | * ((color->green >> 8) - (cells[0].green >> 8))) | ||
| 3106 | + (((color->blue >> 8) - (cells[0].blue >> 8)) | ||
| 3107 | * ((color->blue >> 8) - (cells[0].blue >> 8)))); | ||
| 3108 | for (x = 1; x < no_cells; x++) | ||
| 3109 | { | ||
| 3110 | trial_delta = ((((color->red >> 8) - (cells[x].red >> 8)) | ||
| 3111 | * ((color->red >> 8) - (cells[x].red >> 8))) | ||
| 3112 | + (((color->green >> 8) - (cells[x].green >> 8)) | ||
| 3113 | * ((color->green >> 8) - (cells[x].green >> 8))) | ||
| 3114 | + (((color->blue >> 8) - (cells[x].blue >> 8)) | ||
| 3115 | * ((color->blue >> 8) - (cells[x].blue >> 8)))); | ||
| 3116 | if (trial_delta < nearest_delta) | ||
| 3104 | { | 3117 | { |
| 3105 | nearest = i; | 3118 | XColor temp; |
| 3106 | nearest_delta = delta; | 3119 | temp.red = cells[x].red; |
| 3120 | temp.green = cells[x].green; | ||
| 3121 | temp.blue = cells[x].blue; | ||
| 3122 | status = XAllocColor (dpy, cmap, &temp); | ||
| 3123 | if (status) | ||
| 3124 | { | ||
| 3125 | nearest = x; | ||
| 3126 | nearest_delta = trial_delta; | ||
| 3127 | } | ||
| 3107 | } | 3128 | } |
| 3108 | } | 3129 | } |
| 3109 | 3130 | color->red = cells[nearest].red; | |
| 3110 | color->red = cells[nearest].red; | ||
| 3111 | color->green = cells[nearest].green; | 3131 | color->green = cells[nearest].green; |
| 3112 | color->blue = cells[nearest].blue; | 3132 | color->blue = cells[nearest].blue; |
| 3113 | rc = XAllocColor (dpy, cmap, color) != 0; | 3133 | status = XAllocColor (dpy, cmap, color); |
| 3134 | |||
| 3135 | rc = status != 0; | ||
| 3114 | } | 3136 | } |
| 3115 | else | 3137 | else |
| 3116 | { | 3138 | { |