Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/toolx/X11/colors is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef toolx_X11_colors
0005 #define toolx_X11_colors
0006 
0007 #include <X11/Xlib.h>
0008 #include <vector>
0009 #include <map>
0010 
0011 namespace toolx {
0012 namespace X11 {
0013 
0014 typedef unsigned long Pixel;
0015 typedef std::vector<Pixel> pixels_t;
0016 
0017 typedef unsigned int rgb_t;
0018 typedef std::map<rgb_t,Pixel> colors_t;
0019 
0020 inline bool rgb2pixel(Display* a_display,unsigned int a_monitor,
0021                       unsigned char a_r,unsigned char a_g,unsigned char a_b,
0022                       Pixel& a_pixel,bool& a_allocated) {
0023   // read_write visual class: PseudoColor, GrayScale, DirectColor.
0024   // read_only  visual class: TrueColor, StaticColor, StaticGray.
0025   // Could allocate a read_only cell in a read_write, read_only visual class.
0026   // Could allocate a read_write cell only in read_write visual class.
0027   // StaticGray + (depth=1) is monochrome.
0028 
0029   Screen* screen = ::XScreenOfDisplay(a_display,a_monitor);
0030   int vclass = ::XDefaultVisualOfScreen(screen)->c_class;
0031   Colormap cmap = ::XDefaultColormapOfScreen(screen);
0032 
0033   bool is_grey  = ((vclass==GrayScale) || (vclass==StaticGray) ) ? true : false;
0034 
0035   float r = float(a_r)/255.0f;
0036   float g = float(a_g)/255.0f;
0037   float b = float(a_b)/255.0f;
0038   
0039   XColor xcolor;
0040   xcolor.pixel  = 0L;
0041   if(is_grey) {
0042     float grey = 0.30f * r + 0.59f * g + 0.11f * b;
0043     xcolor.red    = (unsigned short) (grey * 0xffff);
0044     xcolor.green  = (unsigned short) (grey * 0xffff);
0045     xcolor.blue   = (unsigned short) (grey * 0xffff);
0046   } else {
0047     xcolor.red    = (unsigned short) (r  * 0xffff);
0048     xcolor.green  = (unsigned short) (g  * 0xffff);
0049     xcolor.blue   = (unsigned short) (b  * 0xffff);
0050   }
0051 
0052   // Could be done on read_only/read_write visual class.
0053   // Cell is taken from a common pool. It is read only.
0054   if(::XAllocColor(a_display,cmap,&xcolor)==0) {
0055     // Color not found. Try to allocate a private color cell :
0056     if((vclass==TrueColor)||(vclass==StaticColor)||(vclass==StaticGray)) { //Viusal class is read_only.
0057       a_pixel = 0;
0058       a_allocated = false;
0059       return false;
0060     }
0061     if(::XAllocColorCells(a_display,cmap,False,NULL,0,&(xcolor.pixel),1)==0) { //read/write cell.
0062       a_pixel = 0;
0063       a_allocated = false;
0064       return false;
0065     }
0066     xcolor.flags = DoRed|DoGreen|DoBlue;
0067     ::XStoreColor(a_display,cmap,&xcolor);
0068     a_pixel = xcolor.pixel;
0069     a_allocated = true;
0070   } else {
0071     a_pixel = xcolor.pixel;
0072     a_allocated = false;
0073   }
0074 
0075   return true;
0076 }
0077 
0078 inline bool get_pixel(Display* a_display,unsigned int a_monitor,
0079                       pixels_t& a_pixels,colors_t& a_colors,
0080                       unsigned int a_rgba,
0081                       Pixel& a_pixel) {
0082   colors_t::const_iterator it = a_colors.find(a_rgba);
0083   if(it!=a_colors.end()) {a_pixel = (*it).second;return true;}
0084   unsigned char* pos = (unsigned char*)&a_rgba;
0085   bool allocated;
0086   if(!rgb2pixel(a_display,a_monitor,*(pos+0),*(pos+1),*(pos+2),a_pixel,allocated)) {
0087     Screen* screen = ::XScreenOfDisplay(a_display,a_monitor);
0088     a_pixel = XWhitePixelOfScreen(screen);
0089     return false;
0090   }
0091   if(allocated) a_pixels.push_back(a_pixel);
0092   a_colors[a_rgba] = a_pixel;
0093   return true;
0094 }
0095 
0096 inline void set_rgb_t(unsigned char a_r,unsigned char a_g,unsigned char a_b,rgb_t& a_rgb) {
0097   unsigned char* pos = (unsigned char*)&a_rgb;
0098   *pos = a_r;pos++;
0099   *pos = a_g;pos++;
0100   *pos = a_b;pos++;
0101   *pos = 0;pos++;
0102 }
0103 
0104 inline bool set_foreground(Display* a_display,unsigned int a_monitor,GC a_gc,pixels_t& a_pixels,colors_t& a_colors,unsigned char a_r,unsigned char a_g,unsigned char a_b) {
0105   Pixel pixel;
0106   rgb_t rgb;
0107   set_rgb_t(a_r,a_g,a_b,rgb);
0108   if(!get_pixel(a_display,a_monitor,a_pixels,a_colors,rgb,pixel)) return false;
0109   XGCValues gcv;
0110   gcv.foreground = pixel;
0111   if(::XChangeGC(a_display,a_gc,GCForeground,&gcv)==0) return false;
0112   return true;
0113 }
0114 
0115 inline bool set_foregroundf(Display* a_display,unsigned int a_monitor,GC a_gc,pixels_t& a_pixels,colors_t& a_colors,float a_r,float a_g,float a_b) {
0116   return set_foreground(a_display,a_monitor,a_gc,a_pixels,a_colors,a_r*255.0f,a_g*255.0f,a_b*255.0f);
0117 }
0118 
0119 }}
0120 
0121 
0122 #endif