QuestionMike
QuestionMike

Reputation: 21

Fastest method to search an array for matching color in .NET

I am using GetPixel from a bitmap (using LockBits so no efficiency issue there) and need to search an array of colors for the location of the color within the array as fast as possible. I have very large bitmaps to iterate through.

...                
prevColor = Color.FromArgb(255, r, g, b);

int count = 1536;
for (int i = 0; i < count; i++)
{
   if (prevColor == theColorScale[i])
   {
       loc = i;
       break;
   }
}

Any suggestions?

Upvotes: 1

Views: 165

Answers (2)

paparazzo
paparazzo

Reputation: 45096

Accepting the question as stated

A Dictionary has O(1) lookup

Dictionary<color, Int32> colors = new Dictionary<color, Int32>();
...
loc = colors[prevColor];

The key (color) in the Dictionary must be unique

Upvotes: 1

Dakotah Hicock
Dakotah Hicock

Reputation: 390

Could you use a List and search it using .IndexOf()?

...
List<Color> colorScale;
/* add colors */
loc = colorScale.IndexOf(prevColor);

Upvotes: 0

Related Questions