MCPcopy Create free account
hub / github.com/NetHack/NetHack / nhApproxColor

Function nhApproxColor

win/X11/winX.c:423–497  ·  view source on GitHub ↗

* Find a color that approximates the color named in "str". * The "str" color may be a color name ("red") or number ("#7f0000"). * If str is Null, then "color" is assumed to contain the RGB color wanted. * The approximate color found is returned in color as well. * Return True if something close was found. */

Source from the content-addressed store, hash-verified

421 * Return True if something close was found.
422 */
423Boolean
424nhApproxColor(
425 Screen *screen, /* screen to use */
426 Colormap colormap, /* the colormap to use */
427 char *str, /* color name */
428 XColor *color) /* the X color structure; changed only if successful */
429{
430 int ncells;
431 long cdiff = 16777216; /* 2^24; hopefully our map is smaller */
432 XColor tmp;
433 static XColor *table = 0;
434 int i, j;
435 long tdiff;
436
437 /* if the screen doesn't have a big colormap, don't waste our time
438 or if it's huge, and _some_ match should have been possible */
439 if ((ncells = CellsOfScreen(screen)) < 256 || ncells > 4096)
440 return False;
441
442 if (str != (char *) 0) {
443 if (!XParseColor(DisplayOfScreen(screen), colormap, str, &tmp))
444 return False;
445 } else {
446 tmp = *color;
447 tmp.flags = 7; /* force to use all 3 of RGB */
448 }
449
450 if (!table) {
451 table = (XColor *) XtCalloc(ncells, sizeof(XColor));
452 for (i = 0; i < ncells; i++)
453 table[i].pixel = i;
454 XQueryColors(DisplayOfScreen(screen), colormap, table, ncells);
455 }
456
457 /* go thru cells and look for the one with smallest diff;
458 diff is calculated abs(reddiff)+abs(greendiff)+abs(bluediff);
459 a more knowledgeable color person might improve this -dlc */
460 try_again:
461 for (i = 0; i < ncells; i++) {
462 if (table[i].flags == tmp.flags) {
463 j = (int) table[i].red - (int) tmp.red;
464 if (j < 0)
465 j = -j;
466 tdiff = j;
467 j = (int) table[i].green - (int) tmp.green;
468 if (j < 0)
469 j = -j;
470 tdiff += j;
471 j = (int) table[i].blue - (int) tmp.blue;
472 if (j < 0)
473 j = -j;
474 tdiff += j;
475 if (tdiff < cdiff) {
476 cdiff = tdiff;
477 tmp.pixel = i; /* table[i].pixel == i */
478 }
479 }
480 }

Callers 2

nhCvtStringToPixelFunction · 0.85
init_tilesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected