From 72cce16597dd2ccc22808252a61015feb5440696 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 31 Aug 2021 11:59:46 +0200 Subject: [PATCH] Added ISpanFormattable interface to pixel types --- src/ComputeSharp/Core/PixelTypes/Bgra32.cs | 14 +++++++++++++- src/ComputeSharp/Core/PixelTypes/R16.cs | 14 +++++++++++++- src/ComputeSharp/Core/PixelTypes/R8.cs | 14 +++++++++++++- src/ComputeSharp/Core/PixelTypes/Rg16.cs | 14 +++++++++++++- src/ComputeSharp/Core/PixelTypes/Rg32.cs | 14 +++++++++++++- src/ComputeSharp/Core/PixelTypes/Rgba32.cs | 14 +++++++++++++- src/ComputeSharp/Core/PixelTypes/Rgba64.cs | 14 +++++++++++++- 7 files changed, 91 insertions(+), 7 deletions(-) diff --git a/src/ComputeSharp/Core/PixelTypes/Bgra32.cs b/src/ComputeSharp/Core/PixelTypes/Bgra32.cs index b0755c97..76f5c069 100644 --- a/src/ComputeSharp/Core/PixelTypes/Bgra32.cs +++ b/src/ComputeSharp/Core/PixelTypes/Bgra32.cs @@ -18,7 +18,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Bgra32 : IEquatable, IUnorm, IUnorm +public struct Bgra32 : IEquatable, ISpanFormattable, IUnorm, IUnorm { /// /// The blue component. @@ -118,4 +118,16 @@ public struct Bgra32 : IEquatable, IUnorm, IUnorm /// public override readonly string ToString() => $"{nameof(Bgra32)}({this.B}, {this.G}, {this.R}, {this.A})"; + + /// + public string ToString(string? format, IFormatProvider? formatProvider) + { + return string.Create(formatProvider, $"{nameof(Bgra32)}({this.B}, {this.G}, {this.R}, {this.A})"); + } + + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + return destination.TryWrite(provider, $"{nameof(Bgra32)}({this.B}, {this.G}, {this.R}, {this.A})", out charsWritten); + } } diff --git a/src/ComputeSharp/Core/PixelTypes/R16.cs b/src/ComputeSharp/Core/PixelTypes/R16.cs index 2647356a..aaba914a 100644 --- a/src/ComputeSharp/Core/PixelTypes/R16.cs +++ b/src/ComputeSharp/Core/PixelTypes/R16.cs @@ -14,7 +14,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct R16 : IEquatable, IUnorm +public struct R16 : IEquatable, ISpanFormattable, IUnorm { /// /// The red component. @@ -77,4 +77,16 @@ public struct R16 : IEquatable, IUnorm /// public override readonly string ToString() => $"{nameof(R16)}({this.R})"; + + /// + public string ToString(string? format, IFormatProvider? formatProvider) + { + return string.Create(formatProvider, $"{nameof(R16)}({this.R})"); + } + + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + return destination.TryWrite(provider, $"{nameof(R16)}({this.R})", out charsWritten); + } } diff --git a/src/ComputeSharp/Core/PixelTypes/R8.cs b/src/ComputeSharp/Core/PixelTypes/R8.cs index b906f94a..f249f647 100644 --- a/src/ComputeSharp/Core/PixelTypes/R8.cs +++ b/src/ComputeSharp/Core/PixelTypes/R8.cs @@ -14,7 +14,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct R8 : IEquatable, IUnorm +public struct R8 : IEquatable, ISpanFormattable, IUnorm { /// /// The red component. @@ -77,4 +77,16 @@ public struct R8 : IEquatable, IUnorm /// public override readonly string ToString() => $"{nameof(R8)}({this.R})"; + + /// + public string ToString(string? format, IFormatProvider? formatProvider) + { + return string.Create(formatProvider, $"{nameof(R8)}({this.R})"); + } + + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + return destination.TryWrite(provider, $"{nameof(R8)}({this.R})", out charsWritten); + } } diff --git a/src/ComputeSharp/Core/PixelTypes/Rg16.cs b/src/ComputeSharp/Core/PixelTypes/Rg16.cs index f67ebced..c27fb85c 100644 --- a/src/ComputeSharp/Core/PixelTypes/Rg16.cs +++ b/src/ComputeSharp/Core/PixelTypes/Rg16.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct Rg16 : IEquatable, IUnorm, IUnorm +public struct Rg16 : IEquatable, ISpanFormattable, IUnorm, IUnorm { /// /// The red component. @@ -85,4 +85,16 @@ public struct Rg16 : IEquatable, IUnorm, IUnorm /// public override readonly string ToString() => $"{nameof(Rg16)}({this.R}, {this.G})"; + + /// + public string ToString(string? format, IFormatProvider? formatProvider) + { + return string.Create(formatProvider, $"{nameof(Rg16)}({this.R}, {this.G})"); + } + + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + return destination.TryWrite(provider, $"{nameof(Rg16)}({this.R}, {this.G})", out charsWritten); + } } diff --git a/src/ComputeSharp/Core/PixelTypes/Rg32.cs b/src/ComputeSharp/Core/PixelTypes/Rg32.cs index d894532a..2e268f65 100644 --- a/src/ComputeSharp/Core/PixelTypes/Rg32.cs +++ b/src/ComputeSharp/Core/PixelTypes/Rg32.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct Rg32 : IEquatable, IUnorm, IUnorm +public struct Rg32 : IEquatable, ISpanFormattable, IUnorm, IUnorm { /// /// The red component. @@ -85,4 +85,16 @@ public struct Rg32 : IEquatable, IUnorm, IUnorm /// public override readonly string ToString() => $"{nameof(Rg32)}({this.R}, {this.G})"; + + /// + public string ToString(string? format, IFormatProvider? formatProvider) + { + return string.Create(formatProvider, $"{nameof(Rg32)}({this.R}, {this.G})"); + } + + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + return destination.TryWrite(provider, $"{nameof(Rg32)}({this.R}, {this.G})", out charsWritten); + } } diff --git a/src/ComputeSharp/Core/PixelTypes/Rgba32.cs b/src/ComputeSharp/Core/PixelTypes/Rgba32.cs index c8779b2f..e3664dd4 100644 --- a/src/ComputeSharp/Core/PixelTypes/Rgba32.cs +++ b/src/ComputeSharp/Core/PixelTypes/Rgba32.cs @@ -17,7 +17,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Rgba32 : IEquatable, IUnorm, IUnorm +public struct Rgba32 : IEquatable, ISpanFormattable, IUnorm, IUnorm { /// /// The red component. @@ -117,4 +117,16 @@ public struct Rgba32 : IEquatable, IUnorm, IUnorm /// public override readonly string ToString() => $"{nameof(Rgba32)}({this.R}, {this.G}, {this.B}, {this.A})"; + + /// + public string ToString(string? format, IFormatProvider? formatProvider) + { + return string.Create(formatProvider, $"{nameof(Rgba32)}({this.R}, {this.G}, {this.B}, {this.A})"); + } + + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + return destination.TryWrite(provider, $"{nameof(Rgba32)}({this.R}, {this.G}, {this.B}, {this.A})", out charsWritten); + } } diff --git a/src/ComputeSharp/Core/PixelTypes/Rgba64.cs b/src/ComputeSharp/Core/PixelTypes/Rgba64.cs index bb77e891..bc228016 100644 --- a/src/ComputeSharp/Core/PixelTypes/Rgba64.cs +++ b/src/ComputeSharp/Core/PixelTypes/Rgba64.cs @@ -17,7 +17,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Rgba64 : IEquatable, IUnorm, IUnorm +public struct Rgba64 : IEquatable, ISpanFormattable, IUnorm, IUnorm { /// /// The red component. @@ -117,4 +117,16 @@ public struct Rgba64 : IEquatable, IUnorm, IUnorm /// public override readonly string ToString() => $"{nameof(Rgba64)}({this.R}, {this.G}, {this.B}, {this.A})"; + + /// + public string ToString(string? format, IFormatProvider? formatProvider) + { + return string.Create(formatProvider, $"{nameof(Rgba64)}({this.R}, {this.G}, {this.B}, {this.A})"); + } + + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + return destination.TryWrite(provider, $"{nameof(Rgba64)}({this.R}, {this.G}, {this.B}, {this.A})", out charsWritten); + } }