Skip to content

Commit

Permalink
Add spans to the color filters (#2879)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored Jun 3, 2024
1 parent 8e7a51d commit 5e18068
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions binding/SkiaSharp/SKColorFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace SkiaSharp
{
// TODO: `FilterColor` may be useful

public unsafe class SKColorFilter : SKObject, ISKReferenceCounted
{
public const int ColorMatrixSize = 20;
Expand Down Expand Up @@ -42,6 +40,11 @@ public static SKColorFilter CreateColorMatrix(float[] matrix)
{
if (matrix == null)
throw new ArgumentNullException(nameof(matrix));
return CreateColorMatrix(matrix.AsSpan());
}

public static SKColorFilter CreateColorMatrix(ReadOnlySpan<float> matrix)
{
if (matrix.Length != 20)
throw new ArgumentException("Matrix must have a length of 20.", nameof(matrix));
fixed (float* m = matrix) {
Expand All @@ -58,6 +61,11 @@ public static SKColorFilter CreateTable(byte[] table)
{
if (table == null)
throw new ArgumentNullException(nameof(table));
return CreateTable(table.AsSpan());
}

public static SKColorFilter CreateTable(ReadOnlySpan<byte> table)
{
if (table.Length != TableMaxLength)
throw new ArgumentException($"Table must have a length of {TableMaxLength}.", nameof(table));
fixed (byte* t = table) {
Expand All @@ -67,13 +75,26 @@ public static SKColorFilter CreateTable(byte[] table)

public static SKColorFilter CreateTable(byte[] tableA, byte[] tableR, byte[] tableG, byte[] tableB)
{
if (tableA != null && tableA.Length != TableMaxLength)
if (tableA == null)
throw new ArgumentNullException(nameof(tableA));
if (tableR == null)
throw new ArgumentNullException(nameof(tableR));
if (tableG == null)
throw new ArgumentNullException(nameof(tableG));
if (tableB == null)
throw new ArgumentNullException(nameof(tableB));
return CreateTable(tableA.AsSpan(), tableR.AsSpan(), tableG.AsSpan(), tableB.AsSpan());
}

public static SKColorFilter CreateTable(ReadOnlySpan<byte> tableA, ReadOnlySpan<byte> tableR, ReadOnlySpan<byte> tableG, ReadOnlySpan<byte> tableB)
{
if (tableA.Length != TableMaxLength)
throw new ArgumentException($"Table A must have a length of {TableMaxLength}.", nameof(tableA));
if (tableR != null && tableR.Length != TableMaxLength)
if (tableR.Length != TableMaxLength)
throw new ArgumentException($"Table R must have a length of {TableMaxLength}.", nameof(tableR));
if (tableG != null && tableG.Length != TableMaxLength)
if (tableG.Length != TableMaxLength)
throw new ArgumentException($"Table G must have a length of {TableMaxLength}.", nameof(tableG));
if (tableB != null && tableB.Length != TableMaxLength)
if (tableB.Length != TableMaxLength)
throw new ArgumentException($"Table B must have a length of {TableMaxLength}.", nameof(tableB));

fixed (byte* a = tableA)
Expand Down

0 comments on commit 5e18068

Please sign in to comment.