Imported Upstream version 1.1.1
[platform/upstream/libXmu.git] / src / Distinct.c
1 /*
2
3 Copyright 1990, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25 */
26
27 /*
28  * Author:  Keith Packard, MIT X Consortium
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 #include <X11/Xlib.h>
35 #include <stdlib.h>
36 #include <X11/Xutil.h>
37 #include <X11/Xmu/StdCmap.h>
38
39 /*
40  * Distinguishable colors routine.  Determines if two colors are
41  * distinguishable or not.  Somewhat arbitrary meaning.
42  */
43
44 #define MIN_DISTINGUISH 10000.0
45
46 Bool
47 XmuDistinguishableColors(XColor *colors, int count)
48 {
49     double          deltaRed, deltaGreen, deltaBlue;
50     double          dist;
51     int             i, j;
52
53     for (i = 0; i < count - 1; i++)
54         for (j = i + 1; j < count; j++)
55         {
56             deltaRed = (double)colors[i].red - (double)colors[j].red;
57             deltaGreen = (double)colors[i].green - (double)colors[j].green;
58             deltaBlue = (double)colors[i].blue - (double)colors[j].blue;
59             dist = deltaRed * deltaRed +
60                    deltaGreen * deltaGreen +
61                    deltaBlue * deltaBlue;
62             if (dist <= MIN_DISTINGUISH * MIN_DISTINGUISH)
63                 return False;
64         }
65     return True;
66 }
67
68 Bool
69 XmuDistinguishablePixels(Display *dpy, Colormap cmap,
70                          unsigned long *pixels, int  count)
71 {
72     XColor  *defs;
73     int     i, j;
74     Bool    ret;
75
76     for (i = 0; i < count - 1; i++)
77         for (j = i + 1; j < count; j++)
78             if (pixels[i] == pixels[j])
79                 return False;
80     defs = (XColor *) malloc (count * sizeof (XColor));
81     if (!defs)
82         return False;
83     for (i = 0; i < count; i++)
84         defs[i].pixel = pixels[i];
85     XQueryColors (dpy, cmap, defs, count);
86     ret = XmuDistinguishableColors (defs, count);
87     free ((char *) defs);
88     return ret;
89 }