|
9 | 9 |
|
10 | 10 | namespace MADE.App.Design.Color |
11 | 11 | { |
| 12 | + using System.Globalization; |
| 13 | + |
12 | 14 | /// <summary> |
13 | 15 | /// Defines a model for a UI element color. |
14 | 16 | /// </summary> |
@@ -37,6 +39,33 @@ internal Color(byte a, byte r, byte g, byte b) |
37 | 39 | this.B = b; |
38 | 40 | } |
39 | 41 |
|
| 42 | + /// <summary> |
| 43 | + /// Initializes a new instance of the <see cref="Color"/> class. |
| 44 | + /// </summary> |
| 45 | + /// <param name="hexValue"> |
| 46 | + /// The ARGB or RGB hex value represented as a string. |
| 47 | + /// </param> |
| 48 | + public Color(string hexValue) |
| 49 | + { |
| 50 | + string val = hexValue.ToUpper(); |
| 51 | + |
| 52 | + switch (val.Length) |
| 53 | + { |
| 54 | + case 7: |
| 55 | + this.A = 255; |
| 56 | + this.R = byte.Parse(val.Substring(1, 2), NumberStyles.AllowHexSpecifier); |
| 57 | + this.G = byte.Parse(val.Substring(3, 2), NumberStyles.AllowHexSpecifier); |
| 58 | + this.B = byte.Parse(val.Substring(5, 2), NumberStyles.AllowHexSpecifier); |
| 59 | + break; |
| 60 | + case 9: |
| 61 | + this.A = byte.Parse(val.Substring(1, 2), NumberStyles.AllowHexSpecifier); |
| 62 | + this.R = byte.Parse(val.Substring(3, 2), NumberStyles.AllowHexSpecifier); |
| 63 | + this.G = byte.Parse(val.Substring(5, 2), NumberStyles.AllowHexSpecifier); |
| 64 | + this.B = byte.Parse(val.Substring(7, 2), NumberStyles.AllowHexSpecifier); |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + |
40 | 69 | #if WINDOWS_UWP || __ANDROID__ |
41 | 70 | /// <summary> |
42 | 71 | /// Initializes a new instance of the <see cref="Color"/> class with a <see cref="System.Drawing.Color"/>. |
@@ -186,7 +215,7 @@ public Color(UIKit.UIColor color) |
186 | 215 | { |
187 | 216 | if (color.CGColor.NumberOfComponents == 2) |
188 | 217 | { |
189 | | - var rgb = (byte)(color.CGColor.Components[0] * 255); |
| 218 | + byte rgb = (byte)(color.CGColor.Components[0] * 255); |
190 | 219 | this.R = rgb; |
191 | 220 | this.G = rgb; |
192 | 221 | this.B = rgb; |
|
0 commit comments