|
1 | 1 | using System.Runtime.CompilerServices; |
| 2 | +using Robust.Shared.Maths; |
2 | 3 |
|
3 | 4 | namespace DMCompiler; |
4 | 5 |
|
@@ -73,4 +74,108 @@ public static int BitShiftLeft(int left, int right) { |
73 | 74 | public static int BitShiftRight(int left, int right) { |
74 | 75 | return (left & 0x00FFFFFF) >> (right) ; |
75 | 76 | } |
| 77 | + |
| 78 | + public enum ColorSpace { |
| 79 | + RGB = 0, |
| 80 | + HSV = 1, |
| 81 | + HSL = 2 |
| 82 | + } |
| 83 | + |
| 84 | + public static string ParseRgb((string? Name, float? Value)[] arguments) { |
| 85 | + string result; |
| 86 | + float? color1 = null; |
| 87 | + float? color2 = null; |
| 88 | + float? color3 = null; |
| 89 | + float? a = null; |
| 90 | + ColorSpace space = ColorSpace.RGB; |
| 91 | + |
| 92 | + if (arguments[0].Name is null) { |
| 93 | + if (arguments.Length is < 3 or > 5) |
| 94 | + throw new Exception("Expected 3 to 5 arguments for rgb()"); |
| 95 | + |
| 96 | + color1 = arguments[0].Value; |
| 97 | + color2 = arguments[1].Value; |
| 98 | + color3 = arguments[2].Value; |
| 99 | + a = (arguments.Length >= 4) ? arguments[3].Value : null; |
| 100 | + if (arguments.Length == 5) |
| 101 | + space = arguments[4].Value is null ? ColorSpace.RGB : (ColorSpace)(int)arguments[4].Value!; |
| 102 | + } else { |
| 103 | + foreach (var arg in arguments) { |
| 104 | + var name = arg.Name ?? string.Empty; |
| 105 | + |
| 106 | + if (name.StartsWith("r", StringComparison.InvariantCultureIgnoreCase) && color1 is null) { |
| 107 | + color1 = arg.Value; |
| 108 | + space = ColorSpace.RGB; |
| 109 | + } else if (name.StartsWith("g", StringComparison.InvariantCultureIgnoreCase) && color2 is null) { |
| 110 | + color2 = arg.Value; |
| 111 | + space = ColorSpace.RGB; |
| 112 | + } else if (name.StartsWith("b", StringComparison.InvariantCultureIgnoreCase) && color3 is null) { |
| 113 | + color3 = arg.Value; |
| 114 | + space = ColorSpace.RGB; |
| 115 | + } else if (name.StartsWith("h", StringComparison.InvariantCultureIgnoreCase) && color1 is null) { |
| 116 | + color1 = arg.Value; |
| 117 | + space = ColorSpace.HSV; |
| 118 | + } else if (name.StartsWith("s", StringComparison.InvariantCultureIgnoreCase) && color2 is null) { |
| 119 | + color2 = arg.Value; |
| 120 | + space = ColorSpace.HSV; |
| 121 | + } else if (name.StartsWith("v", StringComparison.InvariantCultureIgnoreCase) && color3 is null) { |
| 122 | + color3 = arg.Value; |
| 123 | + space = ColorSpace.HSV; |
| 124 | + } else if (name.StartsWith("l", StringComparison.InvariantCultureIgnoreCase) && color3 is null) { |
| 125 | + color3 = arg.Value; |
| 126 | + space = ColorSpace.HSL; |
| 127 | + } else if (name.StartsWith("a", StringComparison.InvariantCultureIgnoreCase) && a is null) |
| 128 | + a = arg.Value; |
| 129 | + else if (name == "space" && space == default) |
| 130 | + space = (ColorSpace)(int)arg.Value!; |
| 131 | + else |
| 132 | + throw new Exception($"Invalid or double arg \"{name}\""); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + color1 ??= 0; |
| 137 | + color2 ??= 0; |
| 138 | + color3 ??= 0; |
| 139 | + byte aValue = a is null ? (byte)255 : (byte)Math.Clamp((int)a, 0, 255); |
| 140 | + Color color; |
| 141 | + |
| 142 | + switch (space) { |
| 143 | + case ColorSpace.RGB: { |
| 144 | + byte r = (byte)Math.Clamp(color1.Value, 0, 255); |
| 145 | + byte g = (byte)Math.Clamp(color2.Value, 0, 255); |
| 146 | + byte b = (byte)Math.Clamp(color3.Value, 0, 255); |
| 147 | + |
| 148 | + color = new Color(r, g, b, aValue); |
| 149 | + break; |
| 150 | + } |
| 151 | + case ColorSpace.HSV: { |
| 152 | + // TODO: Going beyond the max defined in the docs returns a different value. Don't know why. |
| 153 | + float h = Math.Clamp(color1.Value, 0, 360) / 360f; |
| 154 | + float s = Math.Clamp(color2.Value, 0, 100) / 100f; |
| 155 | + float v = Math.Clamp(color3.Value, 0, 100) / 100f; |
| 156 | + |
| 157 | + color = Color.FromHsv(new(h, s, v, aValue / 255f)); |
| 158 | + break; |
| 159 | + } |
| 160 | + case ColorSpace.HSL: { |
| 161 | + float h = Math.Clamp(color1.Value, 0, 360) / 360f; |
| 162 | + float s = Math.Clamp(color2.Value, 0, 100) / 100f; |
| 163 | + float l = Math.Clamp(color3.Value, 0, 100) / 100f; |
| 164 | + |
| 165 | + color = Color.FromHsl(new(h, s, l, aValue / 255f)); |
| 166 | + break; |
| 167 | + } |
| 168 | + default: |
| 169 | + throw new Exception($"Unimplemented color space {space}"); |
| 170 | + } |
| 171 | + |
| 172 | + // TODO: There is a difference between passing null and not passing a fourth arg at all |
| 173 | + if (a is null) { |
| 174 | + result = $"#{color.RByte:X2}{color.GByte:X2}{color.BByte:X2}".ToLower(); |
| 175 | + } else { |
| 176 | + result = $"#{color.RByte:X2}{color.GByte:X2}{color.BByte:X2}{color.AByte:X2}".ToLower(); |
| 177 | + } |
| 178 | + |
| 179 | + return result; |
| 180 | + } |
76 | 181 | } |
0 commit comments