|
| 1 | +using System.Globalization; |
| 2 | + |
| 3 | +namespace HeyBox; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// 表示一个 CSS 颜色值,参见 |
| 7 | +/// <see href="https://developer.mozilla.org/docs/Web/CSS/color_value"><color> - CSS: Cascading Style Sheets | MDN</see>。 |
| 8 | +/// </summary> |
| 9 | +public readonly struct CssColor : IEquatable<CssColor> |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// 获取颜色的字符串值。 |
| 13 | + /// </summary> |
| 14 | + public string Value { get; } = string.Empty; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// 获取一个表示默认颜色的 <see cref="CssColor"/> 实例。 |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// 黑盒语音客户端将使用等同于 <c>CssColor.FromVariable("--brank-fill")</c> 的值。 |
| 21 | + /// </remarks> |
| 22 | + public static CssColor Default { get; } = new(string.Empty); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// 获取一个表示透明颜色的 <see cref="CssColor"/> 实例。 |
| 26 | + /// </summary> |
| 27 | + public static CssColor Transparent { get; } = new("transparent"); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// 使用指定的颜色字符串值初始化 <see cref="CssColor"/> 结构。 |
| 31 | + /// </summary> |
| 32 | + /// <param name="value">颜色的字符串值。</param> |
| 33 | + private CssColor(string value) |
| 34 | + { |
| 35 | + Value = value; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// 通过颜色值创建 <see cref="CssColor"/> 实例。 |
| 40 | + /// </summary> |
| 41 | + /// <param name="value"> CSS 颜色值字符串。</param> |
| 42 | + /// <returns> 对应的 <see cref="CssColor"/> 实例。</returns> |
| 43 | + public static CssColor FromValue(string value) => new(value); |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// 通过颜色名称创建 <see cref="CssColor"/> 实例。 |
| 47 | + /// </summary> |
| 48 | + /// <param name="name">CSS 颜色名称。</param> |
| 49 | + /// <returns>对应的 <see cref="CssColor"/> 实例。</returns> |
| 50 | + /// <exception cref="ArgumentException">当 <paramref name="name"/> 为空或无效时抛出。</exception> |
| 51 | + public static CssColor FromName(string name) |
| 52 | + { |
| 53 | + if (string.IsNullOrWhiteSpace(name)) |
| 54 | + throw new ArgumentException("Color name cannot be null or empty.", nameof(name)); |
| 55 | + return new CssColor(name); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// 通过十六进制颜色值创建 <see cref="CssColor"/> 实例。 |
| 60 | + /// </summary> |
| 61 | + /// <param name="hex">十六进制颜色值(如 <c>#FFF</c> 或 <c>#FFFFFF</c>)。</param> |
| 62 | + /// <returns>对应的 <see cref="CssColor"/> 实例。</returns> |
| 63 | + /// <exception cref="ArgumentException">当 <paramref name="hex"/> 格式无效时抛出。</exception> |
| 64 | + public static CssColor FromHex(string hex) |
| 65 | + { |
| 66 | + if (string.IsNullOrWhiteSpace(hex) || |
| 67 | + hex.Length is not (4 or 7) || |
| 68 | + !hex.StartsWith('#')) |
| 69 | + throw new ArgumentException("Invalid hex color format.", nameof(hex)); |
| 70 | + return new CssColor(hex); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// 通过 RGB 值创建 <see cref="CssColor"/> 实例。 |
| 75 | + /// </summary> |
| 76 | + /// <param name="r">红色分量(0-255)。</param> |
| 77 | + /// <param name="g">绿色分量(0-255)。</param> |
| 78 | + /// <param name="b">蓝色分量(0-255)。</param> |
| 79 | + /// <returns>对应的 <see cref="CssColor"/> 实例。</returns> |
| 80 | + public static CssColor FromRgb(byte r, byte g, byte b) => new($"rgb({r}, {g}, {b})"); |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// 通过 RGBA 值创建 <see cref="CssColor"/> 实例。 |
| 84 | + /// </summary> |
| 85 | + /// <param name="r">红色分量(0-255)。</param> |
| 86 | + /// <param name="g">绿色分量(0-255)。</param> |
| 87 | + /// <param name="b">蓝色分量(0-255)。</param> |
| 88 | + /// <param name="a">透明度(0-1)。</param> |
| 89 | + /// <returns>对应的 <see cref="CssColor"/> 实例。</returns> |
| 90 | + /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="a"/> 不在 0 到 1 范围内时抛出。</exception> |
| 91 | + public static CssColor FromRgba(byte r, byte g, byte b, float a) |
| 92 | + { |
| 93 | + if (a is < 0 or > 1) |
| 94 | + throw new ArgumentOutOfRangeException(nameof(a), "Alpha must be between 0 and 1."); |
| 95 | + return new CssColor($"rgba({r}, {g}, {b}, {a.ToString(CultureInfo.InvariantCulture)})"); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// 通过 HSL 值创建 <see cref="CssColor"/> 实例。 |
| 100 | + /// </summary> |
| 101 | + /// <param name="h">色调(0-360)。</param> |
| 102 | + /// <param name="s">饱和度(0-100)。</param> |
| 103 | + /// <param name="l">亮度(0-100)。</param> |
| 104 | + /// <returns>对应的 <see cref="CssColor"/> 实例。</returns> |
| 105 | + /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="h"/>、<paramref name="s"/> 或 <paramref name="l"/> 不在有效范围内时抛出。</exception> |
| 106 | + public static CssColor FromHsl(float h, float s, float l) |
| 107 | + { |
| 108 | + if (h is < 0 or >= 360) |
| 109 | + throw new ArgumentOutOfRangeException(nameof(h), "Hue must be between 0 and 360."); |
| 110 | + if (s is < 0 or > 100) |
| 111 | + throw new ArgumentOutOfRangeException(nameof(s), "Saturation must be between 0 and 100."); |
| 112 | + if (l is < 0 or > 100) |
| 113 | + throw new ArgumentOutOfRangeException(nameof(l), "Lightness must be between 0 and 100."); |
| 114 | + return new CssColor($"hsl({h.ToString(CultureInfo.InvariantCulture)}, {s.ToString(CultureInfo.InvariantCulture)}%, {l.ToString(CultureInfo.InvariantCulture)}%)"); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// 通过 HSLA 值创建 <see cref="CssColor"/> 实例。 |
| 119 | + /// </summary> |
| 120 | + /// <param name="h">色调(0-360)。</param> |
| 121 | + /// <param name="s">饱和度(0-100)。</param> |
| 122 | + /// <param name="l">亮度(0-100)。</param> |
| 123 | + /// <param name="a">透明度(0-1)。</param> |
| 124 | + /// <returns>对应的 <see cref="CssColor"/> 实例。</returns> |
| 125 | + /// <exception cref="ArgumentOutOfRangeException">当 <paramref name="h"/>、<paramref name="s"/>、<paramref name="l"/> 或 <paramref name="a"/> 不在有效范围内时抛出。</exception> |
| 126 | + public static CssColor FromHsla(float h, float s, float l, float a) |
| 127 | + { |
| 128 | + if (h is < 0 or >= 360) |
| 129 | + throw new ArgumentOutOfRangeException(nameof(h), "Hue must be between 0 and 360."); |
| 130 | + if (s is < 0 or > 100) |
| 131 | + throw new ArgumentOutOfRangeException(nameof(s), "Saturation must be between 0 and 100."); |
| 132 | + if (l is < 0 or > 100) |
| 133 | + throw new ArgumentOutOfRangeException(nameof(l), "Lightness must be between 0 and 100."); |
| 134 | + if (a is < 0 or > 1) |
| 135 | + throw new ArgumentOutOfRangeException(nameof(a), "Alpha must be between 0 and 1."); |
| 136 | + return new CssColor($"hsla({h.ToString(CultureInfo.InvariantCulture)}, {s.ToString(CultureInfo.InvariantCulture)}%, {l.ToString(CultureInfo.InvariantCulture)}%, {a.ToString(CultureInfo.InvariantCulture)})"); |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// 通过 CSS 变量名创建 <see cref="CssColor"/> 实例。 |
| 141 | + /// </summary> |
| 142 | + /// <param name="variableName">CSS 变量名(不包含 <c>var()</c> 前缀,例如 <c>--brand-fill</c>)。</param> |
| 143 | + /// <returns>对应的 <see cref="CssColor"/> 实例。</returns> |
| 144 | + /// <exception cref="ArgumentException">当 <paramref name="variableName"/> 为空或无效时抛出。</exception> |
| 145 | + public static CssColor FromVariable(string variableName) |
| 146 | + { |
| 147 | + if (string.IsNullOrWhiteSpace(variableName)) |
| 148 | + throw new ArgumentException("CSS variable name cannot be null or empty.", nameof(variableName)); |
| 149 | + return new CssColor($"var(--{variableName.TrimStart('-')})"); |
| 150 | + } |
| 151 | + |
| 152 | + /// <inheritdoc cref="HeyBox.CssColor.Value" /> |
| 153 | + public override string ToString() => Value; |
| 154 | + |
| 155 | + /// <inheritdoc /> |
| 156 | + public override bool Equals(object? obj) => obj is CssColor other && Equals(other); |
| 157 | + |
| 158 | + /// <inheritdoc /> |
| 159 | + public bool Equals(CssColor other) => Value == other.Value; |
| 160 | + |
| 161 | + /// <inheritdoc /> |
| 162 | + public override int GetHashCode() => Value.GetHashCode(); |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// 确定两个 <see cref="CssColor"/> 实例是否相等。 |
| 166 | + /// </summary> |
| 167 | + /// <param name="left">左侧的 <see cref="CssColor"/> 实例。</param> |
| 168 | + /// <param name="right">右侧的 <see cref="CssColor"/> 实例。</param> |
| 169 | + /// <returns>如果相等,则为 true;否则为 false。</returns> |
| 170 | + public static bool operator ==(CssColor left, CssColor right) => left.Equals(right); |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// 确定两个 <see cref="CssColor"/> 实例是否不相等。 |
| 174 | + /// </summary> |
| 175 | + /// <param name="left">左侧的 <see cref="CssColor"/> 实例。</param> |
| 176 | + /// <param name="right">右侧的 <see cref="CssColor"/> 实例。</param> |
| 177 | + /// <returns>如果不相等,则为 true;否则为 false。</returns> |
| 178 | + public static bool operator !=(CssColor left, CssColor right) => !(left == right); |
| 179 | +} |
0 commit comments