Create a dictionary entry containing List <int>
So, I'm trying to create a dictionary of colors in the form
Dictionary<string,List<int>> So:
(colour:colourvals) for example, if the color was red:
("red":(255,0,0)) I am very new to C # (about 1 week), but I have quite a lot of python experience. What I'm trying to achieve in python will look like this:
col_dict = {"red":(255,0,0), "blue":(255,0,0), "green":(255,0,0)} Moving to C #, after a lot of messing around, I finally managed to do something that works. This is the current code I have (which seems messy):
colDict = new Dictionary<string, List<int>>(); colDict.Add("red", new List<int> { 200, 40, 40 }.ToList()); colDict.Add("green", new List<int> { 40, 200, 40 }.ToList()); colDict.Add("blue", new List<int> { 40, 40, 200 }.ToList()); Firstly, is there a better way to do this?
Secondly, then I want to use the list values โโas parameters for Color.FromArgb (). Is there some way that I put the List from colDict in parameters like:
Color.FromArgb(colDict["green"]); or do I need to save the color selection and then put each value that way?
this.col = colDict[colour]; Color.FromArgb(this.col[0],this.col[1],this.col[2])); Thanks to everyone who can help me! :)
I finally figured it out, but I could not have done it without any help! First create a dictionary with string keys and color values:
colDict = new Dictionary<string, Color>(); Then the colors can be added like this:
colDict.Add("red", Color.FromArgb(200, 40, 40)); colDict.Add("green", Color.FromArgb(40, 200, 40)); colDict.Add("blue", Color.FromArgb(40, 40, 200)); And then colors can be referenced, and they can easily be obtained as a โcolorโ data type, for example:
colDict["red"]; I think you need Dictionary<string, Vector3D> . It's easy to create a Vector3D class / struct yourself, as I don't think it comes with the .NET platform:
public class Vector3D { public int R{get;set;} public int G{get;set;} public int B{get;set;} } dict["red"] = new Vector3D{R=255,G=0,B=0} ; Depending on your use, it may be easier for you to keep the RGB color in a single int value.
public int ColorToInt(byte r, byte g, byte b) { return (b << 16) | (g << 8) | r; } You can use Color.FromArgb (Int32) to get the color from it. Therefore, your dictionary should only store <Color, int> (or you can replace Color with a string as a key)
To save time by calling the ColorToInt method each time, you can create an extenstion method.
public static void AddRGB(this Dictionary<Color, int> dict, Color col) { int rgbint = (col.B << 16) | (col.G << 8) | col.R; dict.Add(col, rgbint); } and so whenever you want to add an item to the dictionary, you can do this
Dictionary<Color, int> colDict = new Dictionary<Color, int>(); colDict.AddRGB(Color.Green); It will automatically calculate the int Color.Green and add it as the value for you.
You do not need a ToList list, since it already contains a list:
var colDict = new Dictionary<string, List<int>>(); colDict.Add("red", new List<int> { 200, 40, 40 }); colDict.Add("green", new List<int> { 40, 200, 40 }); colDict.Add("blue", new List<int> { 40, 40, 200 }); Access to such values:
colDict["green"][0] colDict["green"][1] colDict["green"][2] Color.FromArgb(colDict["green"][0],colDict["green"][1],colDict["green"][2])); Another alternative to creating values โโis to use Tuple
var colDict = new Dictionary<string, Tuple<int,int,int>>(); colDict.Add("red", new Tuple<int,int,int>(40,45,49)); colDict["red"].Item1 colDict["red"].Item2 colDict["red"].Item3