Files
QWERTYkez.OpenXmlProcessors/QWERTYkez.ExcelProcessor/Editors/CellFill.cs

53 lines
1.8 KiB
C#
Raw Normal View History

namespace QWERTYkez.ExcelProcessor.Editors;
/// <summary>
/// Определяет заливку (фон) ячейки.
/// </summary>
public readonly struct CellFill : IEquatable<CellFill>
{
/// <summary>Цвет фона.</summary>
public ExColor? BackgroundColor { get; init; }
/// <summary>Создаёт элемент Fill для Open XML.</summary>
public Fill? ToFill()
{
if (!BackgroundColor.HasValue || !BackgroundColor.Value.Color.HasValue)
return null;
var c = BackgroundColor.Value.Color.Value;
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 = new ExColor(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;
}