I am taking an image and checking if there are any shades of red in it. At least one pixel in the image of shade red should return true. In my solution not all shades of red are covered. I use Bitmap to check for each pixel in the image. This is the code that I have tried:
public bool HasRedColor(string imagePath)
{
if(imagePath != null)
{
var image = Image.FromFile(imagePath);
var redColors = TakeRed();
Bitmap bmp = new Bitmap(imagePath, true);
for (int i = 0; i < image.Height; i++)
{
for (int j = 0; j < image.Width; j++)
{
//Get the color at each pixel
Color pixelColor = bmp.GetPixel(j, i);
//Compare Pixel's Color ARGB property with the picked color's ARGB property
if (redColors.Any(x => x == pixelColor))
{
return true;
}
}
}
return false;
}
return false;
}
public List<Color> TakeRed()
{
var listOfRedColors = new List<Color>();
for (int i = 150; i < 255; i++)
{
listOfRedColors.Add(Color.FromArgb(255, i, 0, 0));
}
return listOfRedColors;
}
}
However, my function TakeRed() does not cover all shades of red. Any suggestions on how to get all the shades of red?
What you do, is creating a list of red colors, but it is incomplete. You are not considering the G and B values. If they are not 0, but, lets say, 1. This could be considered as being red too, with R values from 150 to 255. So you need to append your list, until you have all shades of red in it. I think it might be quite subjective, to set the ranges.
BUT! To put all colors you consider to be red in a list and iterate over the list every time, you want to check, if a color is red is quite inefficient. Set ranges instead. If, e.g., you say, every color with a R value greater than 150 and G and B values less than 20, a solution might look like this.
public bool isRed(Color pixelColor)
{
if (pixelColor.R > 0x96 && pixelColor.G < 0x14 && pixelColor.B < 0x14)
{
return true;
}
return false;
}
You are checking the R, G and B values seperately if they are in range. R, G and B are of type byte. You can use hexadecimal values to set your ranges.