home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / Distinct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.7 KB  |  84 lines

  1. /*
  2.  * $XConsortium: Distinct.c,v 1.3 90/12/28 19:12:21 gildea Exp $
  3.  *
  4.  * Copyright 1990 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Keith Packard, MIT X Consortium
  24.  */
  25.  
  26. # include   <X11/Xlib.h>
  27.  
  28. /*
  29.  * Distinguishable colors routine.  Determines if two colors are
  30.  * distinguishable or not.  Somewhat arbitrary meaning.
  31.  */
  32.  
  33. #define MIN_DISTINGUISH    10000.0
  34.  
  35. Bool
  36. XmuDistinguishableColors (colors, count)
  37. XColor    *colors;
  38. int    count;
  39. {
  40.     double        deltaRed, deltaGreen, deltaBlue;
  41.     double        dist;
  42.     int            i, j;
  43.  
  44.     for (i = 0; i < count - 1; i++)
  45.     for (j = i + 1; j < count; j++)
  46.     {
  47.              deltaRed = (double)colors[i].red - (double)colors[j].red;
  48.             deltaGreen = (double)colors[i].green - (double)colors[j].green;
  49.             deltaBlue = (double)colors[i].blue - (double)colors[j].blue;
  50.             dist = deltaRed * deltaRed +
  51.                   deltaGreen * deltaGreen +
  52.                    deltaBlue * deltaBlue;
  53.         if (dist <= MIN_DISTINGUISH * MIN_DISTINGUISH)
  54.         return False;
  55.     }
  56.     return True;
  57. }
  58.  
  59. Bool
  60. XmuDistinguishablePixels (dpy, cmap, pixels, count)
  61.     Display        *dpy;
  62.     Colormap        cmap;
  63.     unsigned long   *pixels;
  64.     int            count;
  65. {
  66.     XColor  *defs;
  67.     int        i, j;
  68.     Bool    ret;
  69.  
  70.     for (i = 0; i < count - 1; i++)
  71.     for (j = i + 1; j < count; j++)
  72.         if (pixels[i] == pixels[j])
  73.         return False;
  74.     defs = (XColor *) malloc (count * sizeof (XColor));
  75.     if (!defs)
  76.     return False;
  77.     for (i = 0; i < count; i++)
  78.     defs[i].pixel = pixels[i];
  79.     XQueryColors (dpy, cmap, defs, count);
  80.     ret = XmuDistinguishableColors (defs, count);
  81.     free ((char *) defs);
  82.     return ret;
  83. }
  84.