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

  1. /* Listing 3 */
  2.  
  3. /*begin************************************************
  4. *   Program   : closest
  5. *   Descript. : Returns the number of the closest color
  6. *               from the palette, given the desired red
  7. *               green, and blue. Find the closest point
  8. *               by using the red index arrey. Look at
  9. *               points on both sides of the closest
  10. *               point. While searching, if a point is
  11. *               closer then it is the shortest distance
  12. *               Once you are examining points that are
  13. *               farther away in just the one dimension
  14. *               (red) then you are done.
  15. *end**************************************************/
  16.  
  17. int closest( int red, int green, int blue )
  18. {
  19. long least;
  20. long dif;
  21. long sum;
  22. int index;
  23. int left;
  24. int right;
  25.  
  26. index = redindex[ red ];
  27. least = dist( red, green, blue, index );
  28. left = index;
  29. right = index;
  30.  
  31. while( ( left >= 0 ) || ( right < PAL_LEN ) )
  32.     {
  33.     if ( --left >= 0 )
  34.         {
  35.         /* if red dist. alone is greater, then quit */
  36.         dif = red - pals[ left ].red;
  37.         if ( ( dif * dif ) > least )
  38.             left = -1;
  39.         else
  40.             {
  41.             sum = dist( red, green, blue, left );
  42.             if ( sum < least )
  43.                 {
  44.                 least = sum;
  45.                 index = left;
  46.                 }
  47.             }
  48.         }
  49.     if ( ++right < PAL_LEN )
  50.         {
  51.         /* if red dist. alone is greater, then quit */
  52.         dif = red - pals[ right ].red;
  53.         if ( ( dif * dif ) > least )
  54.             right = PAL_LEN;
  55.         else
  56.             {
  57.             sum = dist( red, green, blue, right );
  58.             if ( sum < least )
  59.                 {
  60.                 least = sum;
  61.                 index = right;
  62.                 }
  63.             }
  64.         }
  65.     }
  66. return( pals[ index ].num );
  67. }
  68.  
  69.  
  70.  
  71. /*begin************************************************
  72. *   Program   : dist
  73. *   Descript. : Color distance (squared)
  74. *end**************************************************/
  75.  
  76. long dist( int red, int green, int blue, int num )
  77. {
  78. long dif;
  79. long sum;
  80. pal *palt;
  81.  
  82. palt = &pals[ num ];
  83. dif = red - palt->red;
  84. sum = dif * dif;
  85. dif = green - palt->grn;
  86. sum += dif * dif;
  87. dif = blue - palt->blu;
  88. sum += dif * dif;
  89. return( sum );
  90. }
  91.