home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_ParseCol.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  4KB  |  133 lines

  1. /* $XConsortium: ParseCol.c,v 11.31 94/04/17 20:20:22 rws Exp $ */
  2. /*
  3.  
  4. Copyright (c) 1985  X Consortium
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  19. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  
  23. Except as contained in this notice, the name of the X Consortium shall not be
  24. used in advertising or otherwise to promote the sale, use or other dealings
  25. in this Software without prior written authorization from the X Consortium.
  26.  
  27. */
  28.  
  29. #define NEED_REPLIES
  30. #include <stdio.h>
  31. #include "Xlib_private.h"
  32. #include "Xcmsint.h"
  33.  
  34. extern void _XcmsRGB_to_XColor();
  35.  
  36. #if NeedFunctionPrototypes
  37. Status XParseColor (
  38.     register Display *dpy,
  39.         Colormap cmap,
  40.     _Xconst char *spec,
  41.     XColor *def)
  42. #else
  43. Status XParseColor (dpy, cmap, spec, def)
  44.     register Display *dpy;
  45.         Colormap cmap;
  46.     char *spec;
  47.     XColor *def;
  48. #endif
  49. {
  50.     DBUG_ENTER("XParseColor")
  51.     register int n, i;
  52.     int r, g, b;
  53.     char c;
  54.     XcmsCCC ccc;
  55.     XcmsColor cmsColor;
  56.  
  57.         if (!spec) DBUG_RETURN(0);
  58.     n = strlen (spec);
  59.     if (*spec == '#') {
  60.         /*
  61.          * RGB
  62.          */
  63.         spec++;
  64.         n--;
  65.         if (n != 3 && n != 6 && n != 9 && n != 12)
  66.         DBUG_RETURN(0);
  67.         n /= 3;
  68.         g = b = 0;
  69.         do {
  70.         r = g;
  71.         g = b;
  72.         b = 0;
  73.         for (i = n; --i >= 0; ) {
  74.             c = *spec++;
  75.             b <<= 4;
  76.             if (c >= '0' && c <= '9')
  77.             b |= c - '0';
  78.             else if (c >= 'A' && c <= 'F')
  79.             b |= c - ('A' - 10);
  80.             else if (c >= 'a' && c <= 'f')
  81.             b |= c - ('a' - 10);
  82.             else DBUG_RETURN(0);
  83.         }
  84.         } while (*spec != '\0');
  85.         n <<= 2;
  86.         n = 16 - n;
  87.         def->red = r << n;
  88.         def->green = g << n;
  89.         def->blue = b << n;
  90.         def->flags = DoRed | DoGreen | DoBlue;
  91.         DBUG_RETURN(1);
  92.     }
  93.  
  94.  
  95.     /*
  96.      * Let's Attempt to use Xcms and i18n approach to Parse Color
  97.      */
  98.     if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) {
  99.         if (_XcmsResolveColorString(ccc, &spec,
  100.             &cmsColor, XcmsRGBFormat) >= XcmsSuccess) {
  101.         cmsColor.pixel = def->pixel;
  102.         _XcmsRGB_to_XColor(&cmsColor, def, 1);
  103.         DBUG_RETURN(1);
  104.         }
  105.         /*
  106.          * Otherwise we failed; or spec was changed with yet another
  107.          * name.  Thus pass name to the X Server.
  108.          */
  109.     }
  110.  
  111.     /*
  112.      * Xcms and i18n methods failed.
  113.      */
  114.     n = strlen (spec);
  115.     if (!OsLookupColor(0,spec,n,&r,&g,&b)) {
  116.         SyncHandle();
  117.         DBUG_RETURN(0);
  118.     }
  119.     
  120.     def->red   = r;
  121.     def->green = g;
  122.     def->blue  = b;
  123.  
  124.     /* need to put code here for proper colormap translation! */
  125.     def->pixel = 
  126.     ((unsigned long)r << 8)|((unsigned long)g)|((unsigned long)b >> 8);
  127.  
  128.     /*printf("Color '%s' -> (%d,%d,%d) = #%06x\n",colorname,r>>8,g>>8,b>>8,def->pixel);*/
  129.  
  130.     SyncHandle();
  131.     DBUG_RETURN(1);
  132. }
  133.