mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 13:28:37 +00:00
47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
|
|
namespace HPPH.Generators;
|
|
|
|
internal class Average : IGeneratorFeature
|
|
{
|
|
public IEnumerable<(string name, string source)> GenerateFor(ColorFormatData colorFormat)
|
|
{
|
|
yield return ($"ColorFormat{colorFormat.Format}.Average", GenerateColorFormatAverage(colorFormat));
|
|
}
|
|
|
|
public IEnumerable<(string name, string source)> GenerateFor(ImmutableArray<ColorFormatData> colorFormats)
|
|
{
|
|
yield return ("IColorFormat.Average", GenerateColorFormatInterfaceAverage());
|
|
}
|
|
|
|
private static string GenerateColorFormatAverage(ColorFormatData colorFormat)
|
|
{
|
|
return $$"""
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace HPPH;
|
|
|
|
public sealed partial class ColorFormat{{colorFormat.Format}}
|
|
{
|
|
#region Methods
|
|
|
|
unsafe IColor IColorFormat.Average(ReadOnlySpan<byte> data) => PixelHelper.Average(MemoryMarshal.Cast<byte, Color{{colorFormat.Format}}>(data));
|
|
|
|
#endregion
|
|
}
|
|
""";
|
|
}
|
|
|
|
private static string GenerateColorFormatInterfaceAverage()
|
|
{
|
|
return """
|
|
namespace HPPH;
|
|
|
|
public partial interface IColorFormat
|
|
{
|
|
internal IColor Average(ReadOnlySpan<byte> data);
|
|
}
|
|
""";
|
|
}
|
|
} |