Distinct.c revision 6c321187
16c321187Smrg/* $Xorg: Distinct.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */ 26c321187Smrg 36c321187Smrg/* 46c321187Smrg 56c321187SmrgCopyright 1990, 1998 The Open Group 66c321187Smrg 76c321187SmrgPermission to use, copy, modify, distribute, and sell this software and its 86c321187Smrgdocumentation for any purpose is hereby granted without fee, provided that 96c321187Smrgthe above copyright notice appear in all copies and that both that 106c321187Smrgcopyright notice and this permission notice appear in supporting 116c321187Smrgdocumentation. 126c321187Smrg 136c321187SmrgThe above copyright notice and this permission notice shall be included in 146c321187Smrgall copies or substantial portions of the Software. 156c321187Smrg 166c321187SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 176c321187SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 186c321187SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 196c321187SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 206c321187SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 216c321187SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 226c321187Smrg 236c321187SmrgExcept as contained in this notice, the name of The Open Group shall not be 246c321187Smrgused in advertising or otherwise to promote the sale, use or other dealings 256c321187Smrgin this Software without prior written authorization from The Open Group. 266c321187Smrg 276c321187Smrg*/ 286c321187Smrg/* $XFree86: xc/lib/Xmu/Distinct.c,v 3.5 2001/07/25 15:04:50 dawes Exp $ */ 296c321187Smrg 306c321187Smrg/* 316c321187Smrg * Author: Keith Packard, MIT X Consortium 326c321187Smrg */ 336c321187Smrg 346c321187Smrg#ifdef HAVE_CONFIG_H 356c321187Smrg#include <config.h> 366c321187Smrg#endif 376c321187Smrg#include <X11/Xlib.h> 386c321187Smrg#include <stdlib.h> 396c321187Smrg#include <X11/Xutil.h> 406c321187Smrg#include <X11/Xmu/StdCmap.h> 416c321187Smrg 426c321187Smrg/* 436c321187Smrg * Distinguishable colors routine. Determines if two colors are 446c321187Smrg * distinguishable or not. Somewhat arbitrary meaning. 456c321187Smrg */ 466c321187Smrg 476c321187Smrg#define MIN_DISTINGUISH 10000.0 486c321187Smrg 496c321187SmrgBool 506c321187SmrgXmuDistinguishableColors(XColor *colors, int count) 516c321187Smrg{ 526c321187Smrg double deltaRed, deltaGreen, deltaBlue; 536c321187Smrg double dist; 546c321187Smrg int i, j; 556c321187Smrg 566c321187Smrg for (i = 0; i < count - 1; i++) 576c321187Smrg for (j = i + 1; j < count; j++) 586c321187Smrg { 596c321187Smrg deltaRed = (double)colors[i].red - (double)colors[j].red; 606c321187Smrg deltaGreen = (double)colors[i].green - (double)colors[j].green; 616c321187Smrg deltaBlue = (double)colors[i].blue - (double)colors[j].blue; 626c321187Smrg dist = deltaRed * deltaRed + 636c321187Smrg deltaGreen * deltaGreen + 646c321187Smrg deltaBlue * deltaBlue; 656c321187Smrg if (dist <= MIN_DISTINGUISH * MIN_DISTINGUISH) 666c321187Smrg return False; 676c321187Smrg } 686c321187Smrg return True; 696c321187Smrg} 706c321187Smrg 716c321187SmrgBool 726c321187SmrgXmuDistinguishablePixels(Display *dpy, Colormap cmap, 736c321187Smrg unsigned long *pixels, int count) 746c321187Smrg{ 756c321187Smrg XColor *defs; 766c321187Smrg int i, j; 776c321187Smrg Bool ret; 786c321187Smrg 796c321187Smrg for (i = 0; i < count - 1; i++) 806c321187Smrg for (j = i + 1; j < count; j++) 816c321187Smrg if (pixels[i] == pixels[j]) 826c321187Smrg return False; 836c321187Smrg defs = (XColor *) malloc (count * sizeof (XColor)); 846c321187Smrg if (!defs) 856c321187Smrg return False; 866c321187Smrg for (i = 0; i < count; i++) 876c321187Smrg defs[i].pixel = pixels[i]; 886c321187Smrg XQueryColors (dpy, cmap, defs, count); 896c321187Smrg ret = XmuDistinguishableColors (defs, count); 906c321187Smrg free ((char *) defs); 916c321187Smrg return ret; 926c321187Smrg} 93