refactor: simplify imgui extensions

This commit is contained in:
2026-01-16 14:08:08 -05:00
parent c75c0551ee
commit fa500f4c30
+10 -11
View File
@@ -8,7 +8,7 @@ namespace Spectrum
{ {
public static class ImGuiExtensions public static class ImGuiExtensions
{ {
private static readonly Dictionary<string, string> _sliderEditBuffers = new(); private static readonly Dictionary<string, string> _sliderEditBuffers = [];
public static bool ColorEditHSV3(string label, ref Scalar hsv) public static bool ColorEditHSV3(string label, ref Scalar hsv)
{ {
ImGui.PushID(label); ImGui.PushID(label);
@@ -54,8 +54,8 @@ namespace Spectrum
Vector3 rgbColor = OpenCvHsvToRgb(hsv); Vector3 rgbColor = OpenCvHsvToRgb(hsv);
ImGui.SameLine(); ImGui.SameLine();
Vector2 btnSize = new Vector2(previewWidth, previewWidth); Vector2 btnSize = new(previewWidth, previewWidth);
Vector4 previewCol4 = new Vector4(rgbColor.X, rgbColor.Y, rgbColor.Z, 1.0f); Vector4 previewCol4 = new(rgbColor.X, rgbColor.Y, rgbColor.Z, 1.0f);
string previewBtnId = $"##ColorPreview_{label}"; string previewBtnId = $"##ColorPreview_{label}";
if (ImGui.ColorButton(previewBtnId, previewCol4, ImGuiColorEditFlags.NoInputs, btnSize)) if (ImGui.ColorButton(previewBtnId, previewCol4, ImGuiColorEditFlags.NoInputs, btnSize))
{ {
@@ -101,13 +101,12 @@ namespace Spectrum
double huePrime = hueDeg / 60.0; double huePrime = hueDeg / 60.0;
double x = c * (1 - Math.Abs(huePrime % 2 - 1)); double x = c * (1 - Math.Abs(huePrime % 2 - 1));
double m = value - c; double m = value - c;
double r1 = 0, g1 = 0, b1 = 0;
int region = (int)Math.Floor(huePrime); int region = (int)Math.Floor(huePrime);
if (region >= 6) if (region >= 6)
region = 0; region = 0;
double r1;
double b1;
double g1;
switch (region) switch (region)
{ {
case 0: case 0:
@@ -224,7 +223,7 @@ namespace Spectrum
int fPos = core.IndexOf('f', pos + 2); int fPos = core.IndexOf('f', pos + 2);
if (fPos > pos) if (fPos > pos)
{ {
string digits = core.Substring(pos + 2, fPos - (pos + 2)); string digits = core[(pos + 2)..fPos];
if (int.TryParse(digits, out int prec)) if (int.TryParse(digits, out int prec))
result = v.ToString("F" + prec, CultureInfo.InvariantCulture); result = v.ToString("F" + prec, CultureInfo.InvariantCulture);
else else
@@ -286,7 +285,7 @@ namespace Spectrum
int fPos = format.IndexOf('f', pos + 2); int fPos = format.IndexOf('f', pos + 2);
if (fPos > pos) if (fPos > pos)
{ {
string digits = format.Substring(pos + 2, fPos - (pos + 2)); string digits = format[(pos + 2)..fPos];
if (int.TryParse(digits, out int prec)) if (int.TryParse(digits, out int prec))
newValue = (float)Math.Round(newValue, prec); newValue = (float)Math.Round(newValue, prec);
} }
@@ -311,7 +310,7 @@ namespace Spectrum
if (ImGui.InputText($"##SliderFillEdit_{label}", ref buffer, 32, inputFlags)) if (ImGui.InputText($"##SliderFillEdit_{label}", ref buffer, 32, inputFlags))
{ {
string parseStr = buffer.Trim(); string parseStr = buffer.Trim();
if (parseStr.EndsWith("%")) parseStr = parseStr.TrimEnd('%'); if (parseStr.EndsWith('%')) parseStr = parseStr.TrimEnd('%');
if (float.TryParse(parseStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float typed)) if (float.TryParse(parseStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float typed))
{ {
float clamped; float clamped;
@@ -428,7 +427,7 @@ namespace Spectrum
Vector2 headerMin = ImGui.GetCursorScreenPos(); Vector2 headerMin = ImGui.GetCursorScreenPos();
Vector2 headerMax = new Vector2(headerMin.X + g.Width, headerMin.Y + headerHeight); Vector2 headerMax = new(headerMin.X + g.Width, headerMin.Y + headerHeight);
var drawList = ImGui.GetWindowDrawList(); var drawList = ImGui.GetWindowDrawList();
uint bgCol = ImGui.GetColorU32(ImGuiCol.FrameBg); uint bgCol = ImGui.GetColorU32(ImGuiCol.FrameBg);