diff options
| author | Richard M. Stallman | 1995-03-06 04:33:56 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1995-03-06 04:33:56 +0000 |
| commit | 829782956edf73de16096650fdf02d7c96e0f500 (patch) | |
| tree | 2c8afb829d754837b5d084fd7cd1a8641d02b55c /src | |
| parent | a992f9d90e608bbd92c7db5a797f6d799f56e7c6 (diff) | |
| download | emacs-829782956edf73de16096650fdf02d7c96e0f500.tar.gz emacs-829782956edf73de16096650fdf02d7c96e0f500.zip | |
(defined_color): If colormap is full, find closest match.
Diffstat (limited to 'src')
| -rw-r--r-- | src/xfns.c | 69 |
1 files changed, 61 insertions, 8 deletions
diff --git a/src/xfns.c b/src/xfns.c index c62bdd4a090..7a5e4ff9d71 100644 --- a/src/xfns.c +++ b/src/xfns.c | |||
| @@ -963,6 +963,7 @@ x_report_frame_params (f, alistptr) | |||
| 963 | : FRAME_ICONIFIED_P (f) ? Qicon : Qnil)); | 963 | : FRAME_ICONIFIED_P (f) ? Qicon : Qnil)); |
| 964 | } | 964 | } |
| 965 | 965 | ||
| 966 | |||
| 966 | /* Decide if color named COLOR is valid for the display associated with | 967 | /* Decide if color named COLOR is valid for the display associated with |
| 967 | the selected frame; if so, return the rgb values in COLOR_DEF. | 968 | the selected frame; if so, return the rgb values in COLOR_DEF. |
| 968 | If ALLOC is nonzero, allocate a new colormap cell. */ | 969 | If ALLOC is nonzero, allocate a new colormap cell. */ |
| @@ -974,20 +975,72 @@ defined_color (f, color, color_def, alloc) | |||
| 974 | XColor *color_def; | 975 | XColor *color_def; |
| 975 | int alloc; | 976 | int alloc; |
| 976 | { | 977 | { |
| 977 | register int foo; | 978 | register int status; |
| 978 | Colormap screen_colormap; | 979 | Colormap screen_colormap; |
| 980 | Display *display = FRAME_X_DISPLAY (f); | ||
| 979 | 981 | ||
| 980 | BLOCK_INPUT; | 982 | BLOCK_INPUT; |
| 981 | screen_colormap | 983 | screen_colormap = DefaultColormap (display, XDefaultScreen (display)); |
| 982 | = DefaultColormap (FRAME_X_DISPLAY (f), | ||
| 983 | XDefaultScreen (FRAME_X_DISPLAY (f))); | ||
| 984 | 984 | ||
| 985 | foo = XParseColor (FRAME_X_DISPLAY (f), screen_colormap, color, color_def); | 985 | status = XParseColor (display, screen_colormap, color, color_def); |
| 986 | if (foo && alloc) | 986 | if (status && alloc) |
| 987 | foo = XAllocColor (FRAME_X_DISPLAY (f), screen_colormap, color_def); | 987 | { |
| 988 | status = XAllocColor (display, screen_colormap, color_def); | ||
| 989 | if (!status) | ||
| 990 | { | ||
| 991 | /* If we got to this point, the colormap is full, so we're | ||
| 992 | going to try and get the next closest color. | ||
| 993 | The algorithm used is a least-squares matching, which is | ||
| 994 | what X uses for closest color matching with StaticColor visuals. */ | ||
| 995 | |||
| 996 | XColor *cells; | ||
| 997 | int no_cells; | ||
| 998 | int nearest; | ||
| 999 | long nearest_delta, trial_delta; | ||
| 1000 | int x; | ||
| 1001 | |||
| 1002 | no_cells = XDisplayCells (display, XDefaultScreen (display)); | ||
| 1003 | cells = (XColor *) alloca (sizeof (XColor) * no_cells); | ||
| 1004 | |||
| 1005 | for (x = 0; x < no_cells; x++) | ||
| 1006 | cells[x].pixel = x; | ||
| 1007 | |||
| 1008 | XQueryColors (display, screen_colormap, cells, no_cells); | ||
| 1009 | nearest = 0; | ||
| 1010 | /* I'm assuming CSE so I'm not going to condense this. */ | ||
| 1011 | nearest_delta = ((((color_def->red >> 8) - (cells[0].red >> 8)) | ||
| 1012 | * ((color_def->red >> 8) - (cells[0].red >> 8))) | ||
| 1013 | + | ||
| 1014 | (((color_def->green >> 8) - (cells[0].green >> 8)) | ||
| 1015 | * ((color_def->green >> 8) - (cells[0].green >> 8))) | ||
| 1016 | + | ||
| 1017 | (((color_def->blue >> 8) - (cells[0].blue >> 8)) | ||
| 1018 | * ((color_def->blue >> 8) - (cells[0].blue >> 8)))); | ||
| 1019 | for (x = 1; x < no_cells; x++) | ||
| 1020 | { | ||
| 1021 | trial_delta = ((((color_def->red >> 8) - (cells[x].red >> 8)) | ||
| 1022 | * ((color_def->red >> 8) - (cells[x].red >> 8))) | ||
| 1023 | + | ||
| 1024 | (((color_def->green >> 8) - (cells[x].green >> 8)) | ||
| 1025 | * ((color_def->green >> 8) - (cells[x].green >> 8))) + | ||
| 1026 | + | ||
| 1027 | (((color_def->blue >> 8) - (cells[x].blue >> 8)) | ||
| 1028 | * ((color_def->blue >> 8) - (cells[x].blue >> 8)))); | ||
| 1029 | if (trial_delta < nearest_delta) | ||
| 1030 | { | ||
| 1031 | nearest = x; | ||
| 1032 | nearest_delta = trial_delta; | ||
| 1033 | } | ||
| 1034 | } | ||
| 1035 | color_def->red = cells[nearest].red; | ||
| 1036 | color_def->green = cells[nearest].green; | ||
| 1037 | color_def->blue = cells[nearest].blue; | ||
| 1038 | status = XAllocColor (display, screen_colormap, color_def); | ||
| 1039 | } | ||
| 1040 | } | ||
| 988 | UNBLOCK_INPUT; | 1041 | UNBLOCK_INPUT; |
| 989 | 1042 | ||
| 990 | if (foo) | 1043 | if (status) |
| 991 | return 1; | 1044 | return 1; |
| 992 | else | 1045 | else |
| 993 | return 0; | 1046 | return 0; |