Reputation: 1561
How would I edit the method ClosestColor
to accept the parameter List<DuluxColours>
instead of List<Color>
?
I need the method ClosestColor
to return the color name from my List<DuluxColours>
Is this even possible? My Linq proficiency is somewhat lacking :)
Thanks for any help!
public int ClosestColor(List<Color> colours, Color target)
{
var colorDiffs = colours.Select(n => ColorDiff(n, target)).Min(n => n);
return colours.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));
}
public class DuluxColour
{
public string ColourName { get; set; }
public Color Colour { get; set; }
}
Upvotes: 2
Views: 414
Reputation: 120
you can still do,
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
List<DuluxColour> _colors= new List<DuluxColour>();
_colors.Add(new DuluxColour{Colour = Color.Black , ColourName ="Black"});
_colors.Add(new DuluxColour{Colour = Color.Red , ColourName ="Red"});
_colors.Add(new DuluxColour{Colour = Color.Green , ColourName ="Green"});
_colors.Add(new DuluxColour{Colour = Color.Yellow , ColourName ="Yellow"});
var col =ClosestColor(_colors, Color.Purple);
Console.Write(col);
}
public static string ClosestColor(List<DuluxColour> colours, Color target)
{
var colorDiffs = colours.Select(n => ColorDiff(n.Colour, target)).Min(n => n);
return colours.FirstOrDefault(n => ColorDiff(n.Colour, target) == colorDiffs).ColourName;
}
public static 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)
Upvotes: -1
Reputation: 120
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
List<DuluxColour> _colors= new List<DuluxColour>();
_colors.Add(new DuluxColour{Colour = Color.Black , ColourName ="Black"});
_colors.Add(new DuluxColour{Colour = Color.Red , ColourName ="Red"});
_colors.Add(new DuluxColour{Colour = Color.Green , ColourName ="Green"});
_colors.Add(new DuluxColour{Colour = Color.Yellow , ColourName ="Yellow"});
var col =ClosestColor(_colors, Color.Purple);
Console.Write(col.ColourName);
}
public static DuluxColour ClosestColor(List<DuluxColour> colours, Color target)
{
var colorDiffs = colours.Select(n => ColorDiff(n.Colour, target)).Min(n => n);
return colours.Find(n => ColorDiff(n.Colour, target) == colorDiffs);
}
public static 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));
}
}
public class DuluxColour
{
public string ColourName { get; set; }
public Color Colour { get; set; }
}
Upvotes: -1
Reputation: 5355
Try this:
public DuluxColour ClosestColor(List<DuluxColour> colours, Color target)
{
var colorDiffs = colours.Select(n => ColorDiff(n.Colour, target)).Min(n => n);
return colours.Find(n => ColorDiff(n.Colour, target) == colorDiffs);
}
Note that I chose to return DuluxColour
instead of just the name so callers have the option to get the color value. In your case you will need to call:
string closestColorName = ClosestColor(colours, target).ColourName;
Take note that you have the handle the possibility of ClosestColor
returning null.
Without using an explicit List
, you can also use:
public DuluxColour ClosestColor(IEnumerable<DuluxColour> colours, Color target)
{
var colorDiffs = colours.Select(n => ColorDiff(n.Colour, target)).Min(n => n);
return colours.FirstOrDefault(n => ColorDiff(n.Colour, target) == colorDiffs);
}
Upvotes: 4
Reputation: 441
You May Try this,
public int ClosestColor(List<DuluxColour> colours, Color target)
{
var colorDiffs = colours.Select(n => ColorDiff(n.Colour, target)).Min(n => n);
return colours.FindIndex(n => ColorDiff(n.Colour, target) == colorDiffs);
}
I hope this may work for you
Upvotes: -1