home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1999 March / PCShareware-3-99.iso / IMPLE / DJGPP.RAR / DJGPP2 / XLIB-SR0.ZIP / SRC / XLIBEMU / COLOR.C < prev    next >
C/C++ Source or Header  |  1994-01-14  |  4KB  |  198 lines

  1. /* $Id: color.c 1.2 1994/01/15 02:13:01 ulrich Exp $ */
  2. /*
  3.  * color.c
  4.  *
  5.  * X library color functions.
  6.  * Colormap parameters are ignored (only StdColormap).
  7.  */
  8. #include "Xlibemu.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. Status XAllocColor(
  14.     Display*        display,
  15.     Colormap        colormap,
  16.     XColor*        screen_in_out)
  17. {
  18.   int r = screen_in_out->red   >> 8;
  19.   int g = screen_in_out->green >> 8;
  20.   int b = screen_in_out->blue  >> 8;
  21.   int pixel = GrAllocColor (r, g, b);
  22.  
  23.   if (pixel != GrNOCOLOR) {
  24.     screen_in_out->pixel = pixel;
  25.     return 1;
  26.   }
  27.   return 0;
  28. }
  29.  
  30. static struct _ColorEntry {
  31.   unsigned int r:8;
  32.   unsigned int g:8;
  33.   unsigned int b:8;
  34.   char *name;
  35. } _ColorTable[] = {
  36. # include "rgb.h"
  37. };
  38.  
  39. static int _ColorTableSorted = 0;
  40.  
  41. static int CompareColorEntry (const void *c1, const void *c2)
  42. {
  43.   return strcasecmp (((struct _ColorEntry *) c1)->name,
  44.              ((struct _ColorEntry *) c2)->name);
  45. }
  46.  
  47. static void SortColorTable ()
  48. {
  49.   if (_ColorTableSorted == 0) {
  50.     qsort (_ColorTable,
  51.        sizeof(_ColorTable) / sizeof(_ColorTable[0]),
  52.        sizeof(_ColorTable[0]),
  53.        CompareColorEntry);
  54.     _ColorTableSorted = 1;
  55.   }
  56. }
  57.  
  58.  
  59. static int
  60. _WParseColor (char *name, XColor *color)
  61. {
  62.   struct _ColorEntry *entry, key;
  63.  
  64.   if (!name || !color)
  65.     return 0; /* not found */;
  66.  
  67.   if (name[0] == '#')
  68.     {
  69.       unsigned int r, g, b;
  70.       if (sscanf (name, "#%4x%4x%4x", &r, &g, &b) == 3)
  71.     {
  72.       color->red   = r;
  73.       color->green = g;
  74.       color->blue  = b;
  75.       return 1;
  76.     }
  77.       if (sscanf (name, "#%2x%2x%2x", &r, &g, &b) == 3)
  78.     {
  79.       color->red   = (r & 0xff) * 257;
  80.       color->green = (g & 0xff) * 257;
  81.       color->blue  = (b & 0xff) * 257;
  82.       return 1;
  83.     }
  84.       return 0; /* not found */;
  85.     }
  86.  
  87.   SortColorTable ();
  88.   key.name = name;
  89.   entry = bsearch (&key, _ColorTable,
  90.            sizeof(_ColorTable) / sizeof(_ColorTable[0]),
  91.            sizeof(_ColorTable[0]),
  92.            CompareColorEntry);
  93.   if (entry == NULL)
  94.     return 0;
  95.  
  96.   color->red   = entry->r * 257;
  97.   color->green = entry->g * 257;
  98.   color->blue  = entry->b * 257;
  99.   return 1;
  100. }
  101.  
  102.  
  103. Status XParseColor(
  104.     Display*        display,
  105.     Colormap        colormap,
  106.     _Xconst char*    spec,
  107.     XColor*        exact_def_return)
  108. {
  109.   if (_WParseColor ((char *) spec, exact_def_return)) {
  110.     exact_def_return->flags = DoRed|DoGreen|DoBlue;
  111.     return 1;
  112.   }
  113.   return 0;
  114. }
  115.  
  116. int
  117. XQueryColor(
  118.     Display*        display,
  119.     Colormap        colormap,
  120.     XColor*        def_in_out)
  121. {
  122.   return XQueryColors (display, colormap, def_in_out, 1);
  123. }
  124.  
  125. XQueryColors(
  126.     Display*        display,
  127.     Colormap        colormap,
  128.     XColor*        defs_in_out,
  129.     int            ncolors)
  130. {
  131.   int r, g, b;
  132.  
  133.   while (--ncolors >= 0) {
  134.     GrQueryColor (defs_in_out->pixel, &r, &g, &b);
  135.     defs_in_out->red   = r * 257;
  136.     defs_in_out->green = g * 257;
  137.     defs_in_out->blue  = b * 257;
  138.     defs_in_out++;
  139.   }
  140.   return 0;
  141. }
  142.  
  143.  
  144. Status XAllocNamedColor(
  145.     Display*        display,
  146.     Colormap        colormap,
  147.     _Xconst char*    color_name,
  148.     XColor*        screen_def_return,
  149.     XColor*        exact_def_return)
  150. {
  151.   if (XParseColor (display, colormap, color_name, exact_def_return))
  152.     {
  153.       int status;
  154.       *screen_def_return = *exact_def_return;
  155.       status = XAllocColor (display, colormap, screen_def_return);
  156.       exact_def_return->pixel = screen_def_return->pixel;
  157.       return status;
  158.     }
  159.   return 0;
  160. }
  161.  
  162. int XFreeColors(
  163.     Display*        display,
  164.     Colormap        colormap,
  165.     unsigned long*    pixels,
  166.     int            npixels,
  167.     unsigned long    planes)
  168. {
  169.   int i;
  170.  
  171.   for (i = 0; i < npixels; i++) 
  172.     {
  173.       /* if (pixel[i] & planes) */
  174.       GrFreeColor (pixels[i]);
  175.     }
  176.   return 0;
  177. }
  178.  
  179. Status XLookupColor(
  180.     Display*        display,
  181.     Colormap        colormap,
  182.     _Xconst char*    color_name,
  183.     XColor*        exact_def_return,
  184.     XColor*        screen_def_return)
  185. {
  186.   if (XParseColor (display, colormap, color_name, exact_def_return))
  187.     {
  188.       *screen_def_return = *exact_def_return;
  189.       if (XAllocColor (display, colormap, screen_def_return)) {
  190.     XQueryColor (display, colormap, screen_def_return);
  191.     XFreeColors (display, colormap, &screen_def_return->pixel, 1,
  192.              AllPlanes);
  193.     return 1;
  194.       }
  195.     }
  196.   return 0;
  197. }
  198.