"Fossies" - the Fresh Open Source Software Archive

Member "Ted-2.23/appFrame/appColorGtk.c" (1 Feb 2013, 8263 Bytes) of archive /linux/misc/ted-2.23.src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "appColorGtk.c" see the Fossies "Dox" file reference documentation.

    1 /************************************************************************/
    2 /*  Color mangement on X11: Try to get around the limitations.      */
    3 /************************************************************************/
    4 
    5 #   include "appFrameConfig.h"
    6 
    7 #   include <stddef.h>
    8 #   include <stdio.h>
    9 #   include <stdlib.h>
   10 
   11 #   include <appDebugon.h>
   12 
   13 #   ifdef   USE_GTK
   14 
   15 #   include "drawUtilGtk.h"
   16 
   17 /************************************************************************/
   18 /*                                  */
   19 /*  Allocate approximate colors.                    */
   20 /*                                  */
   21 /************************************************************************/
   22 
   23 /***/
   24 
   25 static int appColorAllocSystemColor(    AllocatorColor *    acRes,
   26                     const AllocatorColor *  acScratch,
   27                     AppColors *     acSys )
   28     {
   29     APP_COLOR_RGB       xc;
   30 
   31     xc.red= acScratch->acRed;
   32     xc.green= acScratch->acGreen;
   33     xc.blue= acScratch->acBlue;
   34 
   35     if  ( ! gdk_color_alloc( acSys->acColormap, &xc ) )
   36     { LDEB(1); return -1;   }
   37 
   38     acRes->acAllocated= AC_ALLOCATED;
   39     acRes->acRed= xc.red;
   40     acRes->acGreen= xc.green;
   41     acRes->acBlue= xc.blue;
   42     acRes->acColorNumber= xc.pixel;
   43 
   44     return 0;
   45     }
   46 
   47 static int appColorRgb111(  AllocatorColor *    ac,
   48                 ColorAllocator *    ca,
   49                 unsigned int        r,
   50                 unsigned int        g,
   51                 unsigned int        b )
   52     {
   53     AllocatorColor  acScratch;
   54     AllocatorColor *    acRes;
   55 
   56     bmColorRgbDirect( &acScratch, ca, r, g, b );
   57 
   58     acRes= &(ca->ca222Colors[acScratch.acColorNumber]);
   59 
   60     if  ( acRes->acAllocated == AC_UNALLOCATED )
   61     {
   62     AppColors * acSys= (AppColors *)ca->caSystemPrivate;
   63 
   64     if  ( appColorAllocSystemColor( acRes, &acScratch, acSys ) )
   65         { LDEB(acScratch.acColorNumber); return -1; }
   66     }
   67 
   68     *ac= *acRes;
   69     ac->acAllocated= AC_COPIED;
   70 
   71     return 0;
   72     }
   73 
   74 static int appColorRgb222(  AllocatorColor *    ac,
   75                 ColorAllocator *    ca,
   76                 unsigned int        r,
   77                 unsigned int        g,
   78                 unsigned int        b )
   79     {
   80     AllocatorColor  acScratch;
   81     AllocatorColor *    acRes;
   82 
   83     bmColorRgbDirect( &acScratch, ca, r, g, b );
   84 
   85     acRes= &(ca->ca222Colors[acScratch.acColorNumber]);
   86 
   87     if  ( acRes->acAllocated == AC_UNALLOCATED )
   88     {
   89     AppColors * acSys= (AppColors *)ca->caSystemPrivate;
   90 
   91     if  ( appColorAllocSystemColor( acRes, &acScratch, acSys ) )
   92         { LDEB(acScratch.acColorNumber); return -1; }
   93     }
   94 
   95     *ac= *acRes;
   96     ac->acAllocated= AC_COPIED;
   97 
   98     return 0;
   99     }
  100 
  101 static int appColorRgb555(  AllocatorColor *    ac,
  102                 ColorAllocator *    ca,
  103                 unsigned int        r,
  104                 unsigned int        g,
  105                 unsigned int        b )
  106     {
  107     AllocatorColor  acScratch;
  108     AllocatorColor *    acRes;
  109 
  110     bmColorRgbDirect( &acScratch, ca, r, g, b );
  111 
  112     acRes= &(ca->caColors[acScratch.acColorNumber]);
  113 
  114     if  ( acRes->acAllocated == AC_UNALLOCATED )
  115     {
  116     AppColors * acSys= (AppColors *)ca->caSystemPrivate;
  117 
  118     if  ( appColorAllocSystemColor( acRes, &acScratch, acSys ) )
  119         {
  120         if  ( bmFindNearestColorRgb( ac, ca, r, g, b ) )
  121         { LDEB(1); return -1;   }
  122 
  123         return 0;
  124         }
  125     }
  126 
  127     *ac= *acRes;
  128     ac->acAllocated= AC_COPIED;
  129 
  130     return 0;
  131     }
  132 
  133 static int appColorRgb332(  AllocatorColor *    ac,
  134                 ColorAllocator *    ca,
  135                 unsigned int        r,
  136                 unsigned int        g,
  137                 unsigned int        b )
  138     {
  139     AllocatorColor  acScratch;
  140     AllocatorColor *    acRes;
  141 
  142     bmColorRgbDirect( &acScratch, ca, r, g, b );
  143 
  144     acRes= &(ca->caColors[acScratch.acColorNumber]);
  145 
  146     if  ( acRes->acAllocated == AC_UNALLOCATED )
  147     {
  148     AppColors * acSys= (AppColors *)ca->caSystemPrivate;
  149 
  150     if  ( appColorAllocSystemColor( acRes, &acScratch, acSys ) )
  151         {
  152         if  ( bmFindNearestColorRgb( ac, ca, r, g, b ) )
  153         { LDEB(1); return -1;   }
  154 
  155         return 0;
  156         }
  157     }
  158 
  159     *ac= *acRes;
  160     ac->acAllocated= AC_COPIED;
  161 
  162     return 0;
  163     }
  164 
  165 int appColorRgb(    APP_COLOR_RGB *     xc,
  166             AppColors *     acSys,
  167             unsigned int        r,
  168             unsigned int        g,
  169             unsigned int        b )
  170     {
  171     AllocatorColor  ac;
  172     ColorAllocator *    ca= &(acSys->acAllocator);
  173 
  174     if  ( acSys->acVisualClass == GDK_VISUAL_TRUE_COLOR     ||
  175       acSys->acVisualClass == GDK_VISUAL_DIRECT_COLOR   )
  176     {
  177     bmColorRgbDirect( &ac, ca, r, g, b );
  178     }
  179     else{
  180     if  ( ! ca->caSystemAllocator )
  181         { XDEB(ca->caSystemAllocator); return -1;   }
  182 
  183     if  ( (*ca->caSystemAllocator)( &ac, ca, r, g, b ) )
  184         { LLLDEB(r,g,b); return -1; }
  185     }
  186 
  187     xc->red= ac.acRed;
  188     xc->green= ac.acGreen;
  189     xc->blue= ac.acBlue;
  190     xc->pixel= ac.acColorNumber;
  191 
  192     return 0;
  193     }
  194 
  195 int appColorNamed(  APP_COLOR_RGB *     xc,
  196             AppColors *     acSys,
  197             const char *        name )
  198     {
  199     APP_COLOR_RGB   exact;
  200 
  201     if  ( ! gdk_color_parse( name, &exact ) )
  202     { SDEB(name); return -1;    }
  203 
  204     return appColorRgb( xc, acSys,
  205             exact.red/257, exact.green/257, exact.blue/257 );
  206     }
  207 
  208 static int appAllocateColors(   AppColors *     acSys )
  209     {
  210     GdkVisual *     vis= gdk_visual_get_system();
  211     GdkColormap *   cmap= gdk_colormap_get_system();
  212 
  213     int         count;
  214 
  215     ColorAllocator *    ca= &(acSys->acAllocator);
  216 
  217     acSys->acColormap= cmap;
  218     acSys->acVisualClass= vis->type;
  219 
  220     ca->caSystemPrivate= acSys;
  221     ca->caDepth= vis->depth;
  222 
  223     switch( acSys->acVisualClass )
  224     {
  225     case GDK_VISUAL_STATIC_GRAY:
  226     case GDK_VISUAL_GRAYSCALE:
  227         ca->caAllocationType= CA_INDEX_ALLOC;
  228         switch( ca->caDepth )
  229         {
  230         case 1: case 2: case 4: case 8:
  231             break;
  232         default:
  233             LLDEB(acSys->acVisualClass,ca->caDepth);
  234         }
  235         break;
  236 
  237     case GDK_VISUAL_STATIC_COLOR:
  238         ca->caAllocationType= CA_INDEX_ALLOC;
  239         break;
  240 
  241     case GDK_VISUAL_PSEUDO_COLOR:
  242         ca->caAllocationType= CA_INDEX_ALLOC;
  243         break;
  244 
  245     case GDK_VISUAL_TRUE_COLOR:
  246     case GDK_VISUAL_DIRECT_COLOR:
  247         ca->caAllocationType= CA_CALCULATED;
  248 
  249         bmSetCalculatedShifts( ca, vis->red_mask,
  250                         vis->green_mask, vis->blue_mask );
  251 
  252         return 0;
  253     }
  254 
  255     switch( ca->caDepth )
  256     {
  257     case 1:
  258         count= 2;
  259         ca->caSystemAllocator= appColorRgb111;
  260         bmSetCalculatedShifts( ca, 0x0004, 0x0002, 0x0001 );
  261         break;
  262 
  263     case 2:
  264         count= 4;
  265         ca->caSystemAllocator= appColorRgb111;
  266         bmSetCalculatedShifts( ca, 0x0004, 0x0002, 0x0001 );
  267         break;
  268 
  269     case 4:
  270         count= 16;
  271         ca->caSystemAllocator= appColorRgb222;
  272         bmSetCalculatedShifts( ca, 0x0030, 0x000c, 0x0003 );
  273         break;
  274 
  275     case 8:
  276         count= 256;
  277         ca->caSystemAllocator= appColorRgb332;
  278         bmSetCalculatedShifts( ca, 0x00e0, 0x001c, 0x0003 );
  279         break;
  280 
  281     case 16:
  282         count= 256* 256;
  283         ca->caSystemAllocator= appColorRgb555;
  284         bmSetCalculatedShifts( ca, 0x7c00, 0x03e0, 0x001f );
  285         break;
  286 
  287     case 24:
  288     case 32:
  289         return 0;
  290 
  291     default:
  292         LDEB(vis->depth); return -1;
  293     }
  294 
  295     if  ( bmAllocateAllocatorColors( ca, count ) )
  296     { LDEB(count); return -1;   }
  297 
  298     return 0;
  299     }
  300 
  301 # if 0
  302 void appCleanColors(    AppColors *     acSys )
  303     {
  304     int         i;
  305 
  306     GdkColormap *   cmap= acSys->acColormap;
  307     ColorAllocator *    ca= &(acSys->acAllocator);
  308 
  309     for ( i= 0; i < 64; i++ )
  310     {
  311     if  ( ca->ca222Colors[i].acAllocated == AC_ALLOCATED )
  312         {
  313         gulong  pix= ca->ca222Colors[i].acColorNumber;
  314 
  315         gdk_colors_free( cmap, &pix, 1, 0L );
  316         ca->ca222Colors[i].acAllocated= AC_UNALLOCATED;
  317         }
  318     }
  319 
  320     for ( i= 0; i < ca->caColorCount; i++ )
  321     {
  322     if  ( ca->caColors[i].acAllocated == AC_ALLOCATED )
  323         {
  324         gulong  pix= ca->caColors[i].acColorNumber;
  325 
  326         gdk_colors_free( cmap, &pix, 1, 0L );
  327         ca->caColors[i].acAllocated= AC_UNALLOCATED;
  328         }
  329     }
  330 
  331     bmCleanColorAllocator( ca );
  332     }
  333 # endif
  334 
  335 void appInitColors( AppColors * acSys )
  336     {
  337     ColorAllocator *    ca= &(acSys->acAllocator);
  338 
  339     bmInitColorAllocator( ca );
  340 
  341     acSys->acColormap= (GdkColormap *)0;
  342     }
  343 
  344 int appColorFindRgb(    APP_COLOR_RGB * xc,
  345             AppColors * acSys,
  346             unsigned int    r,
  347             unsigned int    g,
  348             unsigned int    b )
  349     {
  350     ColorAllocator *    ca= &(acSys->acAllocator);
  351     AllocatorColor  acRet;
  352 
  353     if  ( bmFindNearestColorRgb( &acRet, ca, r, g, b ) )
  354     { return -1;    }
  355 
  356     xc->red= acRet.acRed;
  357     xc->green= acRet.acGreen;
  358     xc->blue= acRet.acBlue;
  359     xc->pixel= acRet.acColorNumber;
  360 
  361     return 0;
  362     }
  363 
  364 static AppColors * APP_TheColors= (AppColors *)0;
  365 
  366 AppColors * guiGetColorsGtk( void )
  367     {
  368     if  ( ! APP_TheColors )
  369     {
  370     APP_TheColors= malloc( sizeof(AppColors) );
  371     if  ( ! APP_TheColors )
  372         { XDEB(APP_TheColors); return (AppColors *)0;   }
  373 
  374     appInitColors( APP_TheColors );
  375     appAllocateColors( APP_TheColors );
  376     }
  377 
  378     return APP_TheColors;
  379     }
  380 
  381 #   endif