| 1 |
/* @(#)colors.c 17.1 (ESO-IPG) 01/25/02 17:26:32 */ |
| 2 |
/*--------------------------------------------------------------------- |
| 3 |
* $Date: 2003-06-04 14:49:18 $ $Revision: 1.1.1.1 $ |
| 4 |
*--------------------------------------------------------------------- |
| 5 |
* |
| 6 |
* |
| 7 |
* Copyright (c) 1988, Visual Edge Software Ltd. |
| 8 |
* |
| 9 |
* ALL RIGHTS RESERVED. Permission to use, copy, modify, and |
| 10 |
* distribute this software and its documentation for any purpose |
| 11 |
* and without fee is hereby granted, provided that the above |
| 12 |
* copyright notice appear in all copies and that both that |
| 13 |
* copyright notice and this permission notice appear in supporting |
| 14 |
* documentation, and that the name of Visual Edge Software not be |
| 15 |
* used in advertising or publicity pertaining to distribution of |
| 16 |
* the software without specific, written prior permission. The year |
| 17 |
* included in the notice is the year of the creation of the work. |
| 18 |
*-------------------------------------------------------------------*/ |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
#include <ctype.h> |
| 23 |
#ifdef USE_STDLIB |
| 24 |
#include <stdlib.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <X11/IntrinsicP.h> |
| 28 |
#include <X11/StringDefs.h> |
| 29 |
#include <X11/Xos.h> |
| 30 |
#include <X11/Xutil.h> |
| 31 |
|
| 32 |
#include "global.h" |
| 33 |
#include "dstring.h" |
| 34 |
#include "valloc.h" |
| 35 |
#include "uimx_cat.h" |
| 36 |
#include "misc1_ds.h" |
| 37 |
|
| 38 |
#define HASHSIZE 256 |
| 39 |
|
| 40 |
typedef struct _ctbl |
| 41 |
{ XColor color; |
| 42 |
dstring name; |
| 43 |
struct _ctbl *nextc, *nextp; |
| 44 |
} colortbl; |
| 45 |
|
| 46 |
/* CG */ |
| 47 |
char *UxStripLeadingAndTrailingSpaces(char *str); |
| 48 |
|
| 49 |
static void string_to_xcolor UXPROTO((XrmValuePtr, Cardinal *, XrmValuePtr, |
| 50 |
XrmValuePtr)); |
| 51 |
|
| 52 |
/* Xt provides a XtRString to XtRPixel converter */ |
| 53 |
#ifdef NOT_NEEDED |
| 54 |
static void string_to_pixel UXPROTO((XrmValuePtr, Cardinal *, XrmValuePtr, |
| 55 |
XrmValuePtr)); |
| 56 |
#endif |
| 57 |
|
| 58 |
static void add_color UXPROTO((char*, XColor*)); |
| 59 |
|
| 60 |
#ifdef MOTIF_WIDGETS |
| 61 |
|
| 62 |
/* bugfix 739; CVEje00856, 857 & 880; XtConvertArgRec for colormap is */ |
| 63 |
/* different in Motif */ |
| 64 |
/* bugfix 1103: In X11R4, addressing is XtWidgetBaseOffset */ |
| 65 |
|
| 66 |
static XtConvertArgRec cvtarg[]= |
| 67 |
{{ XtWidgetBaseOffset, (void *) XtOffset(Widget, core.colormap), sizeof(Colormap)}}; |
| 68 |
|
| 69 |
#else |
| 70 |
|
| 71 |
static XtConvertArgRec cvtarg[]= |
| 72 |
{{ XtBaseOffset, (void *) XtOffset(Widget, core.colormap), sizeof(Colormap)}}; |
| 73 |
|
| 74 |
#endif /* MOTIF_WIDGETS */ |
| 75 |
|
| 76 |
static colortbl *chash[HASHSIZE]; |
| 77 |
static colortbl *phash[HASHSIZE]; |
| 78 |
static int depth, cells; |
| 79 |
|
| 80 |
|
| 81 |
/* X/OPEN message catalog macros. These will make the code more compact. */ |
| 82 |
#define CGETS(ms,ds_ms) UxCatGets(MC_MISC1,ms,ds_ms) |
| 83 |
|
| 84 |
/****************************************************************************** |
| 85 |
==== LOCAL ================================================================== |
| 86 |
******************************************************************************* |
| 87 |
NAME: int hash_name(name) |
| 88 |
|
| 89 |
INPUT: char *name - name to hash |
| 90 |
|
| 91 |
RETURN: hash value |
| 92 |
|
| 93 |
DESCRIPTION: return the hash value for a color name |
| 94 |
|
| 95 |
CREATION: Jun 21/1989 |
| 96 |
REVISIONS: -- |
| 97 |
-----------------------------------------------------------------------------*/ |
| 98 |
|
| 99 |
static int hash_name(name) |
| 100 |
char *name; |
| 101 |
{ |
| 102 |
int UxHash= 0; |
| 103 |
|
| 104 |
if(name == NULL) |
| 105 |
return(0); |
| 106 |
|
| 107 |
while(*name) |
| 108 |
UxHash+= *(name++); |
| 109 |
|
| 110 |
return(UxHash%HASHSIZE); |
| 111 |
} |
| 112 |
|
| 113 |
/****************************************************************************** |
| 114 |
==== LOCAL ================================================================== |
| 115 |
******************************************************************************* |
| 116 |
NAME: colortbl *lookup_by_pixel(pixel) |
| 117 |
|
| 118 |
INPUT: int pixel - pixel to find |
| 119 |
|
| 120 |
RETURN: color entry |
| 121 |
|
| 122 |
DESCRIPTION: lookup a color entry using its pixel value as the key |
| 123 |
|
| 124 |
CREATION: Jun 21/1989 |
| 125 |
REVISIONS: -- |
| 126 |
-----------------------------------------------------------------------------*/ |
| 127 |
|
| 128 |
static colortbl *lookup_by_pixel(pixel) |
| 129 |
int pixel; |
| 130 |
{ |
| 131 |
colortbl *pntr; |
| 132 |
|
| 133 |
for(pntr= phash[pixel%HASHSIZE]; pntr != NULL; pntr= pntr->nextp) |
| 134 |
if(pntr->color.pixel == pixel) |
| 135 |
return(pntr); |
| 136 |
|
| 137 |
return(NULL); |
| 138 |
} |
| 139 |
|
| 140 |
/****************************************************************************** |
| 141 |
==== LOCAL ================================================================== |
| 142 |
******************************************************************************* |
| 143 |
NAME: colortbl *lookup_by_name(name) |
| 144 |
|
| 145 |
INPUT: char *name - color name |
| 146 |
|
| 147 |
RETURN: color entry |
| 148 |
|
| 149 |
DESCRIPTION: lookup a color entry using its name as the key |
| 150 |
|
| 151 |
CREATION: Jun 21/1989 |
| 152 |
REVISIONS: -- |
| 153 |
-----------------------------------------------------------------------------*/ |
| 154 |
|
| 155 |
static colortbl *lookup_by_name(name) |
| 156 |
char *name; |
| 157 |
{ |
| 158 |
colortbl *pntr; |
| 159 |
int UxHash; |
| 160 |
|
| 161 |
UxHash= hash_name(name); |
| 162 |
|
| 163 |
for(pntr= chash[UxHash]; pntr != NULL; pntr= pntr->nextc) |
| 164 |
if(!strcmp(dnstr(pntr->name), name)) |
| 165 |
return(pntr); |
| 166 |
|
| 167 |
return(NULL); |
| 168 |
} |
| 169 |
|
| 170 |
/****************************************************************************** |
| 171 |
==== GLOBAL ================================================================= |
| 172 |
******************************************************************************* |
| 173 |
NAME: void UxInit_colors() |
| 174 |
|
| 175 |
DESCRIPTION: Initialize the colors database and register its resource |
| 176 |
converters with the toolkit |
| 177 |
|
| 178 |
CREATION: Jun 21/1989 |
| 179 |
REVISIONS: 15 August 1990 John Albert OLE MERGE |
| 180 |
-- removed ifdef MOTIF_WIDGETS to update |
| 181 |
to R3 for OL_WIDGETS. |
| 182 |
-----------------------------------------------------------------------------*/ |
| 183 |
|
| 184 |
void UxInit_colors() |
| 185 |
{ |
| 186 |
static int init= 0; |
| 187 |
XColor c; |
| 188 |
|
| 189 |
if(init) |
| 190 |
return; |
| 191 |
|
| 192 |
init= 1; |
| 193 |
|
| 194 |
depth= XDefaultDepth(UxDisplay, UxScreen); |
| 195 |
cells= DisplayCells(UxDisplay, UxScreen); |
| 196 |
|
| 197 |
if(depth == 1) |
| 198 |
cells= 2; |
| 199 |
|
| 200 |
/* add default colors */ |
| 201 |
c.red= c.green= c.blue= 0; |
| 202 |
c.pixel= BlackPixel(UxDisplay, UxScreen); |
| 203 |
add_color("xtdefaultforeground", &c); |
| 204 |
add_color("black", &c); |
| 205 |
|
| 206 |
c.red= c.green= c.blue= 65535; |
| 207 |
c.pixel= WhitePixel(UxDisplay, UxScreen); |
| 208 |
add_color("xtdefaultbackground", &c); |
| 209 |
add_color("white", &c); |
| 210 |
|
| 211 |
XtAppAddConverter(UxAppContext, XtRString, XtRColor, |
| 212 |
(XtConverter)string_to_xcolor, cvtarg, 1); |
| 213 |
/* Xt provides a XtRString to XtRPixel converter */ |
| 214 |
#ifdef NOT_NEEDED |
| 215 |
XtAppAddConverter(UxAppContext, XtRString, XtRPixel, |
| 216 |
(XtConverter)string_to_pixel, cvtarg, 1); |
| 217 |
#endif |
| 218 |
} |
| 219 |
|
| 220 |
/****************************************************************************** |
| 221 |
==== GLOBAL ================================================================= |
| 222 |
******************************************************************************* |
| 223 |
NAME: char *UxPixel_to_name(pixel) |
| 224 |
|
| 225 |
INPUT: Pixel pixel - pixel to convert |
| 226 |
|
| 227 |
RETURN: name associated with pixel |
| 228 |
|
| 229 |
DESCRIPTION: return the name of the color that corresponds to 'pixel'. |
| 230 |
The return is in a static buffer, therefore the next time |
| 231 |
this routine is called the previous value will be erased. |
| 232 |
|
| 233 |
CREATION: Jun 21/1989 |
| 234 |
REVISIONS: 04 1990 |
| 235 |
-- if lookup_by_pixel() fails, do not give up. |
| 236 |
Query the RGB values for that pixel value and construct a |
| 237 |
color of the form #rrggbb, if possible, or #rrrrggggbbbb |
| 238 |
-----------------------------------------------------------------------------*/ |
| 239 |
|
| 240 |
char *UxPixel_to_name(pixel) |
| 241 |
Pixel pixel; |
| 242 |
{ |
| 243 |
colortbl *val; |
| 244 |
static char msg[64]; |
| 245 |
XColor cdef; |
| 246 |
|
| 247 |
val= lookup_by_pixel(pixel); |
| 248 |
|
| 249 |
if(val != NULL) |
| 250 |
return(dnstr(val->name)); |
| 251 |
|
| 252 |
cdef.pixel = pixel; |
| 253 |
XQueryColor(XtDisplay(UxTopLevel), UxTopLevel->core.colormap, &cdef); |
| 254 |
|
| 255 |
/* Can we do a color of the form "#rrggbb"? This tends to be |
| 256 |
* the best resolution of most displays. |
| 257 |
*/ |
| 258 |
if (((cdef.red & 0xff) == (cdef.red >> 8)) && |
| 259 |
((cdef.blue & 0xff) == (cdef.blue >> 8)) && |
| 260 |
((cdef.green & 0xff) == (cdef.green >> 8))) { |
| 261 |
|
| 262 |
/* can do "#rrggbb" */ |
| 263 |
sprintf(msg, "#%02x%02x%02x", cdef.red & 0xff, cdef.green & 0xff, |
| 264 |
cdef.blue & 0xff); |
| 265 |
} else { |
| 266 |
/* must do "#rrrrggggbbbb" */ |
| 267 |
sprintf(msg, "#%04x%04x%04x", cdef.red, cdef.green, cdef.blue); |
| 268 |
} |
| 269 |
|
| 270 |
return(msg); |
| 271 |
} |
| 272 |
|
| 273 |
/****************************************************************************** |
| 274 |
==== GLOBAL ================================================================= |
| 275 |
******************************************************************************* |
| 276 |
NAME: Pixel UxName_to_pixel(name) |
| 277 |
|
| 278 |
INPUT: char *name - color name |
| 279 |
|
| 280 |
RETURN: pixel |
| 281 |
|
| 282 |
DESCRIPTION: return the pixel that is associated with 'name' |
| 283 |
|
| 284 |
CREATION: Jun 21/1989 |
| 285 |
REVISIONS: -- |
| 286 |
-----------------------------------------------------------------------------*/ |
| 287 |
|
| 288 |
Pixel UxName_to_pixel(name) |
| 289 |
char *name; |
| 290 |
{ |
| 291 |
colortbl *val; |
| 292 |
|
| 293 |
val= lookup_by_name(name); |
| 294 |
|
| 295 |
if(val == NULL) |
| 296 |
return(BlackPixel(UxDisplay, UxScreen)); |
| 297 |
|
| 298 |
return(val->color.pixel); |
| 299 |
} |
| 300 |
|
| 301 |
/****************************************************************************** |
| 302 |
==== LOCAL ================================================================== |
| 303 |
******************************************************************************* |
| 304 |
NAME: void add_color(cname, c) |
| 305 |
|
| 306 |
INPUT: char *cname - color name |
| 307 |
XColor *c - color value |
| 308 |
|
| 309 |
DESCRIPTION: add 'cname' to the color database so that it can be looked up |
| 310 |
by either name or pixel value. |
| 311 |
|
| 312 |
CREATION: Jun 21/1989 |
| 313 |
REVISIONS: -- |
| 314 |
-----------------------------------------------------------------------------*/ |
| 315 |
|
| 316 |
static void add_color(cname, c) |
| 317 |
char *cname; |
| 318 |
XColor *c; |
| 319 |
{ |
| 320 |
int hashv, phashv; |
| 321 |
colortbl *tbl; |
| 322 |
|
| 323 |
tbl= lookup_by_name(cname); |
| 324 |
|
| 325 |
if(tbl == NULL) |
| 326 |
{ tbl= (colortbl *) UxMalloc(sizeof(colortbl)); |
| 327 |
|
| 328 |
hashv= hash_name(cname); |
| 329 |
phashv= c->pixel%HASHSIZE; |
| 330 |
|
| 331 |
tbl->color= *c; |
| 332 |
tbl->name= dcreate(cname); |
| 333 |
tbl->nextc= chash[hashv]; |
| 334 |
tbl->nextp= phash[phashv]; |
| 335 |
|
| 336 |
chash[hashv]= tbl; |
| 337 |
phash[phashv]= tbl; |
| 338 |
} |
| 339 |
else |
| 340 |
tbl->color= *c; |
| 341 |
} |
| 342 |
|
| 343 |
static void LowerCase( src, dest) |
| 344 |
char *src; |
| 345 |
char *dest; |
| 346 |
{ |
| 347 |
while ( *src) |
| 348 |
{ |
| 349 |
if ( isupper( *src)) |
| 350 |
*dest = tolower( *src); |
| 351 |
else |
| 352 |
*dest = *src; |
| 353 |
src++; |
| 354 |
dest++; |
| 355 |
} |
| 356 |
*dest = '\0'; |
| 357 |
} |
| 358 |
/****************************************************************************** |
| 359 |
==== LOCAL ================================================================== |
| 360 |
******************************************************************************* |
| 361 |
NAME: void string_to_xcolor(args, num_args, from, to) |
| 362 |
|
| 363 |
INPUT: XrmValuePtr args - converter args |
| 364 |
Cardinal *num_args - number of args |
| 365 |
XrmValuePtr from - convert from... |
| 366 |
XrmValuePtr to - convert to... |
| 367 |
|
| 368 |
DESCRIPTION: This is the resource converter called by the toolkit to |
| 369 |
convert a string name to a color structure. It is registered |
| 370 |
with the toolkit by UxInit_colors. If the color is not |
| 371 |
yet allocated, this routine tries to allocate it. |
| 372 |
|
| 373 |
CREATION: Jun 21/1989 |
| 374 |
REVISIONS: July 24/90 (see bug798) |
| 375 |
-- skipped leading white spaces, and removed trailing white |
| 376 |
spaces. (see bug798) |
| 377 |
-----------------------------------------------------------------------------*/ |
| 378 |
static void string_to_xcolor(args, num_args, from, to) |
| 379 |
XrmValuePtr args; |
| 380 |
Cardinal *num_args; |
| 381 |
XrmValuePtr from; |
| 382 |
XrmValuePtr to; |
| 383 |
{ |
| 384 |
static XColor c; |
| 385 |
Colormap cmap; |
| 386 |
int rtrn; |
| 387 |
colortbl *tbl; |
| 388 |
char name_buf[128]; |
| 389 |
char *ptr; |
| 390 |
|
| 391 |
if(*num_args != 1) |
| 392 |
XtAppError(UxAppContext, |
| 393 |
CGETS( MS_MISC_NO_COLORMAP, DS_MS_MISC_NO_COLORMAP)); |
| 394 |
|
| 395 |
cmap= *((Colormap *) args[0].addr); |
| 396 |
|
| 397 |
/* skip leading and strip trailing whitespace */ |
| 398 |
ptr=UxStripLeadingAndTrailingSpaces(from->addr); |
| 399 |
|
| 400 |
LowerCase(ptr, name_buf); |
| 401 |
|
| 402 |
tbl= lookup_by_name(name_buf); |
| 403 |
|
| 404 |
if(tbl != NULL) |
| 405 |
{ c= tbl->color; |
| 406 |
to->addr= (void *)&c; |
| 407 |
to->size= sizeof(XColor); |
| 408 |
|
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
rtrn= XParseColor(UxDisplay, cmap, (char *)name_buf, &c); |
| 413 |
|
| 414 |
if(rtrn == 0) |
| 415 |
{ UxStandardError(CGETS( MS_MISC_NO_COLORNAME, |
| 416 |
DS_MS_MISC_NO_COLORNAME), name_buf); |
| 417 |
to->addr= NULL; |
| 418 |
to->size= 0; |
| 419 |
|
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
rtrn= XAllocColor(UxDisplay, cmap, &c); |
| 424 |
|
| 425 |
if(rtrn == 0) |
| 426 |
{ if(depth == 1) |
| 427 |
{ /* half color intensity */ |
| 428 |
if((c.red > 49152) || (c.green > 49152) || |
| 429 |
(c.blue > 49152)) |
| 430 |
{ c.red= c.green= c.blue= 65535; |
| 431 |
c.pixel= WhitePixel(UxDisplay, UxScreen); |
| 432 |
} |
| 433 |
else |
| 434 |
{ c.red= c.green= c.blue= 0; |
| 435 |
c.pixel= BlackPixel(UxDisplay, UxScreen); |
| 436 |
} |
| 437 |
|
| 438 |
add_color(from->addr, &c); |
| 439 |
|
| 440 |
to->addr= (void *)&c; |
| 441 |
to->size= sizeof(XColor); |
| 442 |
|
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
UxInternalError(__FILE__, __LINE__, |
| 447 |
CGETS( MS_MISC_COLORMAP_ALLOC, |
| 448 |
DS_MS_MISC_COLORMAP_ALLOC), from->addr); |
| 449 |
to->addr= NULL; |
| 450 |
to->size= 0; |
| 451 |
|
| 452 |
return; |
| 453 |
} |
| 454 |
|
| 455 |
add_color(from->addr, &c); |
| 456 |
|
| 457 |
to->addr= (void *)&c; |
| 458 |
to->size= sizeof(XColor); |
| 459 |
} |
| 460 |
|
| 461 |
/* Xt provides a XtRString to XtRPixel converter */ |
| 462 |
#ifdef NOT_NEEDED |
| 463 |
/****************************************************************************** |
| 464 |
==== LOCAL ================================================================== |
| 465 |
******************************************************************************* |
| 466 |
NAME: void string_to_pixel(args, num_args, from, to) |
| 467 |
|
| 468 |
INPUT: XrmValuePtr args - converter args |
| 469 |
Cardinal *num_args - number of args |
| 470 |
XrmValuePtr from - value to convert |
| 471 |
XrmValuePtr to - return value |
| 472 |
|
| 473 |
DESCRIPTION: This is the toolkit resource converter for converter a |
| 474 |
string color name to a pixel value. It is registered by |
| 475 |
UxInit_colors. If the color is not yet allocated it is |
| 476 |
allocated if possible. |
| 477 |
|
| 478 |
CREATION: Jun 21/1989 |
| 479 |
REVISIONS: -- |
| 480 |
-----------------------------------------------------------------------------*/ |
| 481 |
|
| 482 |
static void string_to_pixel(args, num_args, from, to) |
| 483 |
XrmValuePtr args; |
| 484 |
Cardinal *num_args; |
| 485 |
XrmValuePtr from; |
| 486 |
XrmValuePtr to; |
| 487 |
{ |
| 488 |
XColor *c; |
| 489 |
|
| 490 |
string_to_xcolor(args, num_args, from, to); |
| 491 |
|
| 492 |
if(to->addr != NULL) |
| 493 |
{ c= (XColor *)to->addr; |
| 494 |
to->addr= (void *)&(c->pixel); |
| 495 |
to->size= sizeof(int); |
| 496 |
} |
| 497 |
} |
| 498 |
#endif |