Distinct.c

Go to the documentation of this file.
00001 /* $Xorg: Distinct.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */
00002 
00003 /*
00004 
00005 Copyright 1990, 1998  The Open Group
00006 
00007 Permission to use, copy, modify, distribute, and sell this software and its
00008 documentation for any purpose is hereby granted without fee, provided that
00009 the above copyright notice appear in all copies and that both that
00010 copyright notice and this permission notice appear in supporting
00011 documentation.
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of The Open Group shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from The Open Group.
00026 
00027 */
00028 /* $XFree86: xc/lib/Xmu/Distinct.c,v 3.5 2001/07/25 15:04:50 dawes Exp $ */
00029 
00030 /*
00031  * Author:  Keith Packard, MIT X Consortium
00032  */
00033 
00034 #include <X11/Xlib.h>
00035 #include <stdlib.h>
00036 #include <X11/Xutil.h>
00037 #include <X11/Xmu/StdCmap.h>
00038 
00039 /*
00040  * Distinguishable colors routine.  Determines if two colors are
00041  * distinguishable or not.  Somewhat arbitrary meaning.
00042  */
00043 
00044 #define MIN_DISTINGUISH 10000.0
00045 
00046 Bool
00047 XmuDistinguishableColors(XColor *colors, int count)
00048 {
00049     double          deltaRed, deltaGreen, deltaBlue;
00050     double          dist;
00051     int             i, j;
00052 
00053     for (i = 0; i < count - 1; i++)
00054         for (j = i + 1; j < count; j++)
00055         {
00056             deltaRed = (double)colors[i].red - (double)colors[j].red;
00057             deltaGreen = (double)colors[i].green - (double)colors[j].green;
00058             deltaBlue = (double)colors[i].blue - (double)colors[j].blue;
00059             dist = deltaRed * deltaRed +
00060                    deltaGreen * deltaGreen +
00061                    deltaBlue * deltaBlue;
00062             if (dist <= MIN_DISTINGUISH * MIN_DISTINGUISH)
00063                 return False;
00064         }
00065     return True;
00066 }
00067 
00068 Bool
00069 XmuDistinguishablePixels(Display *dpy, Colormap cmap,
00070                          unsigned long *pixels, int  count)
00071 {
00072     XColor  *defs;
00073     int     i, j;
00074     Bool    ret;
00075 
00076     for (i = 0; i < count - 1; i++)
00077         for (j = i + 1; j < count; j++)
00078             if (pixels[i] == pixels[j])
00079                 return False;
00080     defs = (XColor *) malloc (count * sizeof (XColor));
00081     if (!defs)
00082         return False;
00083     for (i = 0; i < count; i++)
00084         defs[i].pixel = pixels[i];
00085     XQueryColors (dpy, cmap, defs, count);
00086     ret = XmuDistinguishableColors (defs, count);
00087     free ((char *) defs);
00088     return ret;
00089 }

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1