Guest User

Fossil → src/timeline.c → bg_to_fg()

a guest
Sep 8th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. /*
  2. ** Change the RGB background color given in the argument in a foreground
  3. ** color with the same hue.
  4. */
  5. /*
  6.   Modifications:
  7.  
  8.   * check if color values contain non-hex chars, and return default value if so
  9.   * allow color values in shorthand hex triplet form #HHH
  10.   * ensure each color component is between 0 and 255 to avoid #HHHHHHHH output
  11.   * return default color values #000000 (for white background) or #FFFFFF (for
  12.     black background) for all non-hex triplet color values (!)
  13.   * moved whiteFg initialization up so it is ready when jumping to default
  14.  
  15.   I tried to follow the Fossil coding style guidelines, and I have some open
  16.   questions:
  17.  
  18.   * is strchr() allowed for hex-digit testing?
  19.     if( strchr("0123456789abcdefABCDEF",zIn[i])==0 ) ...
  20.   * which is the preferred storage format for the default color values, string
  21.     or sqlite3_snprintf() with constant numeric data?
  22. */
  23. static const char *bg_to_fg(const char *zIn){
  24.   int nIn; /* length of zIn */
  25.   int i;
  26.   unsigned int x[3];
  27.   unsigned int mx = 0;
  28.   static int whiteFg = -1;
  29.   static char zRes[10];
  30.   /* Initialization moved here so it is ready when jumping to default_fg */
  31.   if( whiteFg<0 ) whiteFg = skin_detail_boolean("white-foreground");
  32.   nIn = strlen( zIn );
  33.   if(( nIn!=7 && nIn!=4 ) || zIn[0]!='#' )
  34.     goto default_fg;
  35.   /* Testing for non-hex chars, is strchr() allowed? */
  36.   for (i=1; i<nIn; i++){
  37.     if(( zIn[i]<'0' || zIn[i]>'9' ) &&
  38.        ( zIn[i]<'a' || zIn[i]>'f' ) &&
  39.        ( zIn[i]<'A' || zIn[i]>'F' ))
  40.       goto default_fg;
  41.   }
  42.   zIn++;
  43.   if ( nIn==4 ){
  44.     for(i=0; i<3; i++){
  45.       x[i] = hex_digit_value(zIn[0])*17;
  46.       zIn++;
  47.       if( x[i]>mx ) mx = x[i];
  48.     }
  49.   }else{
  50.     for(i=0; i<3; i++){
  51.       x[i] = hex_digit_value(zIn[0])*16 + hex_digit_value(zIn[1]);
  52.       zIn += 2;
  53.       if( x[i]>mx ) mx = x[i];
  54.     }
  55.   }
  56.   if( whiteFg ){
  57.     /* Make the color lighter */
  58.     static const unsigned int t = 215;
  59.     if( mx<t ) for(i=0; i<3; i++) x[i] += t - mx;
  60.   }else{
  61.     /* Make the color darker */
  62.     static const unsigned int t = 128;
  63.     if( mx>t ) for(i=0; i<3; i++) x[i] -= mx - t;
  64.   }
  65.   /* Make sure each color component is between 0 and 255 */
  66.   for (i=0; i<3; i++)
  67.     x[i] = x[i]<0 ? 0 : x[i]>255 ? 255 : x[i];
  68.   sqlite3_snprintf(sizeof(zRes),zRes,"#%02x%02x%02x",x[0],x[1],x[2]);
  69.   return zRes;
  70. default_fg:
  71.   /* By default, set to black for white background, and vice versa */
  72.   if( whiteFg )
  73.     memcpy(zRes,"#FFFFFF",8);
  74.     /* sqlite3_snprintf(sizeof(zRes),zRes,"#%02x%02x%02x",255,255,255); */
  75.   else
  76.     memcpy(zRes,"#000000",8);
  77.     /* sqlite3_snprintf(sizeof(zRes),zRes,"#%02x%02x%02x",0,0,0); */
  78.   return zRes;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment