2026-06-08 14:31:31 +07:00
|
|
|
|
namespace QWERTYkez.ExcelProcessor;
|
2026-06-05 15:58:03 +07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Определяет заливку (фон) ячейки.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public readonly struct CellFill : IEquatable<CellFill>
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>Цвет фона.</summary>
|
2026-06-19 15:06:40 +07:00
|
|
|
|
public System.Drawing.Color? BackgroundColor { get; init; }
|
2026-06-05 15:58:03 +07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>Создаёт элемент Fill для Open XML.</summary>
|
|
|
|
|
|
public Fill? ToFill()
|
|
|
|
|
|
{
|
2026-06-19 15:06:40 +07:00
|
|
|
|
if (BackgroundColor is not { } c) return null;
|
2026-06-05 15:58:03 +07:00
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Создаёт CellFill из элемента Fill Open XML.</summary>
|
|
|
|
|
|
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)
|
|
|
|
|
|
);
|
2026-06-19 15:06:40 +07:00
|
|
|
|
return new CellFill { BackgroundColor = color };
|
2026-06-05 15:58:03 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|