I have an image with colors who corresponds to values following a gradient.
My gradient is like that :
My image look like this :
I search a way to convert a pixel color in my image to find the correct value from this gradient (in C#). For example : a black pixel has a value of 320, a white pixel has a value of 16000, ...
I found this post but the answer is the reverse of what I'm looking for... Converting int value to a color in a gradient
Can anyone offer some help please?
UPDATE:
I find/make this code to reconize the closest color but I'm not sure about the precision to find values...
private void pictureBox1_Click(object sender, MouseEventArgs e)
{
var zone_data = img.GetPixel(e.Location.X, e.Location.Y);
var colors = new List<Color> { Color.Black, Color.SaddleBrown, Color.Purple, Color.Blue, Color.Cyan, Color.Green, Color.Yellow, Color.Orange, Color.Red, Color.Salmon, Color.LightGray, Color.White };
Console.WriteLine("Nearest color : " + colors[closestColor(colors, zone_data)].ToString());
}
public int closestColor(List<Color> colors, Color target)
{
var colorDiffs = colors.Select(n => ColorDiff(n, target)).Min(n => n);
return colors.FindIndex(n => ColorDiff(n, target) == colorDiffs);
}
public int ColorDiff(Color c1, Color c2)
{
return (int)Math.Sqrt((c1.R - c2.R) * (c1.R - c2.R)
+ (c1.G - c2.G) * (c1.G - c2.G)
+ (c1.B - c2.B) * (c1.B - c2.B));
}