WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
RGBAColor.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 System.Xml.Serialization;
7 
8 namespace DirectOutput.General.Color
9 {
10 
14  public class RGBAColor
15  {
16 
17 
18 
19  private int _Red;
20 
25  [XmlIgnoreAttribute]
26  public int Red
27  {
28  get { return _Red; }
29  set { _Red = value.Limit(0, 255); }
30  }
31 
32  private int _Green;
33 
38  [XmlIgnoreAttribute]
39  public int Green
40  {
41  get { return _Green; }
42  set { _Green = value.Limit(0, 255); }
43  }
44 
45  private int _Blue;
50  [XmlIgnoreAttribute]
51  public int Blue
52  {
53  get { return _Blue; }
54  set { _Blue = value.Limit(0, 255); }
55  }
56 
57  private int _Alpha;
62  [XmlIgnoreAttribute]
63  public int Alpha
64  {
65  get { return _Alpha; }
66  set { _Alpha = value.Limit(0, 255); }
67  }
68 
69 
74  public RGBAColor Clone()
75  {
76  return new RGBAColor(HexColor);
77  }
78 
83  public string HexColor
84  {
85  get
86  {
87  return "#{0:X2}{1:X2}{2:X2}{3:X2}".Build(new object[] { Red, Green, Blue, Alpha });
88  }
89  set
90  {
91  SetColor(value);
92  }
93  }
94 
100  {
101  return new RGBAColor(Red, Green, Blue, Alpha);
102  }
103 
104 
105 
106 
115  public bool SetColor(int Red, int Green, int Blue, int Alpha)
116  {
117  this.Red = Red;
118  this.Blue = Blue;
119  this.Green = Green;
120  this.Alpha = Alpha;
121  return true;
122  }
123 
132  public bool SetColor(int Red, int Green, int Blue)
133  {
134  this.Red = Red;
135  this.Blue = Blue;
136  this.Green = Green;
137  this.Alpha = (Red + Green + Blue > 0 ? 255 : 0);
138  return true;
139  }
140 
147  public bool SetColor(RGBColor Color)
148  {
149  SetColor(Color.Red, Color.Green, Color.Blue);
150  return true;
151  }
152 
153 
167  public bool SetColor(string Color)
168  {
169  if ((Color.Length == 8 && Color.IsHexString()) || (Color.Length == 9 && Color.StartsWith("#") && Color.IsHexString(1)))
170  {
171  int Offset;
172  if (Color.StartsWith("#"))
173  {
174  Offset = 1;
175  }
176  else
177  {
178  Offset = 0;
179  }
180  SetColor(Color.Substring(0 + Offset, 2).HexToInt(), Color.Substring(2 + Offset, 2).HexToInt(), Color.Substring(4 + Offset, 2).HexToInt(), Color.Substring(6 + Offset, 2).HexToInt());
181  return true;
182  };
183 
184  if ((Color.Length == 6 && Color.IsHexString()) || (Color.Length == 7 && Color.StartsWith("#") && Color.IsHexString(1)))
185  {
186  int Offset;
187  if (Color.StartsWith("#"))
188  {
189  Offset = 1;
190  }
191  else
192  {
193  Offset = 0;
194  }
195  SetColor(Color.Substring(0 + Offset, 2).HexToInt(), Color.Substring(2 + Offset, 2).HexToInt(), Color.Substring(4 + Offset, 2).HexToInt());
196  return true;
197  };
198 
199  string[] SplitColors = Color.Split(',');
200  if (SplitColors.Length == 3)
201  {
202  bool ColorsOK = true;
203  foreach (string C in SplitColors)
204  {
205  if (C.IsInteger())
206  {
207  ColorsOK = false;
208  }
209  }
210  if (ColorsOK)
211  {
212  SetColor(int.Parse(SplitColors[0]), int.Parse(SplitColors[1]), int.Parse(SplitColors[2]));
213  return true;
214  }
215  }
216  else if (SplitColors.Length == 4)
217  {
218  bool ColorsOK = true;
219  foreach (string C in SplitColors)
220  {
221  if (C.IsInteger())
222  {
223  ColorsOK = false;
224  }
225  }
226  if (ColorsOK)
227  {
228  SetColor(int.Parse(SplitColors[0]), int.Parse(SplitColors[1]), int.Parse(SplitColors[2]), int.Parse(SplitColors[3]));
229  return true;
230  }
231 
232  }
233  return false;
234  }
235 
236  #region Contructor
237  public RGBAColor() { }
241 
242 
250  public RGBAColor(int BrightnessRed, int BrightnessGreen, int BrightnessBlue)
251  {
252  SetColor(BrightnessRed, BrightnessGreen, BrightnessBlue);
253  }
254 
262  public RGBAColor(int BrightnessRed, int BrightnessGreen, int BrightnessBlue, int Alpha)
263  {
264  SetColor(BrightnessRed, BrightnessGreen, BrightnessBlue, Alpha);
265  }
266 
267 
279  public RGBAColor(string Color)
280  {
281  SetColor(Color);
282  }
283 
284 
291  {
292  SetColor(RGBColor);
293  }
294 
295  #endregion
296 
297 
298 
299  new public string ToString()
300  {
301  return HexColor;
302  }
303  }
304 }
bool SetColor(int Red, int Green, int Blue)
Sets the RGB components of the Color. The Alpha value is set to 0 if all color components are set to...
Definition: RGBAColor.cs:132
RGBAColor(int BrightnessRed, int BrightnessGreen, int BrightnessBlue, int Alpha)
Initializes a new instance of the RGBAColor class.
Definition: RGBAColor.cs:262
This class stores information on colors used for toys and effects (e.g. RGBLed).
Definition: RGBAColor.cs:14
int Red
Brightness for Red.
Definition: RGBColor.cs:29
int Green
Brightness for Green.
Definition: RGBColor.cs:42
RGBAColor(int BrightnessRed, int BrightnessGreen, int BrightnessBlue)
Initializes a new instance of the RGBAColor class. If all color components are set to 0...
Definition: RGBAColor.cs:250
RGBAColor GetRGBAColor()
Gets a new RGBAColor instance with the same color values.
Definition: RGBAColor.cs:99
This class stores information on RGB colors used for toys and effects (e.g. RGBLed).
Definition: RGBColor.cs:14
bool SetColor(RGBColor Color)
Sets the RGB components of the RGBAColor. The Alpha value is set to 0 if all color components are se...
Definition: RGBAColor.cs:147
RGBAColor Clone()
Clones this instance.
Definition: RGBAColor.cs:74
int Blue
Brightness for Blue.
Definition: RGBColor.cs:54
bool SetColor(int Red, int Green, int Blue, int Alpha)
Sets the RGBA components of the Color.
Definition: RGBAColor.cs:115
bool SetColor(string Color)
Sets the RGBA components of the Color. The parameter string Color ist first parsed for hexadecimal ...
Definition: RGBAColor.cs:167
The namespace DirectOutput.General contains classes for general use.
RGBAColor(string Color)
Initializes a new instance of the RGBAColor class. The parameter string Color is first parsed for h...
Definition: RGBAColor.cs:279
RGBAColor(RGBColor RGBColor)
Initializes a new instance of the RGBAColor class. The Alpha value is set to 0 if all color component...
Definition: RGBAColor.cs:290