namespace QWERTYkez.ExcelProcessor; /// /// Определяет заливку (фон) ячейки. /// public readonly struct CellFill : IEquatable { /// Цвет фона. public System.Drawing.Color? BackgroundColor { get; init; } /// Создаёт элемент Fill для Open XML. public Fill? ToFill() { if (BackgroundColor is not { } c) return null; var fill = new Fill { PatternFill = new PatternFill { PatternType = PatternValues.Solid, ForegroundColor = new ForegroundColor { Rgb = $"{c.R:X2}{c.G:X2}{c.B:X2}" } } }; return fill; } /// Создаёт CellFill из элемента Fill Open XML. public static CellFill FromFill(Fill? fill) { if (fill?.PatternFill?.ForegroundColor?.Rgb?.Value is not string rgb || rgb.Length < 6) return default; var color = System.Drawing.Color.FromArgb( Convert.ToByte(rgb.Substring(0, 2), 16), Convert.ToByte(rgb.Substring(2, 2), 16), Convert.ToByte(rgb.Substring(4, 2), 16) ); return new CellFill { BackgroundColor = color }; } public override bool Equals(object? obj) => obj is CellFill other && Equals(other); public bool Equals(CellFill other) => this == other; public static bool operator ==(CellFill left, CellFill right) => Equals(left.BackgroundColor, right.BackgroundColor); public static bool operator !=(CellFill left, CellFill right) => !(left == right); public override int GetHashCode() => BackgroundColor?.GetHashCode() ?? 0; }