annotate lwlib/lwlib-colors.c @ 4916:a6c778975d7d

split USE_XFT into HAVE_XFT/USE_XFT -------------------- ChangeLog entries follow: -------------------- ChangeLog addition: 2010-02-01 Ben Wing <ben@xemacs.org> * configure: * configure.ac (TAB): USE_XFT* -> HAVE_XFT*. lwlib/ChangeLog addition: 2010-02-01 Ben Wing <ben@xemacs.org> * lwlib-colors.c: * lwlib-colors.h: * lwlib-fonts.c: * lwlib-fonts.h: * xlwmenu.c: * xlwmenu.c (xlwMenuResources): * xlwmenu.c (x_xft_text_width): * xlwmenu.c (label_button_draw): * xlwmenu.c (push_button_size): * xlwmenu.c (push_button_draw): * xlwmenu.c (make_drawing_gcs): * xlwmenu.c (extract_font_extents): * xlwmenu.c (default_font_of_font_list): * xlwmenu.c (XlwMenuInitialize): * xlwmenu.c (XlwMenuDestroy): * xlwmenu.c (XlwMenuSetValues): * xlwmenuP.h: * xlwmenuP.h (_XlwMenu_part): * xlwtabs.c: * xlwtabs.c (TabsInit): * xlwtabs.c (TabsDestroy): * xlwtabs.c (TabsSetValues): * xlwtabs.c (DrawTab): * xlwtabs.c (TabWidth): * xlwtabs.c (TabsAllocFgGC): * xlwtabs.c (TabsAllocGreyGC): * xlwtabsP.h: USE_XFT* -> HAVE_XFT*. src/ChangeLog addition: 2010-02-01 Ben Wing <ben@xemacs.org> * Makefile.in.in: * compiler.h: * config.h.in: * console-x-impl.h: * console-x-impl.h (struct x_frame): * console-x-impl.h (FRAME_X_TOTALLY_VISIBLE_P): * console-x.h: * console-xlike-inc.h: * emacs.c (main_1): * faces.c (complex_vars_of_faces): * font-mgr.h: * frame-x.c (x_delete_frame): * frame-x.c (x_update_frame_external_traits): * glyphs-x.c (update_widget_face): * inline.c: * objects-x-impl.h: * objects-x-impl.h (struct x_color_instance_data): * objects-x-impl.h (struct x_font_instance_data): * objects-x.c: * objects-x.c (x_initialize_color_instance): * objects-x.c (x_initialize_font_instance): * objects-x.c (x_print_font_instance): * objects-x.c (x_finalize_font_instance): * objects-x.c (x_font_instance_truename): * objects-x.c (vars_of_objects_x): * objects-x.h: USE_XFT* -> HAVE_XFT*. But in objects-xlike-inc.c and redisplay-xlike-inc.c, continue to use USE_XFT, and define it appropriately in console-xlike-inc.h when both HAVE_XFT and THIS_IS_X -- even if HAVE_XFT, we don't want to enable XFT code when included in a *-gtk.c file.
author Ben Wing <ben@xemacs.org>
date Mon, 01 Feb 2010 22:00:29 -0600
parents ad2f4ae9895b
children 308d34e9f07d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3094
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
1 /* Color data structures for X and Xft.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
2
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
3 Copyright (C) 2004 Free Software Foundation, Inc.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
4
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
5 Author: Stephen J. Turnbull <stephen@xemacs.org>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
6 Created: 24 Jul 2004 by Stephen J. Turnbull
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
7
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
8 This file is part of XEmacs.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
9
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
10 XEmacs is free software; you can redistribute it and/or modify it
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
11 under the terms of the GNU General Public License as published by the
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
12 Free Software Foundation; either version 2, or (at your option) any
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
13 later version.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
14
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
18 for more details.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
19
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
20 You should have received a copy of the GNU General Public License
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
21 along with XEmacs; see the file COPYING. If not, write to
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
23 Boston, MA 02111-1307, USA. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
24
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
25 /* Synched up with: Not in GNU Emacs. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
26
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
27 #include <config.h>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
28 #include <limits.h> /* for ULONG_MAX */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
29 #include <stdlib.h> /* for malloc() */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
30 #include <stdio.h>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
31 #include <X11/Xlib.h>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
32 #include <X11/IntrinsicP.h>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
33 #include <X11/ShellP.h> /* for ShellWidget */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
34 #include "lwlib-colors.h"
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
35
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
36 static int debug_colors = 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
37
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
38 #ifdef __cplusplus
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
39 #define X_CLASSFIELD c_class
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
40 #else
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
41 #define X_CLASSFIELD class
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
42 #endif
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
43
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
44 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
45 ? ((unsigned long) (x)) : ((unsigned long) (y)))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
46
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
47 /* WIDGET is an Xt widget, VISUAL and DEPTH are return values */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
48 void
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
49 visual_info_from_widget (Widget widget, Visual **visual, int *depth)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
50 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
51 /* grab the visual and depth from the nearest shell ancestor */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
52 Widget p = XtParent(widget);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
53
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
54 *visual = CopyFromParent;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
55 *depth = -1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
56 while (*visual == CopyFromParent && p)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
57 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
58 if (XtIsShell(p))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
59 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
60 *visual = ((ShellWidget)p)->shell.visual;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
61 *depth = p->core.depth;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
62 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
63 p = XtParent(p);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
64 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
65 if (*visual == CopyFromParent || !*visual)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
66 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
67 if (debug_colors > 1)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
68 fprintf (stderr, "\nvisual_info_from_widget:"
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
69 " failed, using DefaultVisualOfScreen");
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
70 *visual = DefaultVisualOfScreen (XtScreen (widget));
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
71 *depth = DefaultDepthOfScreen (XtScreen (widget));
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
72 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
73 else if (debug_colors > 1)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
74 fprintf (stderr, "\nvisual_info_from_widget: succeeded");
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
75 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
76
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
77 /* Do we need all this hair on modern hardware? */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
78
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
79 /* Replacement for XAllocColor() that tries to return the nearest
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
80 available color if the colormap is full. Original was from FSFmacs,
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
81 but rewritten by Jareth Hein <jareth@camelot-soft.com> 97/11/25
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
82 Modified by Lee Kindness <lkindness@csl.co.uk> 31/08/99 to handle previous
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
83 total failure which was due to a read/write colorcell being the nearest
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
84 match - tries the next nearest...
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
85
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
86 Return value is 1 for normal success, 2 for nearest color success,
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
87 3 for Non-deallocable success. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
88 int
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
89 x_allocate_nearest_color (Display *display, Colormap colormap,
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
90 Visual *visual, XColor *color_def)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
91 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
92 int status;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
93
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
94 /* #### [[Apparently this is often called with data derived from a widget
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
95 with no ShellWidget ancestor, or before the shell has a visual.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
96 Currently this recovery code is in xlwmenu.c and xlwscrollbar.c, but
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
97 likely should come here.]]
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
98 I suspect the problem is that the visual-tracing code was improperly
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
99 written, missing a level of indirection.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
100 CopyFromParent == NULL in XFree86/Darwin.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
101 */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
102 if (visual == CopyFromParent || !visual)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
103 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
104 Screen *screen = DefaultScreenOfDisplay (display);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
105 fprintf (stderr, "\nx_allocate_nearest_color: bad visual (%08lx)",
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
106 (unsigned long) visual);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
107 visual = DefaultVisualOfScreen (screen);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
108 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
109
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
110 if (visual->X_CLASSFIELD == DirectColor || visual->X_CLASSFIELD == TrueColor)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
111 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
112 if (XAllocColor (display, colormap, color_def) != 0)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
113 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
114 status = 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
115 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
116 else
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
117 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
118 /* We're dealing with a TrueColor/DirectColor visual, so play games
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
119 with the RGB values in the XColor struct. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
120 /* #### JH: I'm not sure how a call to XAllocColor can fail in a
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
121 TrueColor or DirectColor visual, so I will just reformat the
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
122 request to match the requirements of the visual, and re-issue
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
123 the request. If this fails for anybody, I wanna know about it
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
124 so I can come up with a better plan */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
125
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
126 unsigned long rshift,gshift,bshift,rbits,gbits,bbits,junk;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
127 junk = visual->red_mask;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
128 rshift = 0;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
129 while ((junk & 0x1) == 0) {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
130 junk = junk >> 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
131 rshift ++;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
132 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
133 rbits = 0;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
134 while (junk != 0) {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
135 junk = junk >> 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
136 rbits++;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
137 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
138 junk = visual->green_mask;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
139 gshift = 0;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
140 while ((junk & 0x1) == 0) {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
141 junk = junk >> 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
142 gshift ++;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
143 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
144 gbits = 0;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
145 while (junk != 0) {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
146 junk = junk >> 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
147 gbits++;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
148 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
149 junk = visual->blue_mask;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
150 bshift = 0;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
151 while ((junk & 0x1) == 0) {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
152 junk = junk >> 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
153 bshift ++;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
154 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
155 bbits = 0;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
156 while (junk != 0) {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
157 junk = junk >> 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
158 bbits++;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
159 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
160
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
161 color_def->red = color_def->red >> (16 - rbits);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
162 color_def->green = color_def->green >> (16 - gbits);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
163 color_def->blue = color_def->blue >> (16 - bbits);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
164 if (XAllocColor (display, colormap, color_def) != 0)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
165 status = 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
166 else
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
167 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
168 int rd, gr, bl;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
169 /* #### JH: I'm punting here, knowing that doing this will at
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
170 least draw the color correctly. However, unless we convert
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
171 all of the functions that allocate colors (graphics
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
172 libraries, etc) to use this function doing this is very
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
173 likely to cause problems later... */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
174
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
175 if (rbits > 8)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
176 rd = color_def->red << (rbits - 8);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
177 else
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
178 rd = color_def->red >> (8 - rbits);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
179 if (gbits > 8)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
180 gr = color_def->green << (gbits - 8);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
181 else
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
182 gr = color_def->green >> (8 - gbits);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
183 if (bbits > 8)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
184 bl = color_def->blue << (bbits - 8);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
185 else
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
186 bl = color_def->blue >> (8 - bbits);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
187 color_def->pixel = (rd << rshift) | (gr << gshift) | (bl <<
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
188 bshift);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
189 status = 3;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
190 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
191 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
192 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
193 else
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
194 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
195 XColor *cells = NULL;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
196 /* JH: I can't believe there's no way to go backwards from a
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
197 colormap ID and get its visual and number of entries, but X
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
198 apparently isn't built that way... */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
199 int no_cells = visual->map_entries;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
200 status = 0;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
201
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
202 if (XAllocColor (display, colormap, color_def) != 0)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
203 status = 1;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
204 else while( status != 2 )
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
205 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
206 /* If we got to this point, the colormap is full, so we're
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
207 going to try and get the next closest color. The algorithm used
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
208 is a least-squares matching, which is what X uses for closest
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
209 color matching with StaticColor visuals. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
210 int nearest;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
211 long nearest_delta, trial_delta;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
212 int x;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
213
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
214 if( cells == NULL )
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
215 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
216 /* #### this could be annoyingly slow
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
217 tell me again why lwlib can't use alloca & friends? */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
218 cells = (XColor *) malloc (sizeof(XColor)*no_cells);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
219 for (x = 0; x < no_cells; x++)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
220 cells[x].pixel = x;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
221
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
222 /* read the current colormap */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
223 XQueryColors (display, colormap, cells, no_cells);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
224 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
225
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
226 nearest = 0;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
227 /* I'm assuming CSE so I'm not going to condense this. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
228 nearest_delta = ((((color_def->red >> 8) - (cells[0].red >> 8))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
229 * ((color_def->red >> 8) - (cells[0].red >> 8)))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
230 +
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
231 (((color_def->green >> 8) - (cells[0].green >> 8))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
232 * ((color_def->green >> 8) - (cells[0].green >>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
233 8)))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
234 +
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
235 (((color_def->blue >> 8) - (cells[0].blue >> 8))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
236 * ((color_def->blue >> 8) - (cells[0].blue >>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
237 8))));
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
238 for (x = 1; x < no_cells; x++)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
239 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
240 trial_delta = ((((color_def->red >> 8) - (cells[x].red >> 8))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
241 * ((color_def->red >> 8) - (cells[x].red >> 8)))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
242 +
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
243 (((color_def->green >> 8) - (cells[x].green >> 8))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
244 * ((color_def->green >> 8) - (cells[x].green >>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
245 8)))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
246 +
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
247 (((color_def->blue >> 8) - (cells[x].blue >> 8))
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
248 * ((color_def->blue >> 8) - (cells[x].blue >>
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
249 8))));
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
250
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
251 /* less? Ignore cells marked as previously failing */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
252 if( (trial_delta < nearest_delta) &&
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
253 (cells[x].pixel != ULONG_MAX) )
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
254 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
255 nearest = x;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
256 nearest_delta = trial_delta;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
257 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
258 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
259 color_def->red = cells[nearest].red;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
260 color_def->green = cells[nearest].green;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
261 color_def->blue = cells[nearest].blue;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
262 if (XAllocColor (display, colormap, color_def) != 0)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
263 status = 2;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
264 else
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
265 /* LSK: Either the colour map has changed since
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
266 * we read it, or the colour is allocated
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
267 * read/write... Mark this cmap entry so it's
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
268 * ignored in the next iteration.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
269 */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
270 cells[nearest].pixel = ULONG_MAX;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
271 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
272 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
273 return status;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
274 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
275
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
276 #if 0
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
277 /* Replacement for XAllocColor() that tries to return the nearest
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
278 available color if the colormap is full. From GNU Emacs.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
279 #### Review this to see if there's anything our hairy version could use. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
280
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
281 int
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
282 FIXME_allocate_nearest_color (Display *display, Colormap screen_colormap,
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
283 XColor *color_def)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
284 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
285 int status = XAllocColor (display, screen_colormap, color_def);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
286 if (status)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
287 return status;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
288
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
289 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
290 /* If we got to this point, the colormap is full, so we're
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
291 going to try to get the next closest color.
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
292 The algorithm used is a least-squares matching, which is
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
293 what X uses for closest color matching with StaticColor visuals. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
294
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
295 int nearest, x;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
296 unsigned long nearest_delta = ULONG_MAX;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
297
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
298 int no_cells = XDisplayCells (display, XDefaultScreen (display));
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
299 /* Don't use alloca here because lwlib doesn't have the
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
300 necessary configuration information that src does. */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
301 XColor *cells = (XColor *) malloc (sizeof (XColor) * no_cells);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
302
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
303 for (x = 0; x < no_cells; x++)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
304 cells[x].pixel = x;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
305
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
306 XQueryColors (display, screen_colormap, cells, no_cells);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
307
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
308 for (nearest = 0, x = 0; x < no_cells; x++)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
309 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
310 long dred = (color_def->red >> 8) - (cells[x].red >> 8);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
311 long dgreen = (color_def->green >> 8) - (cells[x].green >> 8);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
312 long dblue = (color_def->blue >> 8) - (cells[x].blue >> 8);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
313 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
314
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
315 if (delta < nearest_delta)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
316 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
317 nearest = x;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
318 nearest_delta = delta;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
319 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
320 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
321 color_def->red = cells[nearest].red;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
322 color_def->green = cells[nearest].green;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
323 color_def->blue = cells[nearest].blue;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
324 free (cells);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
325 return XAllocColor (display, screen_colormap, color_def);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
326 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
327 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
328 #endif
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
329
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
330
4916
a6c778975d7d split USE_XFT into HAVE_XFT/USE_XFT
Ben Wing <ben@xemacs.org>
parents: 3094
diff changeset
331 #ifdef HAVE_XFT
3094
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
332
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
333 XftColor
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
334 xft_convert_color (Display *dpy, Colormap cmap, Visual *visual, int c, int dim)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
335 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
336 static XColor color; /* #### why is this static ?? */
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
337 XftColor result;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
338
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
339 color.pixel = c;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
340 XQueryColor(dpy, cmap, &color);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
341
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
342 if (dim)
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
343 {
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
344 color.red = MINL (65535, color.red * 1.5);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
345 color.green = MINL (65535, color.green * 1.5);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
346 color.blue = MINL (65535, color.blue * 1.5);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
347 x_allocate_nearest_color (dpy, cmap, visual, &color);
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
348 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
349
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
350 result.pixel = color.pixel;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
351 result.color.red = color.red;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
352 result.color.green = color.green;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
353 result.color.blue = color.blue;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
354 result.color.alpha = 0xffff;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
355
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
356 return result;
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
357 }
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
358
4916
a6c778975d7d split USE_XFT into HAVE_XFT/USE_XFT
Ben Wing <ben@xemacs.org>
parents: 3094
diff changeset
359 #endif /* HAVE_XFT */
3094
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
360
ad2f4ae9895b [xemacs-hg @ 2005-11-26 11:45:47 by stephent]
stephent
parents:
diff changeset
361 /* end of lwlib-colors.c */