| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a CSS <c>color</c> value within the Allyaria theming system. Provides a strongly typed wrapper around col |
| | | 5 | | /// values supporting parsing and conversion to and from <see cref="HexColor" /> and string representations. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed record StyleColor : StyleValueBase |
| | | 8 | | { |
| | | 9 | | /// <summary>Initializes a new instance of the <see cref="StyleColor" /> record using a string color value.</summary |
| | | 10 | | /// <param name="value">The color value as a string, typically in hexadecimal, RGB(A), or named CSS color format.</p |
| | | 11 | | /// <exception cref="ArgumentException"> |
| | | 12 | | /// Thrown when the provided <paramref name="value" /> cannot be parsed into a valid <see cref="HexColor" />. |
| | | 13 | | /// </exception> |
| | | 14 | | public StyleColor(string value) |
| | 25117 | 15 | | : this(color: new HexColor(value: value)) { } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="StyleColor" /> record using a <see cref="HexColor" /> instance. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="color">The <see cref="HexColor" /> instance to represent.</param> |
| | | 21 | | public StyleColor(HexColor color) |
| | 18031 | 22 | | : base(value: color.ToString()) |
| | 18031 | 23 | | => Color = color; |
| | | 24 | | |
| | | 25 | | /// <summary>Gets the strongly typed <see cref="HexColor" /> value represented by this instance.</summary> |
| | 17483 | 26 | | public HexColor Color { get; } |
| | | 27 | | |
| | | 28 | | /// <summary>Parses a string representation of a CSS color into a <see cref="StyleColor" /> instance.</summary> |
| | | 29 | | /// <param name="value">The string representation of the color to parse. Supports hexadecimal, RGB(A), and named col |
| | | 30 | | /// <returns>A new <see cref="StyleColor" /> instance representing the parsed color.</returns> |
| | | 31 | | /// <exception cref="ArgumentException"> |
| | | 32 | | /// Thrown when the provided <paramref name="value" /> cannot be parsed into a valid |
| | | 33 | | /// color format. |
| | | 34 | | /// </exception> |
| | 7 | 35 | | public static StyleColor Parse(string? value) => new(value: value ?? string.Empty); |
| | | 36 | | |
| | | 37 | | /// <summary>Attempts to parse a string representation of a CSS color into a <see cref="StyleColor" /> instance.</su |
| | | 38 | | /// <param name="value">The string representation of the color value to parse.</param> |
| | | 39 | | /// <param name="result"> |
| | | 40 | | /// When this method returns, contains the parsed <see cref="StyleColor" /> instance or <see langword="null" /> if p |
| | | 41 | | /// failed. |
| | | 42 | | /// </param> |
| | | 43 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 44 | | public static bool TryParse(string? value, out StyleColor? result) |
| | | 45 | | { |
| | | 46 | | try |
| | | 47 | | { |
| | 2 | 48 | | result = Parse(value: value); |
| | | 49 | | |
| | 1 | 50 | | return true; |
| | | 51 | | } |
| | 1 | 52 | | catch |
| | | 53 | | { |
| | 1 | 54 | | result = null; |
| | | 55 | | |
| | 1 | 56 | | return false; |
| | | 57 | | } |
| | 2 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary>Implicitly converts a string value into a <see cref="StyleColor" /> instance.</summary> |
| | | 61 | | /// <param name="value">The string representation of the color value.</param> |
| | | 62 | | /// <returns>A <see cref="StyleColor" /> instance representing the provided value.</returns> |
| | | 63 | | /// <exception cref="ArgumentException">Thrown when the provided string cannot be parsed into a valid color format.< |
| | 2 | 64 | | public static implicit operator StyleColor(string? value) => Parse(value: value); |
| | | 65 | | |
| | | 66 | | /// <summary>Implicitly converts a <see cref="StyleColor" /> instance to its string representation.</summary> |
| | | 67 | | /// <param name="value">The <see cref="StyleColor" /> instance to convert.</param> |
| | | 68 | | /// <returns> |
| | | 69 | | /// The CSS string representation of the color (typically in hex format), or an empty string if <paramref name="valu |
| | | 70 | | /// is <see langword="null" />. |
| | | 71 | | /// </returns> |
| | 2 | 72 | | public static implicit operator string(StyleColor? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 73 | | } |