home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_02 / 1002037a < prev    next >
Text File  |  1991-12-06  |  1KB  |  59 lines

  1. /* Listing 2 */
  2.  
  3. /*begin************************************************
  4. *   Program   : sort_color
  5. *   Descript. : Sorts the palette by the red color only
  6. *               Also builds the index array
  7. *               for fast lookup.
  8. *end**************************************************/
  9.  
  10. void sort_color()
  11. {
  12. int i;
  13. int j;
  14.  
  15. /*  Make a copy of the original palette */
  16. for ( i=0; i<PAL_LEN; i++ )
  17.     {
  18.     pals[ i ].num = i;
  19.     pals[ i ].red = palo[ i ].red;
  20.     pals[ i ].grn = palo[ i ].grn;
  21.     pals[ i ].blu = palo[ i ].blu;
  22.     }
  23.  
  24. /*  Sort the copy of the palette */
  25. qsort( (void *)pals, PAL_LEN, sizeof(pal), color_comp);
  26.  
  27. /* build the quick index    */
  28. /* so we don't have to do a */
  29. /* binary search every time */
  30.  
  31. for ( i=0, j=0; i<PAL_LEN; i++ )
  32.     {
  33.     while( pals[ j ].red < i )
  34.         if ( ++j >= PAL_LEN )
  35.             {
  36.             j = PAL_LEN - 1;
  37.             break;
  38.             }
  39.     redindex[ i ] = j;
  40.     }
  41. }
  42.  
  43.  
  44.  
  45. /*begin************************************************
  46. *   Program   : color_comp
  47. *   Descript. : Compares two colors (red) in a palette.
  48. *end**************************************************/
  49.  
  50. int color_comp( const void *a, const void *b )
  51. {
  52. pal *aa;
  53. pal *bb;
  54.  
  55. aa = (pal *)a;
  56. bb = (pal *)b;
  57. return( aa->red - bb->red );
  58. }
  59.