WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
RGBAToy.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using DirectOutput.Cab.Out;
8 
9 namespace DirectOutput.Cab.Toys.Layer
10 {
20  {
21 
22  #region Layers
23  [System.Xml.Serialization.XmlIgnore]
30  public LayerDictionary<RGBAColor> Layers { get; private set; }
31 
32 
33 
39  {
40  if (Layers.Count > 0)
41  {
42  float Red = 0;
43  float Green = 0;
44  float Blue = 0;
45  foreach (KeyValuePair<int, RGBAColor> KV in Layers)
46  {
47  int Alpha = KV.Value.Alpha;
48  if (Alpha != 0)
49  {
50  int NegAlpha = 255 - Alpha;
51  Red = AlphaMappingTable.AlphaMapping[NegAlpha, (int)Red] + AlphaMappingTable.AlphaMapping[Alpha, KV.Value.Red];
52  Green = AlphaMappingTable.AlphaMapping[NegAlpha, (int)Green] + AlphaMappingTable.AlphaMapping[Alpha, KV.Value.Green];
53  Blue = AlphaMappingTable.AlphaMapping[NegAlpha, (int)Blue] + AlphaMappingTable.AlphaMapping[Alpha, KV.Value.Blue];
54  }
55  }
56 
57  return new RGBColor((int)Red, (int)Green, (int)Blue);
58  }
59  else
60  {
61  return new RGBColor(0, 0, 0);
62  }
63  }
64 
65 
66 
67  #endregion
68 
69 
70 
71  #region Outputs
72  private IOutput _OutputRed;
73 
77  public string OutputNameRed { get; set; }
78 
79  private IOutput _OutputGreen;
80 
84  public string OutputNameGreen { get; set; }
85 
86  private IOutput _OutputBlue;
87 
91  public string OutputNameBlue { get; set; }
92 
93  #endregion
94 
95 
96  #region Fading curve
97  private string _FadingCurveName = "Linear";
98  private Curve FadingCurve = null;
99 
107  public string FadingCurveName
108  {
109  get { return _FadingCurveName; }
110  set { _FadingCurveName = value; }
111  }
112 
113  private void InitFadingCurve(Cabinet Cabinet)
114  {
115  if (Cabinet.Curves.Contains(FadingCurveName))
116  {
117  FadingCurve = Cabinet.Curves[FadingCurveName];
118  }
119  else if (!FadingCurveName.IsNullOrWhiteSpace())
120  {
121  if (Enum.GetNames(typeof(Curve.CurveTypeEnum)).Contains(FadingCurveName))
122  {
124  Enum.TryParse(FadingCurveName, out T);
125  FadingCurve = new Curve(T);
126  }
127  else
128  {
129  FadingCurve = new Curve(Curve.CurveTypeEnum.Linear) { Name = FadingCurveName };
130  Cabinet.Curves.Add(FadingCurveName);
131  }
132  }
133  else
134  {
135  FadingCurve = new Curve(Curve.CurveTypeEnum.Linear);
136  }
137 
138  }
139  #endregion
140 
141 
142 
143  private Cabinet _Cabinet;
144  #region Init
145  public override void Init(Cabinet Cabinet)
150  {
151  _Cabinet = Cabinet;
152  InitFadingCurve(Cabinet);
153  InitOutputs(Cabinet);
154  }
155 
156  private void InitOutputs(Cabinet Cabinet)
157  {
158  if (Cabinet.Outputs.Contains(OutputNameRed))
159  {
160  _OutputRed = Cabinet.Outputs[OutputNameRed];
161  }
162  else
163  {
164  _OutputRed = null;
165  }
166  if (Cabinet.Outputs.Contains(OutputNameGreen))
167  {
168  _OutputGreen = Cabinet.Outputs[OutputNameGreen];
169  }
170  else
171  {
172  _OutputGreen = null;
173  }
174  if (Cabinet.Outputs.Contains(OutputNameBlue))
175  {
176  _OutputBlue = Cabinet.Outputs[OutputNameBlue];
177  }
178  else
179  {
180  _OutputBlue = null;
181  }
182  }
183  #endregion
184 
185  #region Finish
186 
191  public override void Finish()
192  {
193  Reset();
194  _Cabinet = null;
195  _OutputRed = null;
196  _OutputGreen = null;
197  _OutputBlue = null;
198  }
199  #endregion
200 
201 
202 
203 
207  public override void UpdateOutputs()
208  {
209  RGBColor RGB = GetResultingData();
210 
211  if (_OutputRed != null)
212  {
213  _OutputRed.Value = FadingCurve.MapValue(RGB.Red);
214  }
215  if (_OutputGreen != null)
216  {
217  _OutputGreen.Value = FadingCurve.MapValue(RGB.Green);
218  }
219  if (_OutputBlue != null)
220  {
221  _OutputBlue.Value = FadingCurve.MapValue(RGB.Blue);
222  }
223  }
224 
225 
229  public override void Reset()
230  {
231  Layers.Clear();
232  if (_OutputRed != null)
233  {
234  _OutputRed.Value = 0;
235  }
236  if (_OutputGreen != null)
237  {
238  _OutputGreen.Value = 0;
239  }
240  if (_OutputBlue != null)
241  {
242  _OutputBlue.Value = 0;
243  }
244  }
245 
246 
250  public RGBAToy()
251  {
252  Layers = new LayerDictionary<RGBAColor>();
253  }
254 
255 
256 
257 
258 
259  }
260 }
The Cabinet object describes the parts of a pinball cabinet (toys, outputcontrollers, outputs and more).
Definition: Cabinet.cs:17
CurveList Curves
List of named curve objects used to set Curves for toys.
Definition: Cabinet.cs:137
override void UpdateOutputs()
Updates the outputs of the RGBAToy.
Definition: RGBAToy.cs:207
RGBAToy()
Initializes a new instance of the RGBAToy class.
Definition: RGBAToy.cs:250
Dictionary for RGBALayer objects.
Common interface for RGB toys supporting several layers of color with alpha value.
Definition: IRGBAToy.cs:9
bool Contains(string Name)
Checks if a INamedItem object with the specified name exists in the list.
Common interface for RGB toys supporting several layers of color with alpha value.
Definition: IRGBOutputToy.cs:9
byte Value
Value of the output.
Definition: IOutput.cs:18
int Red
Brightness for Red.
Definition: RGBColor.cs:29
int Green
Brightness for Green.
Definition: RGBColor.cs:42
byte MapValue(byte CurvePosition)
Returns the value from the specified curve position.
Definition: Curve.cs:97
override void Reset()
Clears all layers and sets all outputs to 0 (off).
Definition: RGBAToy.cs:229
This class stores information on RGB colors used for toys and effects (e.g. RGBLed).
Definition: RGBColor.cs:14
RGBColor GetResultingData()
Get the RGBColor resulting from the colors and alpha values in the layers.
Definition: RGBAToy.cs:38
The namespace DirectOutput.Cab contains all cabinet related classes like the Cabinet class itself...
Definition: Cab.cs:16
DirectOutput.Cab.Out is the namespace for all output controller related classes like different output...
Common interface for outputs of any output controller. The Output class implements this interface and...
Definition: IOutput.cs:10
int Blue
Brightness for Blue.
Definition: RGBColor.cs:54
Red-Green-Blue (usual color order)
override void Finish()
Finishes the RGBALed toy. Resets the the toy and releases all references.
Definition: RGBAToy.cs:191
Base class for toys which need update calls to update the state of their assigned outputs...
Thie RGBAToy controls RGB leds and other gadgets displaying RGB colors. The RGBAToy has multilayer ...
Definition: RGBAToy.cs:19
CurveTypeEnum
Enumeration of predefined curves.
Definition: Curve.cs:20
Represents a curve which can be used to map values (e.g. adjust a brighnetss value of a led to the br...
Definition: Curve.cs:15
The namespace DirectOutput.General contains classes for general use.