Advertisement
Ham62

Untitled

Jan 28th, 2016
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1.         private int NearestColor(Color Pixel)
  2.         {
  3.             int diffR; int diffB; int diffG;
  4.             int smallestDiffValue = 0x7FFFFFFF;
  5.             int SmallestDiff = 0x00;
  6.             int diff = 0x00;
  7.            
  8.             for (int C = 0; C < 256; C++)
  9.             {
  10.                 diffR = Pixel.R - Palette[C].R;
  11.                 diffB = Pixel.B - Palette[C].B;
  12.                 diffG = Pixel.G - Palette[C].G;
  13.                 diff = diffR*diffR + diffB*diffB + diffG*diffG;
  14.                 if (diff == 0) { return C; }         // Quit early if match found
  15.                 if (diff < smallestDiffValue)        // Otherwise compare to smallest color found
  16.                 { SmallestDiff = C; smallestDiffValue = diff; }
  17.             }
  18.             return SmallestDiff;
  19.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement