1 module caLib.palettes.ColorListPalette;
2 
3 import caLib_util.structs : Color;
4 
5 
6 
7 auto create_ColorListPalette(Ct)(Color[Ct] colors)
8 in
9 {
10 	assert(colors !is null);
11 }
12 body
13 {
14 	return create_ColorListPalette!(Ct)(colors, Color.init);
15 }
16 
17 
18 
19 auto create_ColorListPalette(Ct)(Color[Ct] colors, Color defaultValue)
20 in
21 {
22 	assert(colors !is null);
23 }
24 body
25 {
26 	return new ColorListPalette!(Ct)(colors, defaultValue);
27 }
28 
29 
30 
31 struct ColorListPalette(Ct)
32 {
33 
34 private:
35 
36 	Color[Ct] colors;
37 	Color defaultValue;
38 
39 public:
40 
41 	alias CellStateType = Ct;
42 	alias DisplayValueType = Color;
43 
44 	this(Color[Ct] colors, Color defaultValue)
45 	in
46 	{
47 		assert(colors !is null);
48 	}
49 	body
50 	{
51 		this.colors = colors;
52 		this.defaultValue = defaultValue;
53 	}
54 
55 	Color getDisplayValue(string behaviour)(Ct cellState)
56 	{
57 		static if(behaviour == "withDefaultValue")
58 		{
59 			return colors.get(cellState, defaultValue);
60 			//auto ptr = cellState in colors;
61 			//return ptr ? *ptr : defaultValue;
62 		}
63 		else static if(behaviour == "assumeEntryExists")
64 		{
65 			return colors[cellState];
66 		}
67 		else static if(behaviour == "_test")
68 		{
69 			return Color.init;
70 		}
71 		else
72 		{
73 			static assert(0, "ColorListPalette getDisplayValue method dosen't"
74 				~ "have a \"" ~ behaviour ~"\" behaviour");
75 		}
76 	}
77 
78 	Color getDisplayValue()(Ct cellState)
79 	{
80 		return getDisplayValue!"withDefaultValue"(cellState);
81 	}
82 }
83 
84 version(unittest)
85 {
86 	import caLib_abstract.palette : isColorPalette;
87 }
88 
89 unittest
90 {
91 	static assert( isColorPalette!(ColorListPalette!(int), int));
92 }