using System.Text.RegularExpressions; namespace StitcherApi.Services.Utilities; public static class CoordinateHelper { private static readonly Regex CoordRegex = new(@"([A-Z]+)(\d+)", RegexOptions.Compiled); public static (int startRow, int endRow, int startCol, int endCol) ParseCanvasRect( string rectStr ) { var parts = rectStr.Split(':'); if (parts.Length != 2) { throw new ArgumentException("Invalid canvas_rect format. Expected format 'A1:H12'."); } var (r1, c1) = ParseSingleCoordinate(parts[0]); var (r2, c2) = ParseSingleCoordinate(parts[1]); return (Math.Min(r1, r2), Math.Max(r1, r2), Math.Min(c1, c2), Math.Max(c1, c2)); } public static string IndexToRow(int index) { index++; var result = ""; while (index > 0) { int remainder = (index - 1) % 26; result = (char)('A' + remainder) + result; index = (index - 1) / 26; } return result; } private static (int row, int col) ParseSingleCoordinate(string coord) { var match = CoordRegex.Match(coord); if (!match.Success) { throw new ArgumentException($"Invalid coordinate format: '{coord}'."); } string rowStr = match.Groups[1].Value; int col = int.Parse(match.Groups[2].Value) - 1; return (RowToIndex(rowStr), col); } private static int RowToIndex(string rowStr) { int index = 0; foreach (char c in rowStr) { index = index * 26 + (c - 'A' + 1); } return index - 1; } }