Files
QWERTYkez.OpenXmlProcessors/QWERTYkez.ExcelProcessor/Editors/CellFill.cs
melekhin e373d4108a
All checks were successful
Publish NuGet packages / publish (push) Successful in 28s
many debugs
2026-06-19 15:06:40 +07:00

51 lines
1.7 KiB
C#

namespace QWERTYkez.ExcelProcessor;
/// <summary>
/// Определяет заливку (фон) ячейки.
/// </summary>
public readonly struct CellFill : IEquatable<CellFill>
{
/// <summary>Цвет фона.</summary>
public System.Drawing.Color? BackgroundColor { get; init; }
/// <summary>Создаёт элемент Fill для Open XML.</summary>
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;
}
/// <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)
);
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;
}