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