Logo Search packages:      
Sourcecode: dfm version File versions  Download package

color.c

/**************************************************************
  (C) 1997 by Achim Kaiser

  You may distribute under the terms of the GNU General Public
  License as specified in the COPYING file.
**************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdlib.h>
#include "global.h"
#include "color.h"

/* Reserves Colors for PseudoColor mode. Look into color.h for the number of
   colors to be allocated (MAXCOLORS). If an other programm has already
   allocated MAXCOLORS colors then InitColors does nothing but allocates them 
   once again. */
void InitColors()
{
  Colormap cmap;
  XColor pixs[MAXCOLORS];
  XColor pix;
  unsigned long a,b;
  
  if (XDefaultVisual(GetDisplay(),
                     XDefaultScreen(GetDisplay()))->class==PseudoColor) {
    cmap=XDefaultColormap(GetDisplay(),XDefaultScreen(GetDisplay()));
    a=0;
    do {
      b =(((a>>0) & 1)^((a>>3) & 1 )^((a>>6) & 1)) << 0 ;
      b|=(((a>>0) & 1)^((a>>3) & 1 ))              << 1 ;
      b|=(((a>>0) & 1))                            << 2 ;
      b|=(((a>>1) & 1)^((a>>4) & 1 )^((a>>7) & 1)) << 3 ;
      b|=(((a>>1) & 1)^((a>>4) & 1 ))              << 4 ;
      b|=(((a>>1) & 1))                            << 5 ;
      b|=(((a>>2) & 1)^((a>>5) & 1 )^((a>>8) & 1)) << 6 ;
      b|=(((a>>2) & 1)^((a>>5) & 1 ))              << 7 ;
      b|=(((a>>2) & 1))                            << 8 ;
      pix.red=9362*(448&b)/64;
      pix.green=9362*(56&b)/8;
      pix.blue=9362*(7&b);
      if(XAllocColor(GetDisplay(),cmap,&pix)==0)
      a=255;
      a++;
    } while (pix.pixel<MAXCOLORS && a!=256);
    if (a<256)
      XFreeColors(GetDisplay(),cmap,&pix.pixel,1,0);
    for (a=0;a<MAXCOLORS;a++) 
      pixs[a].pixel=a;
    XQueryColors(GetDisplay(),cmap,pixs,MAXCOLORS);
    for (a=0;a<MAXCOLORS;a++)
      XAllocColor(GetDisplay(),cmap,&pixs[a]);
  }
}

/* GetBestColor returns the pixel representing the requested color in the 
   DefaultColormap. In case of Pseudocolor it chooses the nearest color in 
   the DefaultColormap (not using colors which numbers are > MAXCOLORS) */
unsigned long GetBestColor(int r,int g,int b)
{    
  Colormap cmap;
  XColor backcolor;
  static XColor *pixcolors=NULL;
  long difference,localdifference;
  int i;
  unsigned long bg=0;
  
  backcolor.red=r;
  backcolor.green=g;
  backcolor.blue=b;
  cmap=XDefaultColormap(GetDisplay(),XDefaultScreen(GetDisplay()));
  if (XDefaultVisual(GetDisplay(),
                     XDefaultScreen(GetDisplay()))->class!=PseudoColor && 
      XAllocColor(GetDisplay(),cmap,&backcolor)!=0)
    bg=backcolor.pixel;
  else {
    if (pixcolors==NULL) {
      pixcolors=malloc(MAXCOLORS*sizeof(XColor));
      for (i=0;i<MAXCOLORS;i++) 
      pixcolors[i].pixel=(unsigned long)i;
      XQueryColors(GetDisplay(),cmap,pixcolors,MAXCOLORS);
    }
    difference=65536*3;
    for (i=0;i<MAXCOLORS;i++) {
      localdifference=labs((long)pixcolors[i].red-(long)backcolor.red)+
                  labs((long)pixcolors[i].green-(long)backcolor.green)+
                  labs((long)pixcolors[i].blue-(long)backcolor.blue);      
      if (localdifference<difference) {
      difference=localdifference;
      bg=(unsigned long)i;
      }
    }
  }
  return bg;
}

Generated by  Doxygen 1.6.0   Back to index