Decompiled source of Image Sharp v1.0.0
BepInEx/patchers/SixLabors.ImageSharp.dll
Decompiled 2 months ago
The result has been truncated due to the large size, download it to view full contents!
using System; using System.Buffers; using System.Buffers.Binary; using System.Buffers.Text; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.IO.Compression; using System.Linq; using System.Numerics; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Cryptography; using System.Security.Permissions; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml.Linq; using Microsoft.CodeAnalysis; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.ColorSpaces; using SixLabors.ImageSharp.ColorSpaces.Companding; using SixLabors.ImageSharp.ColorSpaces.Conversion; using SixLabors.ImageSharp.Common.Helpers; using SixLabors.ImageSharp.Compression.Zlib; using SixLabors.ImageSharp.Diagnostics; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Jpeg.Components; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters; using SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder; using SixLabors.ImageSharp.Formats.Pbm; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Png.Chunks; using SixLabors.ImageSharp.Formats.Png.Filters; using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.Formats.Tiff; using SixLabors.ImageSharp.Formats.Tiff.Compression; using SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors; using SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors; using SixLabors.ImageSharp.Formats.Tiff.Constants; using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Writers; using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Formats.Webp.BitReader; using SixLabors.ImageSharp.Formats.Webp.BitWriter; using SixLabors.ImageSharp.Formats.Webp.Lossless; using SixLabors.ImageSharp.Formats.Webp.Lossy; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory.Internals; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.Metadata.Profiles.Icc; using SixLabors.ImageSharp.Metadata.Profiles.Iptc; using SixLabors.ImageSharp.Metadata.Profiles.Xmp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats.PixelBlenders; using SixLabors.ImageSharp.PixelFormats.Utils; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; using SixLabors.ImageSharp.Processing.Processors.Binarization; using SixLabors.ImageSharp.Processing.Processors.Convolution; using SixLabors.ImageSharp.Processing.Processors.Convolution.Parameters; using SixLabors.ImageSharp.Processing.Processors.Dithering; using SixLabors.ImageSharp.Processing.Processors.Drawing; using SixLabors.ImageSharp.Processing.Processors.Effects; using SixLabors.ImageSharp.Processing.Processors.Filters; using SixLabors.ImageSharp.Processing.Processors.Normalization; using SixLabors.ImageSharp.Processing.Processors.Overlays; using SixLabors.ImageSharp.Processing.Processors.Quantization; using SixLabors.ImageSharp.Processing.Processors.Transforms; using SixLabors.ImageSharp.Tuples; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")] [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] [assembly: InternalsVisibleTo("ImageSharp.Benchmarks, PublicKey=00240000048000009400000006020000002400005253413100040000010001000147e6fe6766715eec6cfed61f1e7dcdbf69748a3e355c67e9d8dfd953acab1d5e012ba34b23308166fdc61ee1d0390d5f36d814a6091dd4b5ed9eda5a26afced924c683b4bfb4b3d64b0586a57eff9f02b1f84e3cb0ddd518bd1697f2c84dcbb97eb8bb5c7801be12112ed0ec86db934b0e9a5171e6bb1384b6d2f7d54dfa97")] [assembly: InternalsVisibleTo("ImageSharp.Tests.ProfilingSandbox, PublicKey=00240000048000009400000006020000002400005253413100040000010001000147e6fe6766715eec6cfed61f1e7dcdbf69748a3e355c67e9d8dfd953acab1d5e012ba34b23308166fdc61ee1d0390d5f36d814a6091dd4b5ed9eda5a26afced924c683b4bfb4b3d64b0586a57eff9f02b1f84e3cb0ddd518bd1697f2c84dcbb97eb8bb5c7801be12112ed0ec86db934b0e9a5171e6bb1384b6d2f7d54dfa97")] [assembly: InternalsVisibleTo("SixLabors.ImageSharp.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001000147e6fe6766715eec6cfed61f1e7dcdbf69748a3e355c67e9d8dfd953acab1d5e012ba34b23308166fdc61ee1d0390d5f36d814a6091dd4b5ed9eda5a26afced924c683b4bfb4b3d64b0586a57eff9f02b1f84e3cb0ddd518bd1697f2c84dcbb97eb8bb5c7801be12112ed0ec86db934b0e9a5171e6bb1384b6d2f7d54dfa97")] [assembly: InternalsVisibleTo("SixLabors.ImageSharp.Drawing.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001000147e6fe6766715eec6cfed61f1e7dcdbf69748a3e355c67e9d8dfd953acab1d5e012ba34b23308166fdc61ee1d0390d5f36d814a6091dd4b5ed9eda5a26afced924c683b4bfb4b3d64b0586a57eff9f02b1f84e3cb0ddd518bd1697f2c84dcbb97eb8bb5c7801be12112ed0ec86db934b0e9a5171e6bb1384b6d2f7d54dfa97")] [assembly: AssemblyCompany("Six Labors")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright © Six Labors")] [assembly: AssemblyDescription("A new, fully featured, fully managed, cross-platform, 2D graphics API for .NET")] [assembly: AssemblyFileVersion("2.1.12.0")] [assembly: AssemblyInformationalVersion("2.1.12+168f3f550b94d4d58c7f9662968016b0bf6cc044")] [assembly: AssemblyProduct("SixLabors.ImageSharp")] [assembly: AssemblyTitle("SixLabors.ImageSharp")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/SixLabors/ImageSharp")] [assembly: NeutralResourcesLanguage("en")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("2.0.0.0")] [assembly: TypeForwardedTo(typeof(ExcludeFromCodeCoverageAttribute))] [module: UnverifiableCode] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsUnmanagedAttribute : Attribute { } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NativeIntegerAttribute : Attribute { public readonly bool[] TransformFlags; public NativeIntegerAttribute() { TransformFlags = new bool[1] { true }; } public NativeIntegerAttribute(bool[] P_0) { TransformFlags = P_0; } } } namespace System { [ExcludeFromCodeCoverage] internal struct HashCode { private static readonly uint s_seed = GenerateGlobalSeed(); private const uint Prime1 = 2654435761u; private const uint Prime2 = 2246822519u; private const uint Prime3 = 3266489917u; private const uint Prime4 = 668265263u; private const uint Prime5 = 374761393u; private uint _v1; private uint _v2; private uint _v3; private uint _v4; private uint _queue1; private uint _queue2; private uint _queue3; private uint _length; private static uint GenerateGlobalSeed() { byte[] array = new byte[4]; using (RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create()) { randomNumberGenerator.GetBytes(array); } return BinaryPrimitives.ReadUInt32LittleEndian(array); } public static int Combine<T1>(T1 value1) { uint queuedValue = (uint)(value1?.GetHashCode() ?? 0); return (int)MixFinal(QueueRound(MixEmptyState() + 4, queuedValue)); } public static int Combine<T1, T2>(T1 value1, T2 value2) { uint queuedValue = (uint)(value1?.GetHashCode() ?? 0); uint queuedValue2 = (uint)(value2?.GetHashCode() ?? 0); return (int)MixFinal(QueueRound(QueueRound(MixEmptyState() + 8, queuedValue), queuedValue2)); } public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3) { uint queuedValue = (uint)(value1?.GetHashCode() ?? 0); uint queuedValue2 = (uint)(value2?.GetHashCode() ?? 0); uint queuedValue3 = (uint)(value3?.GetHashCode() ?? 0); return (int)MixFinal(QueueRound(QueueRound(QueueRound(MixEmptyState() + 12, queuedValue), queuedValue2), queuedValue3)); } public static int Combine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); return (int)MixFinal(MixState(v, v2, v3, v4) + 16); } public static int Combine<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); uint queuedValue = (uint)(value5?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); return (int)MixFinal(QueueRound(MixState(v, v2, v3, v4) + 20, queuedValue)); } public static int Combine<T1, T2, T3, T4, T5, T6>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); uint queuedValue = (uint)(value5?.GetHashCode() ?? 0); uint queuedValue2 = (uint)(value6?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); return (int)MixFinal(QueueRound(QueueRound(MixState(v, v2, v3, v4) + 24, queuedValue), queuedValue2)); } public static int Combine<T1, T2, T3, T4, T5, T6, T7>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); uint queuedValue = (uint)(value5?.GetHashCode() ?? 0); uint queuedValue2 = (uint)(value6?.GetHashCode() ?? 0); uint queuedValue3 = (uint)(value7?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); return (int)MixFinal(QueueRound(QueueRound(QueueRound(MixState(v, v2, v3, v4) + 28, queuedValue), queuedValue2), queuedValue3)); } public static int Combine<T1, T2, T3, T4, T5, T6, T7, T8>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); uint input5 = (uint)(value5?.GetHashCode() ?? 0); uint input6 = (uint)(value6?.GetHashCode() ?? 0); uint input7 = (uint)(value7?.GetHashCode() ?? 0); uint input8 = (uint)(value8?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); v = Round(v, input5); v2 = Round(v2, input6); v3 = Round(v3, input7); v4 = Round(v4, input8); return (int)MixFinal(MixState(v, v2, v3, v4) + 32); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint Rol(uint value, int count) { return (value << count) | (value >> 32 - count); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4) { v1 = (uint)((int)s_seed + -1640531535 + -2048144777); v2 = s_seed + 2246822519u; v3 = s_seed; v4 = s_seed - 2654435761u; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint Round(uint hash, uint input) { hash += (uint)((int)input * -2048144777); hash = Rol(hash, 13); hash *= 2654435761u; return hash; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint QueueRound(uint hash, uint queuedValue) { hash += (uint)((int)queuedValue * -1028477379); return Rol(hash, 17) * 668265263; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint MixState(uint v1, uint v2, uint v3, uint v4) { return Rol(v1, 1) + Rol(v2, 7) + Rol(v3, 12) + Rol(v4, 18); } private static uint MixEmptyState() { return s_seed + 374761393; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint MixFinal(uint hash) { hash ^= hash >> 15; hash *= 2246822519u; hash ^= hash >> 13; hash *= 3266489917u; hash ^= hash >> 16; return hash; } public void Add<T>(T value) { Add(value?.GetHashCode() ?? 0); } public void Add<T>(T value, IEqualityComparer<T> comparer) { Add(comparer?.GetHashCode(value) ?? value?.GetHashCode() ?? 0); } private void Add(int value) { uint num = _length++; switch (num % 4) { case 0u: _queue1 = (uint)value; return; case 1u: _queue2 = (uint)value; return; case 2u: _queue3 = (uint)value; return; } if (num == 3) { Initialize(out _v1, out _v2, out _v3, out _v4); } _v1 = Round(_v1, _queue1); _v2 = Round(_v2, _queue2); _v3 = Round(_v3, _queue3); _v4 = Round(_v4, (uint)value); } public int ToHashCode() { uint length = _length; uint num = length % 4; uint num2 = ((length < 4) ? MixEmptyState() : MixState(_v1, _v2, _v3, _v4)); num2 += length * 4; if (num != 0) { num2 = QueueRound(num2, _queue1); if (num > 1) { num2 = QueueRound(num2, _queue2); if (num > 2) { num2 = QueueRound(num2, _queue3); } } } return (int)MixFinal(num2); } [Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes. Use ToHashCode to retrieve the computed hash code.", true)] [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { throw new NotSupportedException("Equality not supported"); } [Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes.", true)] [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { throw new NotSupportedException("Equality not supported"); } } internal static class MathF { public const float PI = 3.1415927f; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Abs(float f) { return Math.Abs(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Atan2(float y, float x) { return (float)Math.Atan2(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Ceiling(float f) { return (float)Math.Ceiling(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Cos(float f) { return (float)Math.Cos(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Exp(float f) { return (float)Math.Exp(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Floor(float f) { return (float)Math.Floor(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Max(float val1, float val2) { return Math.Max(val1, val2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Min(float val1, float val2) { return Math.Min(val1, val2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Pow(float x, float y) { return (float)Math.Pow(x, y); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Round(float f) { return (float)Math.Round(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Round(float f, MidpointRounding mode) { return (float)Math.Round(f, mode); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Sin(float f) { return (float)Math.Sin(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Sqrt(float f) { return (float)Math.Sqrt(f); } } } namespace SixLabors { [DebuggerStepThrough] internal static class DebugGuard { [Conditional("DEBUG")] public static void IsTrue(bool target, string message) { if (!target) { throw new InvalidOperationException(message); } } [Conditional("DEBUG")] public static void NotDisposed(bool isDisposed, string objectName) { if (isDisposed) { throw new ObjectDisposedException(objectName); } } [Conditional("DEBUG")] public static void MustBeSameSized<T>(ReadOnlySpan<T> target, ReadOnlySpan<T> other, string parameterName) where T : struct { if (target.Length != other.Length) { throw new ArgumentException("Span-s must be the same size!", parameterName); } } [Conditional("DEBUG")] public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> target, ReadOnlySpan<T> minSpan, string parameterName) where T : struct { if (target.Length < minSpan.Length) { throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName); } } [Conditional("DEBUG")] public static void NotNull<TValue>(TValue value, string parameterName) where TValue : class { if (value == null) { ThrowArgumentNullException(parameterName); } } [Conditional("DEBUG")] public static void NotNullOrWhiteSpace(string value, string parameterName) { if (value == null) { ThrowArgumentNullException(parameterName); } if (string.IsNullOrWhiteSpace(value)) { ThrowArgumentException("Must not be empty or whitespace.", parameterName); } } [Conditional("DEBUG")] public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(max) >= 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}."); } } [Conditional("DEBUG")] public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(max) > 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}."); } } [Conditional("DEBUG")] public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) <= 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than {min}."); } } [Conditional("DEBUG")] public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) < 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}."); } } [Conditional("DEBUG")] public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}."); } } [Conditional("DEBUG")] public static void IsTrue(bool target, string parameterName, string message) { if (!target) { ThrowArgumentException(message, parameterName); } } [Conditional("DEBUG")] public static void IsFalse(bool target, string parameterName, string message) { if (target) { ThrowArgumentException(message, parameterName); } } [Conditional("DEBUG")] public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> source, int minLength, string parameterName) { if (source.Length < minLength) { ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName); } } [Conditional("DEBUG")] public static void MustBeSizedAtLeast<T>(Span<T> source, int minLength, string parameterName) { if (source.Length < minLength) { ThrowArgumentException($"The size must be at least {minLength}.", parameterName); } } [Conditional("DEBUG")] public static void DestinationShouldNotBeTooShort<TSource, TDest>(ReadOnlySpan<TSource> source, Span<TDest> destination, string destinationParamName) { if (destination.Length < source.Length) { ThrowArgumentException("Destination span is too short!", destinationParamName); } } [Conditional("DEBUG")] public static void DestinationShouldNotBeTooShort<TSource, TDest>(Span<TSource> source, Span<TDest> destination, string destinationParamName) { if (destination.Length < source.Length) { ThrowArgumentException("Destination span is too short!", destinationParamName); } } [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentException(string message, string parameterName) { throw new ArgumentException(message, parameterName); } [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentOutOfRangeException(string parameterName, string message) { throw new ArgumentOutOfRangeException(parameterName, message); } [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentNullException(string parameterName) { throw new ArgumentNullException(parameterName); } } [DebuggerStepThrough] internal static class Guard { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeValueType<TValue>(TValue value, string parameterName) { if (!value.GetType().IsValueType) { ThrowHelper.ThrowArgumentException("Type must be a struct.", parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void NotNull<TValue>(TValue value, string parameterName) where TValue : class { if (value == null) { ThrowHelper.ThrowArgumentNullExceptionForNotNull(parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void NotNullOrWhiteSpace(string value, string parameterName) { if (string.IsNullOrWhiteSpace(value)) { ThrowHelper.ThrowArgumentExceptionForNotNullOrWhitespace(value, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(max) >= 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(max) > 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) <= 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) < 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsTrue(bool target, string parameterName, string message) { if (!target) { ThrowHelper.ThrowArgumentException(message, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsFalse(bool target, string parameterName, string message) { if (target) { ThrowHelper.ThrowArgumentException(message, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> source, int minLength, string parameterName) { if (source.Length < minLength) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(minLength, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeSizedAtLeast<T>(Span<T> source, int minLength, string parameterName) { if (source.Length < minLength) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(minLength, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void DestinationShouldNotBeTooShort<TSource, TDest>(ReadOnlySpan<TSource> source, Span<TDest> destination, string destinationParamName) { if (destination.Length < source.Length) { ThrowHelper.ThrowArgumentException("Destination span is too short!", destinationParamName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void DestinationShouldNotBeTooShort<TSource, TDest>(Span<TSource> source, Span<TDest> destination, string destinationParamName) { if (destination.Length < source.Length) { ThrowHelper.ThrowArgumentException("Destination span is too short!", destinationParamName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(byte value, byte max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(byte value, byte max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(byte value, byte min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(byte value, byte min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(byte value, byte min, byte max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(sbyte value, sbyte max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(sbyte value, sbyte max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(sbyte value, sbyte min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(sbyte value, sbyte min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(sbyte value, sbyte min, sbyte max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(short value, short max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(short value, short max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(short value, short min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(short value, short min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(short value, short min, short max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(ushort value, ushort max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(ushort value, ushort max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(ushort value, ushort min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(ushort value, ushort min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(ushort value, ushort min, ushort max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(char value, char max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(char value, char max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(char value, char min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(char value, char min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(char value, char min, char max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(int value, int max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(int value, int max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(int value, int min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(int value, int min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(int value, int min, int max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(uint value, uint max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(uint value, uint max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(uint value, uint min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(uint value, uint min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(uint value, uint min, uint max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(float value, float max, string parameterName) { if (!(value < max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(float value, float max, string parameterName) { if (!(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(float value, float min, string parameterName) { if (!(value > min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(float value, float min, string parameterName) { if (!(value >= min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(float value, float min, float max, string parameterName) { if (!(value >= min) || !(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(long value, long max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(long value, long max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(long value, long min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(long value, long min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(long value, long min, long max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(ulong value, ulong max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(ulong value, ulong max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(ulong value, ulong min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(ulong value, ulong min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(ulong value, ulong min, ulong max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(double value, double max, string parameterName) { if (!(value < max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(double value, double max, string parameterName) { if (!(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(double value, double min, string parameterName) { if (!(value > min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(double value, double min, string parameterName) { if (!(value >= min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(double value, double min, double max, string parameterName) { if (!(value >= min) || !(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(decimal value, decimal max, string parameterName) { if (!(value < max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(decimal value, decimal max, string parameterName) { if (!(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(decimal value, decimal min, string parameterName) { if (!(value > min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(decimal value, decimal min, string parameterName) { if (!(value >= min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(decimal value, decimal min, decimal max, string parameterName) { if (!(value >= min) || !(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } } internal static class ThrowHelper { [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentNullExceptionForNotNull(string name) { ThrowArgumentNullException(name, "Parameter \"" + name + "\" must be not null."); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForNotNullOrWhitespace(string value, string name) { if (value == null) { ThrowArgumentNullException(name, "Parameter \"" + name + "\" must be not null."); } else { ThrowArgumentException(name, "Parameter \"" + name + "\" must not be empty or whitespace."); } } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeLessThan<T>(T value, T max, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be less than {max}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo<T>(T value, T maximum, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be less than or equal to {maximum}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan<T>(T value, T minimum, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be greater than {minimum}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo<T>(T value, T minimum, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be greater than or equal to {minimum}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo<T>(T value, T minimum, T maximum, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be between or equal to {minimum} and {maximum}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(int minLength, string parameterName) { ThrowArgumentException($"Spans must be at least of length {minLength}!", parameterName); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentException(string message, string name) { throw new ArgumentException(message, name); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentNullException(string name, string message) { throw new ArgumentNullException(name, message); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeException(string name, string message) { throw new ArgumentOutOfRangeException(name, message); } } } namespace SixLabors.ImageSharp { public readonly struct Color : IEquatable<Color> { private readonly Rgba64 data; private readonly IPixel boxedHighPrecisionPixel; private static readonly Lazy<Dictionary<string, Color>> NamedColorsLookupLazy = new Lazy<Dictionary<string, Color>>(CreateNamedColorsLookup, isThreadSafe: true); public static readonly Color AliceBlue = FromRgba(240, 248, byte.MaxValue, byte.MaxValue); public static readonly Color AntiqueWhite = FromRgba(250, 235, 215, byte.MaxValue); public static readonly Color Aqua = FromRgba(0, byte.MaxValue, byte.MaxValue, byte.MaxValue); public static readonly Color Aquamarine = FromRgba(127, byte.MaxValue, 212, byte.MaxValue); public static readonly Color Azure = FromRgba(240, byte.MaxValue, byte.MaxValue, byte.MaxValue); public static readonly Color Beige = FromRgba(245, 245, 220, byte.MaxValue); public static readonly Color Bisque = FromRgba(byte.MaxValue, 228, 196, byte.MaxValue); public static readonly Color Black = FromRgba(0, 0, 0, byte.MaxValue); public static readonly Color BlanchedAlmond = FromRgba(byte.MaxValue, 235, 205, byte.MaxValue); public static readonly Color Blue = FromRgba(0, 0, byte.MaxValue, byte.MaxValue); public static readonly Color BlueViolet = FromRgba(138, 43, 226, byte.MaxValue); public static readonly Color Brown = FromRgba(165, 42, 42, byte.MaxValue); public static readonly Color BurlyWood = FromRgba(222, 184, 135, byte.MaxValue); public static readonly Color CadetBlue = FromRgba(95, 158, 160, byte.MaxValue); public static readonly Color Chartreuse = FromRgba(127, byte.MaxValue, 0, byte.MaxValue); public static readonly Color Chocolate = FromRgba(210, 105, 30, byte.MaxValue); public static readonly Color Coral = FromRgba(byte.MaxValue, 127, 80, byte.MaxValue); public static readonly Color CornflowerBlue = FromRgba(100, 149, 237, byte.MaxValue); public static readonly Color Cornsilk = FromRgba(byte.MaxValue, 248, 220, byte.MaxValue); public static readonly Color Crimson = FromRgba(220, 20, 60, byte.MaxValue); public static readonly Color Cyan = Aqua; public static readonly Color DarkBlue = FromRgba(0, 0, 139, byte.MaxValue); public static readonly Color DarkCyan = FromRgba(0, 139, 139, byte.MaxValue); public static readonly Color DarkGoldenrod = FromRgba(184, 134, 11, byte.MaxValue); public static readonly Color DarkGray = FromRgba(169, 169, 169, byte.MaxValue); public static readonly Color DarkGreen = FromRgba(0, 100, 0, byte.MaxValue); public static readonly Color DarkGrey = DarkGray; public static readonly Color DarkKhaki = FromRgba(189, 183, 107, byte.MaxValue); public static readonly Color DarkMagenta = FromRgba(139, 0, 139, byte.MaxValue); public static readonly Color DarkOliveGreen = FromRgba(85, 107, 47, byte.MaxValue); public static readonly Color DarkOrange = FromRgba(byte.MaxValue, 140, 0, byte.MaxValue); public static readonly Color DarkOrchid = FromRgba(153, 50, 204, byte.MaxValue); public static readonly Color DarkRed = FromRgba(139, 0, 0, byte.MaxValue); public static readonly Color DarkSalmon = FromRgba(233, 150, 122, byte.MaxValue); public static readonly Color DarkSeaGreen = FromRgba(143, 188, 143, byte.MaxValue); public static readonly Color DarkSlateBlue = FromRgba(72, 61, 139, byte.MaxValue); public static readonly Color DarkSlateGray = FromRgba(47, 79, 79, byte.MaxValue); public static readonly Color DarkSlateGrey = DarkSlateGray; public static readonly Color DarkTurquoise = FromRgba(0, 206, 209, byte.MaxValue); public static readonly Color DarkViolet = FromRgba(148, 0, 211, byte.MaxValue); public static readonly Color DeepPink = FromRgba(byte.MaxValue, 20, 147, byte.MaxValue); public static readonly Color DeepSkyBlue = FromRgba(0, 191, byte.MaxValue, byte.MaxValue); public static readonly Color DimGray = FromRgba(105, 105, 105, byte.MaxValue); public static readonly Color DimGrey = DimGray; public static readonly Color DodgerBlue = FromRgba(30, 144, byte.MaxValue, byte.MaxValue); public static readonly Color Firebrick = FromRgba(178, 34, 34, byte.MaxValue); public static readonly Color FloralWhite = FromRgba(byte.MaxValue, 250, 240, byte.MaxValue); public static readonly Color ForestGreen = FromRgba(34, 139, 34, byte.MaxValue); public static readonly Color Fuchsia = FromRgba(byte.MaxValue, 0, byte.MaxValue, byte.MaxValue); public static readonly Color Gainsboro = FromRgba(220, 220, 220, byte.MaxValue); public static readonly Color GhostWhite = FromRgba(248, 248, byte.MaxValue, byte.MaxValue); public static readonly Color Gold = FromRgba(byte.MaxValue, 215, 0, byte.MaxValue); public static readonly Color Goldenrod = FromRgba(218, 165, 32, byte.MaxValue); public static readonly Color Gray = FromRgba(128, 128, 128, byte.MaxValue); public static readonly Color Green = FromRgba(0, 128, 0, byte.MaxValue); public static readonly Color GreenYellow = FromRgba(173, byte.MaxValue, 47, byte.MaxValue); public static readonly Color Grey = Gray; public static readonly Color Honeydew = FromRgba(240, byte.MaxValue, 240, byte.MaxValue); public static readonly Color HotPink = FromRgba(byte.MaxValue, 105, 180, byte.MaxValue); public static readonly Color IndianRed = FromRgba(205, 92, 92, byte.MaxValue); public static readonly Color Indigo = FromRgba(75, 0, 130, byte.MaxValue); public static readonly Color Ivory = FromRgba(byte.MaxValue, byte.MaxValue, 240, byte.MaxValue); public static readonly Color Khaki = FromRgba(240, 230, 140, byte.MaxValue); public static readonly Color Lavender = FromRgba(230, 230, 250, byte.MaxValue); public static readonly Color LavenderBlush = FromRgba(byte.MaxValue, 240, 245, byte.MaxValue); public static readonly Color LawnGreen = FromRgba(124, 252, 0, byte.MaxValue); public static readonly Color LemonChiffon = FromRgba(byte.MaxValue, 250, 205, byte.MaxValue); public static readonly Color LightBlue = FromRgba(173, 216, 230, byte.MaxValue); public static readonly Color LightCoral = FromRgba(240, 128, 128, byte.MaxValue); public static readonly Color LightCyan = FromRgba(224, byte.MaxValue, byte.MaxValue, byte.MaxValue); public static readonly Color LightGoldenrodYellow = FromRgba(250, 250, 210, byte.MaxValue); public static readonly Color LightGray = FromRgba(211, 211, 211, byte.MaxValue); public static readonly Color LightGreen = FromRgba(144, 238, 144, byte.MaxValue); public static readonly Color LightGrey = LightGray; public static readonly Color LightPink = FromRgba(byte.MaxValue, 182, 193, byte.MaxValue); public static readonly Color LightSalmon = FromRgba(byte.MaxValue, 160, 122, byte.MaxValue); public static readonly Color LightSeaGreen = FromRgba(32, 178, 170, byte.MaxValue); public static readonly Color LightSkyBlue = FromRgba(135, 206, 250, byte.MaxValue); public static readonly Color LightSlateGray = FromRgba(119, 136, 153, byte.MaxValue); public static readonly Color LightSlateGrey = LightSlateGray; public static readonly Color LightSteelBlue = FromRgba(176, 196, 222, byte.MaxValue); public static readonly Color LightYellow = FromRgba(byte.MaxValue, byte.MaxValue, 224, byte.MaxValue); public static readonly Color Lime = FromRgba(0, byte.MaxValue, 0, byte.MaxValue); public static readonly Color LimeGreen = FromRgba(50, 205, 50, byte.MaxValue); public static readonly Color Linen = FromRgba(250, 240, 230, byte.MaxValue); public static readonly Color Magenta = Fuchsia; public static readonly Color Maroon = FromRgba(128, 0, 0, byte.MaxValue); public static readonly Color MediumAquamarine = FromRgba(102, 205, 170, byte.MaxValue); public static readonly Color MediumBlue = FromRgba(0, 0, 205, byte.MaxValue); public static readonly Color MediumOrchid = FromRgba(186, 85, 211, byte.MaxValue); public static readonly Color MediumPurple = FromRgba(147, 112, 219, byte.MaxValue); public static readonly Color MediumSeaGreen = FromRgba(60, 179, 113, byte.MaxValue); public static readonly Color MediumSlateBlue = FromRgba(123, 104, 238, byte.MaxValue); public static readonly Color MediumSpringGreen = FromRgba(0, 250, 154, byte.MaxValue); public static readonly Color MediumTurquoise = FromRgba(72, 209, 204, byte.MaxValue); public static readonly Color MediumVioletRed = FromRgba(199, 21, 133, byte.MaxValue); public static readonly Color MidnightBlue = FromRgba(25, 25, 112, byte.MaxValue); public static readonly Color MintCream = FromRgba(245, byte.MaxValue, 250, byte.MaxValue); public static readonly Color MistyRose = FromRgba(byte.MaxValue, 228, 225, byte.MaxValue); public static readonly Color Moccasin = FromRgba(byte.MaxValue, 228, 181, byte.MaxValue); public static readonly Color NavajoWhite = FromRgba(byte.MaxValue, 222, 173, byte.MaxValue); public static readonly Color Navy = FromRgba(0, 0, 128, byte.MaxValue); public static readonly Color OldLace = FromRgba(253, 245, 230, byte.MaxValue); public static readonly Color Olive = FromRgba(128, 128, 0, byte.MaxValue); public static readonly Color OliveDrab = FromRgba(107, 142, 35, byte.MaxValue); public static readonly Color Orange = FromRgba(byte.MaxValue, 165, 0, byte.MaxValue); public static readonly Color OrangeRed = FromRgba(byte.MaxValue, 69, 0, byte.MaxValue); public static readonly Color Orchid = FromRgba(218, 112, 214, byte.MaxValue); public static readonly Color PaleGoldenrod = FromRgba(238, 232, 170, byte.MaxValue); public static readonly Color PaleGreen = FromRgba(152, 251, 152, byte.MaxValue); public static readonly Color PaleTurquoise = FromRgba(175, 238, 238, byte.MaxValue); public static readonly Color PaleVioletRed = FromRgba(219, 112, 147, byte.MaxValue); public static readonly Color PapayaWhip = FromRgba(byte.MaxValue, 239, 213, byte.MaxValue); public static readonly Color PeachPuff = FromRgba(byte.MaxValue, 218, 185, byte.MaxValue); public static readonly Color Peru = FromRgba(205, 133, 63, byte.MaxValue); public static readonly Color Pink = FromRgba(byte.MaxValue, 192, 203, byte.MaxValue); public static readonly Color Plum = FromRgba(221, 160, 221, byte.MaxValue); public static readonly Color PowderBlue = FromRgba(176, 224, 230, byte.MaxValue); public static readonly Color Purple = FromRgba(128, 0, 128, byte.MaxValue); public static readonly Color RebeccaPurple = FromRgba(102, 51, 153, byte.MaxValue); public static readonly Color Red = FromRgba(byte.MaxValue, 0, 0, byte.MaxValue); public static readonly Color RosyBrown = FromRgba(188, 143, 143, byte.MaxValue); public static readonly Color RoyalBlue = FromRgba(65, 105, 225, byte.MaxValue); public static readonly Color SaddleBrown = FromRgba(139, 69, 19, byte.MaxValue); public static readonly Color Salmon = FromRgba(250, 128, 114, byte.MaxValue); public static readonly Color SandyBrown = FromRgba(244, 164, 96, byte.MaxValue); public static readonly Color SeaGreen = FromRgba(46, 139, 87, byte.MaxValue); public static readonly Color SeaShell = FromRgba(byte.MaxValue, 245, 238, byte.MaxValue); public static readonly Color Sienna = FromRgba(160, 82, 45, byte.MaxValue); public static readonly Color Silver = FromRgba(192, 192, 192, byte.MaxValue); public static readonly Color SkyBlue = FromRgba(135, 206, 235, byte.MaxValue); public static readonly Color SlateBlue = FromRgba(106, 90, 205, byte.MaxValue); public static readonly Color SlateGray = FromRgba(112, 128, 144, byte.MaxValue); public static readonly Color SlateGrey = SlateGray; public static readonly Color Snow = FromRgba(byte.MaxValue, 250, 250, byte.MaxValue); public static readonly Color SpringGreen = FromRgba(0, byte.MaxValue, 127, byte.MaxValue); public static readonly Color SteelBlue = FromRgba(70, 130, 180, byte.MaxValue); public static readonly Color Tan = FromRgba(210, 180, 140, byte.MaxValue); public static readonly Color Teal = FromRgba(0, 128, 128, byte.MaxValue); public static readonly Color Thistle = FromRgba(216, 191, 216, byte.MaxValue); public static readonly Color Tomato = FromRgba(byte.MaxValue, 99, 71, byte.MaxValue); public static readonly Color Transparent = FromRgba(0, 0, 0, 0); public static readonly Color Turquoise = FromRgba(64, 224, 208, byte.MaxValue); public static readonly Color Violet = FromRgba(238, 130, 238, byte.MaxValue); public static readonly Color Wheat = FromRgba(245, 222, 179, byte.MaxValue); public static readonly Color White = FromRgba(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); public static readonly Color WhiteSmoke = FromRgba(245, 245, 245, byte.MaxValue); public static readonly Color Yellow = FromRgba(byte.MaxValue, byte.MaxValue, 0, byte.MaxValue); public static readonly Color YellowGreen = FromRgba(154, 205, 50, byte.MaxValue); private static readonly Lazy<Color[]> WebSafePaletteLazy = new Lazy<Color[]>(CreateWebSafePalette, isThreadSafe: true); private static readonly Lazy<Color[]> WernerPaletteLazy = new Lazy<Color[]>(CreateWernerPalette, isThreadSafe: true); public static ReadOnlyMemory<Color> WebSafePalette => WebSafePaletteLazy.Value; public static ReadOnlyMemory<Color> WernerPalette => WernerPaletteLazy.Value; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Rgba64 pixel) { data = pixel; boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Rgb48 pixel) { data = new Rgba64(pixel.R, pixel.G, pixel.B, ushort.MaxValue); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(La32 pixel) { data = new Rgba64(pixel.L, pixel.L, pixel.L, pixel.A); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(L16 pixel) { data = new Rgba64(pixel.PackedValue, pixel.PackedValue, pixel.PackedValue, ushort.MaxValue); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Rgba32 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Argb32 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Bgra32 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Abgr32 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Rgb24 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Bgr24 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Vector4 vector) { vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One); boxedHighPrecisionPixel = new RgbaVector(vector.X, vector.Y, vector.Z, vector.W); data = default(Rgba64); } public static explicit operator Vector4(Color color) { return color.ToVector4(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator Color(Vector4 source) { return new Color(source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Rgba32 ToRgba32() { if (boxedHighPrecisionPixel == null) { return data.ToRgba32(); } Rgba32 dest = default(Rgba32); boxedHighPrecisionPixel.ToRgba32(ref dest); return dest; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Bgra32 ToBgra32() { if (boxedHighPrecisionPixel == null) { return data.ToBgra32(); } Bgra32 result = default(Bgra32); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Argb32 ToArgb32() { if (boxedHighPrecisionPixel == null) { return data.ToArgb32(); } Argb32 result = default(Argb32); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Abgr32 ToAbgr32() { if (boxedHighPrecisionPixel == null) { return data.ToAbgr32(); } Abgr32 result = default(Abgr32); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Rgb24 ToRgb24() { if (boxedHighPrecisionPixel == null) { return data.ToRgb24(); } Rgb24 result = default(Rgb24); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Bgr24 ToBgr24() { if (boxedHighPrecisionPixel == null) { return data.ToBgr24(); } Bgr24 result = default(Bgr24); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Vector4 ToVector4() { if (boxedHighPrecisionPixel == null) { return data.ToScaledVector4(); } return boxedHighPrecisionPixel.ToScaledVector4(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private Color(byte r, byte g, byte b, byte a) { data = new Rgba64(ColorNumerics.UpscaleFrom8BitTo16Bit(r), ColorNumerics.UpscaleFrom8BitTo16Bit(g), ColorNumerics.UpscaleFrom8BitTo16Bit(b), ColorNumerics.UpscaleFrom8BitTo16Bit(a)); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private Color(byte r, byte g, byte b) { data = new Rgba64(ColorNumerics.UpscaleFrom8BitTo16Bit(r), ColorNumerics.UpscaleFrom8BitTo16Bit(g), ColorNumerics.UpscaleFrom8BitTo16Bit(b), ushort.MaxValue); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private Color(IPixel pixel) { boxedHighPrecisionPixel = pixel; data = default(Rgba64); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Color left, Color right) { return left.Equals(right); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(Color left, Color right) { return !left.Equals(right); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Color FromRgba(byte r, byte g, byte b, byte a) { return new Color(r, g, b, a); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Color FromRgb(byte r, byte g, byte b) { return new Color(r, g, b); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Color FromPixel<TPixel>(TPixel pixel) where TPixel : unmanaged, IPixel<TPixel> { if (typeof(TPixel) == typeof(Rgba64)) { return new Color((Rgba64)(object)pixel); } if (typeof(TPixel) == typeof(Rgb48)) { return new Color((Rgb48)(object)pixel); } if (typeof(TPixel) == typeof(La32)) { return new Color((La32)(object)pixel); } if (typeof(TPixel) == typeof(L16)) { return new Color((L16)(object)pixel); } if (System.Runtime.CompilerServices.Unsafe.SizeOf<TPixel>() <= System.Runtime.CompilerServices.Unsafe.SizeOf<Rgba32>()) { Rgba32 dest = default(Rgba32); pixel.ToRgba32(ref dest); return new Color(dest); } return new Color(pixel); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Color ParseHex(string hex) { return new Color(Rgba32.ParseHex(hex)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryParseHex(string hex, out Color result) { result = default(Color); if (Rgba32.TryParseHex(hex, out var result2)) { result = new Color(result2); return true; } return false; } public static Color Parse(string input) { Guard.NotNull(input, "input"); if (!TryParse(input, out var result)) { throw new ArgumentException("Input string is not in the correct format.", "input"); } return result; } public static bool TryParse(string input, out Color result) { result = default(Color); if (string.IsNullOrWhiteSpace(input)) { return false; } if (NamedColorsLookupLazy.Value.TryGetValue(input, out result)) { return true; } return TryParseHex(input, out result); } public Color WithAlpha(float alpha) { Vector4 vector = (Vector4)this; vector.W = alpha; return new Color(vector); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ToHex() { return data.ToRgba32().ToHex(); } public override string ToString() { return ToHex(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public TPixel ToPixel<TPixel>() where TPixel : unmanaged, IPixel<TPixel> { IPixel pixel = boxedHighPrecisionPixel; if (pixel is TPixel) { return (TPixel)pixel; } TPixel result; if (boxedHighPrecisionPixel == null) { result = default(TPixel); result.FromRgba64(data); return result; } result = default(TPixel); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ToPixel<TPixel>(Configuration configuration, ReadOnlySpan<Color> source, Span<TPixel> destination) where TPixel : unmanaged, IPixel<TPixel> { Guard.DestinationShouldNotBeTooShort(source, destination, "destination"); for (int i = 0; i < source.Length; i++) { destination[i] = source[i].ToPixel<TPixel>(); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Color other) { if (boxedHighPrecisionPixel == null && other.boxedHighPrecisionPixel == null) { return data.PackedValue == other.data.PackedValue; } return boxedHighPrecisionPixel?.Equals(other.boxedHighPrecisionPixel) ?? false; } public override bool Equals(object obj) { if (obj is Color other) { return Equals(other); } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() { if (boxedHighPrecisionPixel == null) { return data.PackedValue.GetHashCode(); } return boxedHighPrecisionPixel.GetHashCode(); } private static Dictionary<string, Color> CreateNamedColorsLookup() { return new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase) { { "AliceBlue", AliceBlue }, { "AntiqueWhite", AntiqueWhite }, { "Aqua", Aqua }, { "Aquamarine", Aquamarine }, { "Azure", Azure }, { "Beige", Beige }, { "Bisque", Bisque }, { "Black", Black }, { "BlanchedAlmond", BlanchedAlmond }, { "Blue", Blue }, { "BlueViolet", BlueViolet }, { "Brown", Brown }, { "BurlyWood", BurlyWood }, { "CadetBlue", CadetBlue }, { "Chartreuse", Chartreuse }, { "Chocolate", Chocolate }, { "Coral", Coral }, { "CornflowerBlue", CornflowerBlue }, { "Cornsilk", Cornsilk }, { "Crimson", Crimson }, { "Cyan", Cyan }, { "DarkBlue", DarkBlue }, { "DarkCyan", DarkCyan }, { "DarkGoldenrod", DarkGoldenrod }, { "DarkGray", DarkGray }, { "DarkGreen", DarkGreen }, { "DarkGrey", DarkGrey }, { "DarkKhaki", DarkKhaki }, { "DarkMagenta", DarkMagenta }, { "DarkOliveGreen", DarkOliveGreen }, { "DarkOrange", DarkOrange }, { "DarkOrchid", DarkOrchid }, { "DarkRed", DarkRed }, { "DarkSalmon", DarkSalmon }, { "DarkSeaGreen", DarkSeaGreen }, { "DarkSlateBlue", DarkSlateBlue }, { "DarkSlateGray", DarkSlateGray }, { "DarkSlateGrey", DarkSlateGrey }, { "DarkTurquoise", DarkTurquoise }, { "DarkViolet", DarkViolet }, { "DeepPink", DeepPink }, { "DeepSkyBlue", DeepSkyBlue }, { "DimGray", DimGray }, { "DimGrey", DimGrey }, { "DodgerBlue", DodgerBlue }, { "Firebrick", Firebrick }, { "FloralWhite", FloralWhite }, { "ForestGreen", ForestGreen }, { "Fuchsia", Fuchsia }, { "Gainsboro", Gainsboro }, { "GhostWhite", GhostWhite }, { "Gold", Gold }, { "Goldenrod", Goldenrod }, { "Gray", Gray }, { "Green", Green }, { "GreenYellow", GreenYellow }, { "Grey", Grey }, { "Honeydew", Honeydew }, { "HotPink", HotPink }, { "IndianRed", IndianRed }, { "Indigo", Indigo }, { "Ivory", Ivory }, { "Khaki", Khaki }, { "Lavender", Lavender }, { "LavenderBlush", LavenderBlush }, { "LawnGreen", LawnGreen }, { "LemonChiffon", LemonChiffon }, { "LightBlue", LightBlue }, { "LightCoral", LightCoral }, { "LightCyan", LightCyan }, { "LightGoldenrodYellow", LightGoldenrodYellow }, { "LightGray", LightGray }, { "LightGreen", LightGreen }, { "LightGrey", LightGrey }, { "LightPink", LightPink }, { "LightSalmon", LightSalmon }, { "LightSeaGreen", LightSeaGreen }, { "LightSkyBlue", LightSkyBlue }, { "LightSlateGray", LightSlateGray }, { "LightSlateGrey", LightSlateGrey }, { "LightSteelBlue", LightSteelBlue }, { "LightYellow", LightYellow }, { "Lime", Lime }, { "LimeGreen", LimeGreen }, { "Linen", Linen }, { "Magenta", Magenta }, { "Maroon", Maroon }, { "MediumAquamarine", MediumAquamarine }, { "MediumBlue", MediumBlue }, { "MediumOrchid", MediumOrchid }, { "MediumPurple", MediumPurple }, { "MediumSeaGreen", MediumSeaGreen }, { "MediumSlateBlue", MediumSlateBlue }, { "MediumSpringGreen", MediumSpringGreen }, { "MediumTurquoise", MediumTurquoise }, { "MediumVioletRed", MediumVioletRed }, { "MidnightBlue", MidnightBlue }, { "MintCream", MintCream }, { "MistyRose", MistyRose }, { "Moccasin", Moccasin }, { "NavajoWhite", NavajoWhite }, { "Navy", Navy }, { "OldLace", OldLace }, { "Olive", Olive }, { "OliveDrab", OliveDrab }, { "Orange", Orange }, { "OrangeRed", OrangeRed }, { "Orchid", Orchid }, { "PaleGoldenrod", PaleGoldenrod }, { "PaleGreen", PaleGreen }, { "PaleTurquoise", PaleTurquoise }, { "PaleVioletRed", PaleVioletRed }, { "PapayaWhip", PapayaWhip }, { "PeachPuff", PeachPuff }, { "Peru", Peru }, { "Pink", Pink }, { "Plum", Plum }, { "PowderBlue", PowderBlue }, { "Purple", Purple }, { "RebeccaPurple", RebeccaPurple }, { "Red", Red }, { "RosyBrown", RosyBrown }, { "RoyalBlue", RoyalBlue }, { "SaddleBrown", SaddleBrown }, { "Salmon", Salmon }, { "SandyBrown", SandyBrown }, { "SeaGreen", SeaGreen }, { "SeaShell", SeaShell }, { "Sienna", Sienna }, { "Silver", Silver }, { "SkyBlue", SkyBlue }, { "SlateBlue", SlateBlue }, { "SlateGray", SlateGray }, { "SlateGrey", SlateGrey }, { "Snow", Snow }, { "SpringGreen", SpringGreen }, { "SteelBlue", SteelBlue }, { "Tan", Tan }, { "Teal", Teal }, { "Thistle", Thistle }, { "Tomato", Tomato }, { "Transparent", Transparent }, { "Turquoise", Turquoise }, { "Violet", Violet }, { "Wheat", Wheat }, { "White", White }, { "WhiteSmoke", WhiteSmoke }, { "Yellow", Yellow }, { "YellowGreen", YellowGreen } }; } private static Color[] CreateWebSafePalette() { return new Color[142] { AliceBlue, AntiqueWhite, Aqua, Aquamarine, Azure, Beige, Bisque, Black, BlanchedAlmond, Blue, BlueViolet, Brown, BurlyWood, CadetBlue, Chartreuse, Chocolate, Coral, CornflowerBlue, Cornsilk, Crimson, Cyan, DarkBlue, DarkCyan, DarkGoldenrod, DarkGray, DarkGreen, DarkKhaki, DarkMagenta, DarkOliveGreen, DarkOrange, DarkOrchid, DarkRed, DarkSalmon, DarkSeaGreen, DarkSlateBlue, DarkSlateGray, DarkTurquoise, DarkViolet, DeepPink, DeepSkyBlue, DimGray, DodgerBlue, Firebrick, FloralWhite, ForestGreen, Fuchsia, Gainsboro, GhostWhite, Gold, Goldenrod, Gray, Green, GreenYellow, Honeydew, HotPink, IndianRed, Indigo, Ivory, Khaki, Lavender, LavenderBlush, LawnGreen, LemonChiffon, LightBlue, LightCoral, LightCyan, LightGoldenrodYellow, LightGray, LightGreen, LightPink, LightSalmon, LightSeaGreen, LightSkyBlue, LightSlateGray, LightSteelBlue, LightYellow, Lime, LimeGreen, Linen, Magenta, Maroon, MediumAquamarine, MediumBlue, MediumOrchid, MediumPurple, MediumSeaGreen, MediumSlateBlue, MediumSpringGreen, MediumTurquoise, MediumVioletRed, MidnightBlue, MintCream, MistyRose, Moccasin, NavajoWhite, Navy, OldLace, Olive, OliveDrab, Orange, OrangeRed, Orchid, PaleGoldenrod, PaleGreen, PaleTurquoise, PaleVioletRed, PapayaWhip, PeachPuff, Peru, Pink, Plum, PowderBlue, Purple, RebeccaPurple, Red, RosyBrown, RoyalBlue, SaddleBrown, Salmon, SandyBrown, SeaGreen, SeaShell, Sienna, Silver, SkyBlue, SlateBlue, SlateGray, Snow, SpringGreen, SteelBlue, Tan, Teal, Thistle, Tomato, Transparent, Turquoise, Violet, Wheat, White, WhiteSmoke, Yellow, YellowGreen }; } private static Color[] CreateWernerPalette() { return new Color[110] { ParseHex("#f1e9cd"), ParseHex("#f2e7cf"), ParseHex("#ece6d0"), ParseHex("#f2eacc"), ParseHex("#f3e9ca"), ParseHex("#f2ebcd"), ParseHex("#e6e1c9"), ParseHex("#e2ddc6"), ParseHex("#cbc8b7"), ParseHex("#bfbbb0"), ParseHex("#bebeb3"), ParseHex("#b7b5ac"), ParseHex("#bab191"), ParseHex("#9c9d9a"), ParseHex("#8a8d84"), ParseHex("#5b5c61"), ParseHex("#555152"), ParseHex("#413f44"), ParseHex("#454445"), ParseHex("#423937"), ParseHex("#433635"), ParseHex("#252024"), ParseHex("#241f20"), ParseHex("#281f3f"), ParseHex("#1c1949"), ParseHex("#4f638d"), ParseHex("#383867"), ParseHex("#5c6b8f"), ParseHex("#657abb"), ParseHex("#6f88af"), ParseHex("#7994b5"), ParseHex("#6fb5a8"), ParseHex("#719ba2"), ParseHex("#8aa1a6"), ParseHex("#d0d5d3"), ParseHex("#8590ae"), ParseHex("#3a2f52"), ParseHex("#39334a"), ParseHex("#6c6d94"), ParseHex("#584c77"), ParseHex("#533552"), ParseHex("#463759"), ParseHex("#bfbac0"), ParseHex("#77747f"), ParseHex("#4a475c"), ParseHex("#b8bfaf"), ParseHex("#b2b599"), ParseHex("#979c84"), ParseHex("#5d6161"), ParseHex("#61ac86"), ParseHex("#a4b6a7"), ParseHex("#adba98"), ParseHex("#93b778"), ParseHex("#7d8c55"), ParseHex("#33431e"), ParseHex("#7c8635"), ParseHex("#8e9849"), ParseHex("#c2c190"), ParseHex("#67765b"), ParseHex("#ab924b"), ParseHex("#c8c76f"), ParseHex("#ccc050"), ParseHex("#ebdd99"), ParseHex("#ab9649"), ParseHex("#dbc364"), ParseHex("#e6d058"), ParseHex("#ead665"), ParseHex("#d09b2c"), ParseHex("#a36629"), ParseHex("#a77d35"), ParseHex("#f0d696"), ParseHex("#d7c485"), ParseHex("#f1d28c"), ParseHex("#efcc83"), ParseHex("#f3daa7"), ParseHex("#dfa837"), ParseHex("#ebbc71"), ParseHex("#d17c3f"), ParseHex("#92462f"), ParseHex("#be7249"), ParseHex("#bb603c"), ParseHex("#c76b4a"), ParseHex("#a75536"), ParseHex("#b63e36"), ParseHex("#b5493a"), ParseHex("#cd6d57"), ParseHex("#711518"), ParseHex("#e9c49d"), ParseHex("#eedac3"), ParseHex("#eecfbf"), ParseHex("#ce536b"), ParseHex("#b74a70"), ParseHex("#b7757c"), ParseHex("#612741"), ParseHex("#7a4848"), ParseHex("#3f3033"), ParseHex("#8d746f"), ParseHex("#4d3635"), ParseHex("#6e3b31"), ParseHex("#864735"), ParseHex("#553d3a"), ParseHex("#613936"), ParseHex("#7a4b3a"), ParseHex("#946943"), ParseHex("#c39e6d"), ParseHex("#513e32"), ParseHex("#8b7859"), ParseHex("#9b856b"), ParseHex("#766051"), ParseHex("#453b32") }; } } public enum ByteOrder { BigEndian, LittleEndian } internal static class Constants { public static readonly float Epsilon = 0.001f; public static readonly float EpsilonSquared = Epsilon * Epsilon; } public class ImageFormatException : Exception { internal ImageFormatException(string errorMessage) : base(errorMessage) { } internal ImageFormatException(string errorMessage, Exception innerException) : base(errorMessage, innerException) { } } public sealed class ImageProcessingException : Exception { public ImageProcessingException() { } public ImageProcessingException(string errorMessage) : base(errorMessage) { } public ImageProcessingException(string errorMessage, Exception innerException) : base(errorMessage, innerException) { } } public sealed class InvalidImageContentException : ImageFormatException { public InvalidImageContentException(string errorMessage) : base(errorMessage) { } public InvalidImageContentException(string errorMessage, Exception innerException) : base(errorMessage, innerException) { } internal InvalidImageContentException(Size size, InvalidMemoryOperationException memoryException) : this($"Cannot decode image. Failed to allocate buffers for possibly degenerate dimensions: {size.Width}x{size.Height}.", memoryException) { } } public sealed class UnknownImageFormatException : ImageFormatException { public UnknownImageFormatException(string errorMessage) : base(errorMessage) { } } internal static class ConfigurationExtensions { public static ParallelOptions GetParallelOptions(this Configuration configuration) { return new ParallelOptions { MaxDegreeOfParallelism = configuration.MaxDegreeOfParallelism }; } } internal static class EncoderExtensions { public unsafe static string GetString(this Encoding encoding, ReadOnlySpan<byte> buffer) { if (buffer.Length == 0) { return string.Empty; } fixed (byte* bytes = buffer) { return encoding.GetString(bytes, buffer.Length); } } } internal static class EnumerableExtensions { [CompilerGenerated] private sealed class <RangeIterator>d__1 : IEnumerable<int>, IEnumerable, IEnumerator<int>, IDisposable, IEnumerator { private int <>1__state; private int <>2__current; private int <>l__initialThreadId; private int fromInclusive; public int <>3__fromInclusive; private int step; public int <>3__step; private Func<int, bool> toDelegate; public Func<int, bool> <>3__toDelegate; private int <i>5__2; int IEnumerator<int>.Current { [DebuggerHidden] get { return <>2__current; } } object IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } [DebuggerHidden] public <RangeIterator>d__1(int <>1__state) { this.<>1__state = <>1__state; <>l__initialThreadId = Environment.CurrentManagedThreadId; } [DebuggerHidden] void IDisposable.Dispose() { <>1__state = -2; } private bool MoveNext() { switch (<>1__state) { default: return false; case 0: <>1__state = -1; <i>5__2 = fromInclusive; break; case 1: <>1__state = -1; <i>5__2 += step; break; } if (toDelegate(<i>5__2)) { <>2__current = <i>5__2; <>1__state = 1; return true; } return false; } bool IEnumerator.MoveNext() { //ILSpy generated this explicit interface implementation from .override directive in MoveNext return this.MoveNext(); } [DebuggerHidden] void IEnumerator.Reset() { throw new NotSupportedException(); } [DebuggerHidden] IEnumerator<int> IEnumerable<int>.GetEnumerator() { <RangeIterator>d__1 <RangeIterator>d__; if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId) { <>1__state = 0; <RangeIterator>d__ = this; } else { <RangeIterator>d__ = new <RangeIterator>d__1(0); } <RangeIterator>d__.fromInclusive = <>3__fromInclusive; <RangeIterator>d__.toDelegate = <>3__toDelegate; <RangeIterator>d__.step = <>3__step; return <RangeIterator>d__; } [DebuggerHidden] IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable<int>)this).GetEnumerator(); } } public static IEnumerable<int> SteppedRange(int fromInclusive, Func<int, bool> toDelegate, int step) { return RangeIterator(fromInclusive, toDelegate, step); } [IteratorStateMachine(typeof(<RangeIterator>d__1))] private static IEnumerable<int> RangeIterator(int fromInclusive, Func<int, bool> toDelegate, int step) { //yield-return decompiler failed: Unexpected instruction in Iterator.Dispose() return new <RangeIterator>d__1(-2) { <>3__fromInclusive = fromInclusive, <>3__toDelegate = toDelegate, <>3__step = step }; } } internal static class StreamExtensions { public static void Write(this Stream stream, Span<byte> buffer, int offset, int count) { Write(stream, buffer.Slice(offset, count)); } public static int Read(this Stream stream, Span<byte> buffer, int offset, int count) { return Read(stream, buffer.Slice(offset, count)); } public static void Skip(this Stream stream, int count) { if (count < 1) { return; } if (stream.CanSeek) { stream.Seek(count, SeekOrigin.Current); return; } byte[] array = ArrayPool<byte>.Shared.Rent(count); try { while (count > 0) { int num = stream.Read(array, 0, count); if (num == 0) { break; } count -= num; } } finally { ArrayPool<byte>.Shared.Return(array); } } public static int Read(this Stream stream, Span<byte> buffer) { byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length); try { int num = stream.Read(array, 0, buffer.Length); if ((uint)num > (uint)buffer.Length) { throw new IOException("Stream was too long."); } new Span<byte>(array, 0, num).CopyTo(buffer); return num; } finally { ArrayPool<byte>.Shared.Return(array); } } public static void Write(this Stream stream, ReadOnlySpan<byte> buffer) { byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length); try { buffer.CopyTo(array); stream.Write(array, 0, buffer.Length); } finally { ArrayPool<byte>.Shared.Return(array); } } } internal static class ColorNumerics { private static readonly Vector4 Bt709 = new Vector4(0.2126f, 0.7152f, 0.0722f, 0f); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetBT709Luminance(ref Vector4 vector, int luminanceLevels) { return (int)MathF.Round(Vector4.Dot(vector, Bt709) * (float)(luminanceLevels - 1)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte Get8BitBT709Luminance(byte r, byte g, byte b) { return (byte)((float)(int)r * 0.2126f + (float)(int)g * 0.7152f + (float)(int)b * 0.0722f + 0.5f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ushort Get16BitBT709Luminance(ushort r, ushort g, ushort b) { return (ushort)((float)(int)r * 0.2126f + (float)(int)g * 0.7152f + (float)(int)b * 0.0722f + 0.5f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ushort Get16BitBT709Luminance(float r, float g, float b) { return (ushort)(r * 0.2126f + g * 0.7152f + b * 0.0722f + 0.5f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte DownScaleFrom16BitTo8Bit(ushort component) { return (byte)(component * 255 + 32895 >> 16); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ushort UpscaleFrom8BitTo16Bit(byte component) { return (ushort)(component * 257); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetBitsNeededForColorDepth(int colors) { return Math.Max(1, (int)Math.Ceiling(Math.Log(colors, 2.0))); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetColorCountForBitDepth(int bitDepth) { return 1 << bitDepth; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Transform(ref Vector4 vector, ref ColorMatrix matrix) { float x = vector.X; float y = vector.Y; float z = vector.Z; float w = vector.W; vector.X = x * matrix.M11 + y * matrix.M21 + z * matrix.M31 + w * matrix.M41 + matrix.M51; vector.Y = x * matrix.M12 + y * matrix.M22 + z * matrix.M32 + w * matrix.M42 + matrix.M52; vector.Z = x * matrix.M13 + y * matrix.M23 + z * matrix.M33 + w * matrix.M43 + matrix.M53; vector.W = x * matrix.M14 + y * matrix.M24 + z * matrix.M34 + w * matrix.M44 + matrix.M54; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Transform(Span<Vector4> vectors, ref ColorMatrix matrix) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); for (int i = 0; i < vectors.Length; i++) { Transform(ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, i), ref matrix); } } } internal static class EnumUtils { public static TEnum Parse<TEnum>(int value, TEnum defaultValue) where TEnum : Enum { foreach (TEnum value2 in Enum.GetValues(typeof(TEnum))) { TEnum val2 = value2; if (value == System.Runtime.CompilerServices.Unsafe.As<TEnum, int>(ref val2)) { return value2; } } return defaultValue; } public static bool HasFlag<TEnum>(TEnum value, TEnum flag) where TEnum : Enum { uint num = System.Runtime.CompilerServices.Unsafe.As<TEnum, uint>(ref flag); return (System.Runtime.CompilerServices.Unsafe.As<TEnum, uint>(ref value) & num) == num; } } internal static class InliningOptions { public const MethodImplOptions AlwaysInline = MethodImplOptions.AggressiveInlining; public const MethodImplOptions HotPath = MethodImplOptions.AggressiveInlining; public const MethodImplOptions ShortMethod = MethodImplOptions.AggressiveInlining; public const MethodImplOptions ColdPath = MethodImplOptions.NoInlining; } internal static class Numerics { private static ReadOnlySpan<byte> Log2DeBruijn => new byte[32] { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GreatestCommonDivisor(int a, int b) { while (b != 0) { int num = b; b = a % b; a = num; } return a; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LeastCommonMultiple(int a, int b) { return a / GreatestCommonDivisor(a, b) * b; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Modulo2(int x) { return x & 1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Modulo4(int x) { return x & 3; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Modulo8(int x) { return x & 7; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int ModuloP2(int x, int m) { return x & (m - 1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Abs(int x) { int num = x >> 31; return (x ^ num) - num; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Pow2(float x) { return x * x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Pow3(float x) { return x * x * x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Gaussian(float x, float sigma) { float num = MathF.Sqrt((float)Math.PI * 2f) * sigma; float num2 = (0f - x) * x; float num3 = 2f * Pow2(sigma); float num4 = 1f / num; float num5 = MathF.Exp(num2 / num3); return num4 * num5; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float SinC(float f) { if (MathF.Abs(f) > Constants.Epsilon) { f *= (float)Math.PI; float num = MathF.Sin(f) / f; if (!(MathF.Abs(num) < Constants.Epsilon)) { return num; } return 0f; } return 1f; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte Clamp(byte value, byte min, byte max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint Clamp(uint value, uint min, uint max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Clamp(int value, int min, int max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Clamp(float value, float min, float max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double Clamp(double value, double min, double max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Clamp(Vector4 value, Vector4 min, Vector4 max) { return Vector4.Min(Vector4.Max(value, min), max); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<byte> span, byte min, byte max) { Span<byte> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref byte reference = ref MemoryMarshal.GetReference(span2); ref byte reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<byte>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<byte>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<byte>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<uint> span, uint min, uint max) { Span<uint> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref uint reference = ref MemoryMarshal.GetReference(span2); ref uint reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<uint>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<uint>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<uint>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<int> span, int min, int max) { Span<int> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref int reference = ref MemoryMarshal.GetReference(span2); ref int reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<int>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<int>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<int>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<float> span, float min, float max) { Span<float> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref float reference = ref MemoryMarshal.GetReference(span2); ref float reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<float>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<float>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<float>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<double> span, double min, double max) { Span<double> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref double reference = ref MemoryMarshal.GetReference(span2); ref double reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<double>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<double>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<double>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int ClampReduce<T>(Span<T> span, T min, T max) where T : unmanaged { if (Vector.IsHardwareAccelerated && span.Length >= Vector<T>.Count) { int num = ModuloP2(span.Length, Vector<T>.Count); int num2 = span.Length - num; if (num2 > 0) { ClampImpl(span.Slice(0, num2), min, max); } return num2; } return 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void ClampImpl<T>(Span<T> span, T min, T max) where T : unmanaged { _ = ref MemoryMarshal.GetReference(span); Vector<T> left = new Vector<T>(min); Vector<T> right = new Vector<T>(max); int num = span.Length / Vector<T>.Count; int num2 = Modulo4(num); int num3 = num - num2; ref Vector<T> reference = ref System.Runtime.CompilerServices.Unsafe.As<T, Vector<T>>(ref MemoryMarshal.GetReference(span)); ref Vector<T> reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 1); ref Vector<T> reference3 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 2); ref Vector<T> reference4 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 3); ref Vector<T> reference5 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, num3); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector<T>>(ref reference, ref reference5)) { reference = Vector.Min(Vector.Max(left, reference), right); reference2 = Vector.Min(Vector.Max(left, reference2), right); reference3 = Vector.Min(Vector.Max(left, reference3), right); reference4 = Vector.Min(Vector.Max(left, reference4), right); reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 4); reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference2, 4); reference3 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference3, 4); reference4 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference4, 4); } if (num2 > 0) { reference = ref reference5; reference5 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference5, num2); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector<T>>(ref reference, ref reference5)) { reference = Vector.Min(Vector.Max(left, reference), right); reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Premultiply(ref Vector4 source) { float w = source.W; source *= w; source.W = w; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void UnPremultiply(ref Vector4 source) { float w = source.W; source /= w; source.W = w; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Premultiply(Span<Vector4> vectors) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); ref Vector4 reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, vectors.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector4>(ref reference, ref reference2)) { Premultiply(ref reference); reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, 1); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void UnPremultiply(Span<Vector4> vectors) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); ref Vector4 reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, vectors.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector4>(ref reference, ref reference2)) { UnPremultiply(ref reference); reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, 1); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void CubePowOnXYZ(Span<Vector4> vectors) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); ref Vector4 reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, vectors.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector4>(ref reference, ref reference2)) { Vector4 vector = reference; float w = vector.W; vector = vector * vector * vector; vector.W = w; reference = vector; reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, 1); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void CubeRootOnXYZ(Span<Vector4> vectors) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); ref Vector4 reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, vectors.Length); Vector4 vector2 = default(Vector4); while (System.Runtime.CompilerServices.Unsa
BepInEx/plugins/SixLabors.ImageSharp.dll
Decompiled 2 months ago
The result has been truncated due to the large size, download it to view full contents!
using System; using System.Buffers; using System.Buffers.Binary; using System.Buffers.Text; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.IO.Compression; using System.Linq; using System.Numerics; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Cryptography; using System.Security.Permissions; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml.Linq; using Microsoft.CodeAnalysis; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.ColorSpaces; using SixLabors.ImageSharp.ColorSpaces.Companding; using SixLabors.ImageSharp.ColorSpaces.Conversion; using SixLabors.ImageSharp.Common.Helpers; using SixLabors.ImageSharp.Compression.Zlib; using SixLabors.ImageSharp.Diagnostics; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Jpeg.Components; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters; using SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder; using SixLabors.ImageSharp.Formats.Pbm; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Png.Chunks; using SixLabors.ImageSharp.Formats.Png.Filters; using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.Formats.Tiff; using SixLabors.ImageSharp.Formats.Tiff.Compression; using SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors; using SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors; using SixLabors.ImageSharp.Formats.Tiff.Constants; using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Writers; using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Formats.Webp.BitReader; using SixLabors.ImageSharp.Formats.Webp.BitWriter; using SixLabors.ImageSharp.Formats.Webp.Lossless; using SixLabors.ImageSharp.Formats.Webp.Lossy; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory.Internals; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.Metadata.Profiles.Icc; using SixLabors.ImageSharp.Metadata.Profiles.Iptc; using SixLabors.ImageSharp.Metadata.Profiles.Xmp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats.PixelBlenders; using SixLabors.ImageSharp.PixelFormats.Utils; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; using SixLabors.ImageSharp.Processing.Processors.Binarization; using SixLabors.ImageSharp.Processing.Processors.Convolution; using SixLabors.ImageSharp.Processing.Processors.Convolution.Parameters; using SixLabors.ImageSharp.Processing.Processors.Dithering; using SixLabors.ImageSharp.Processing.Processors.Drawing; using SixLabors.ImageSharp.Processing.Processors.Effects; using SixLabors.ImageSharp.Processing.Processors.Filters; using SixLabors.ImageSharp.Processing.Processors.Normalization; using SixLabors.ImageSharp.Processing.Processors.Overlays; using SixLabors.ImageSharp.Processing.Processors.Quantization; using SixLabors.ImageSharp.Processing.Processors.Transforms; using SixLabors.ImageSharp.Tuples; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")] [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] [assembly: InternalsVisibleTo("ImageSharp.Benchmarks, PublicKey=00240000048000009400000006020000002400005253413100040000010001000147e6fe6766715eec6cfed61f1e7dcdbf69748a3e355c67e9d8dfd953acab1d5e012ba34b23308166fdc61ee1d0390d5f36d814a6091dd4b5ed9eda5a26afced924c683b4bfb4b3d64b0586a57eff9f02b1f84e3cb0ddd518bd1697f2c84dcbb97eb8bb5c7801be12112ed0ec86db934b0e9a5171e6bb1384b6d2f7d54dfa97")] [assembly: InternalsVisibleTo("ImageSharp.Tests.ProfilingSandbox, PublicKey=00240000048000009400000006020000002400005253413100040000010001000147e6fe6766715eec6cfed61f1e7dcdbf69748a3e355c67e9d8dfd953acab1d5e012ba34b23308166fdc61ee1d0390d5f36d814a6091dd4b5ed9eda5a26afced924c683b4bfb4b3d64b0586a57eff9f02b1f84e3cb0ddd518bd1697f2c84dcbb97eb8bb5c7801be12112ed0ec86db934b0e9a5171e6bb1384b6d2f7d54dfa97")] [assembly: InternalsVisibleTo("SixLabors.ImageSharp.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001000147e6fe6766715eec6cfed61f1e7dcdbf69748a3e355c67e9d8dfd953acab1d5e012ba34b23308166fdc61ee1d0390d5f36d814a6091dd4b5ed9eda5a26afced924c683b4bfb4b3d64b0586a57eff9f02b1f84e3cb0ddd518bd1697f2c84dcbb97eb8bb5c7801be12112ed0ec86db934b0e9a5171e6bb1384b6d2f7d54dfa97")] [assembly: InternalsVisibleTo("SixLabors.ImageSharp.Drawing.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001000147e6fe6766715eec6cfed61f1e7dcdbf69748a3e355c67e9d8dfd953acab1d5e012ba34b23308166fdc61ee1d0390d5f36d814a6091dd4b5ed9eda5a26afced924c683b4bfb4b3d64b0586a57eff9f02b1f84e3cb0ddd518bd1697f2c84dcbb97eb8bb5c7801be12112ed0ec86db934b0e9a5171e6bb1384b6d2f7d54dfa97")] [assembly: AssemblyCompany("Six Labors")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright © Six Labors")] [assembly: AssemblyDescription("A new, fully featured, fully managed, cross-platform, 2D graphics API for .NET")] [assembly: AssemblyFileVersion("2.1.12.0")] [assembly: AssemblyInformationalVersion("2.1.12+168f3f550b94d4d58c7f9662968016b0bf6cc044")] [assembly: AssemblyProduct("SixLabors.ImageSharp")] [assembly: AssemblyTitle("SixLabors.ImageSharp")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/SixLabors/ImageSharp")] [assembly: NeutralResourcesLanguage("en")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("2.0.0.0")] [assembly: TypeForwardedTo(typeof(ExcludeFromCodeCoverageAttribute))] [module: UnverifiableCode] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsUnmanagedAttribute : Attribute { } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NativeIntegerAttribute : Attribute { public readonly bool[] TransformFlags; public NativeIntegerAttribute() { TransformFlags = new bool[1] { true }; } public NativeIntegerAttribute(bool[] P_0) { TransformFlags = P_0; } } } namespace System { [ExcludeFromCodeCoverage] internal struct HashCode { private static readonly uint s_seed = GenerateGlobalSeed(); private const uint Prime1 = 2654435761u; private const uint Prime2 = 2246822519u; private const uint Prime3 = 3266489917u; private const uint Prime4 = 668265263u; private const uint Prime5 = 374761393u; private uint _v1; private uint _v2; private uint _v3; private uint _v4; private uint _queue1; private uint _queue2; private uint _queue3; private uint _length; private static uint GenerateGlobalSeed() { byte[] array = new byte[4]; using (RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create()) { randomNumberGenerator.GetBytes(array); } return BinaryPrimitives.ReadUInt32LittleEndian(array); } public static int Combine<T1>(T1 value1) { uint queuedValue = (uint)(value1?.GetHashCode() ?? 0); return (int)MixFinal(QueueRound(MixEmptyState() + 4, queuedValue)); } public static int Combine<T1, T2>(T1 value1, T2 value2) { uint queuedValue = (uint)(value1?.GetHashCode() ?? 0); uint queuedValue2 = (uint)(value2?.GetHashCode() ?? 0); return (int)MixFinal(QueueRound(QueueRound(MixEmptyState() + 8, queuedValue), queuedValue2)); } public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3) { uint queuedValue = (uint)(value1?.GetHashCode() ?? 0); uint queuedValue2 = (uint)(value2?.GetHashCode() ?? 0); uint queuedValue3 = (uint)(value3?.GetHashCode() ?? 0); return (int)MixFinal(QueueRound(QueueRound(QueueRound(MixEmptyState() + 12, queuedValue), queuedValue2), queuedValue3)); } public static int Combine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); return (int)MixFinal(MixState(v, v2, v3, v4) + 16); } public static int Combine<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); uint queuedValue = (uint)(value5?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); return (int)MixFinal(QueueRound(MixState(v, v2, v3, v4) + 20, queuedValue)); } public static int Combine<T1, T2, T3, T4, T5, T6>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); uint queuedValue = (uint)(value5?.GetHashCode() ?? 0); uint queuedValue2 = (uint)(value6?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); return (int)MixFinal(QueueRound(QueueRound(MixState(v, v2, v3, v4) + 24, queuedValue), queuedValue2)); } public static int Combine<T1, T2, T3, T4, T5, T6, T7>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); uint queuedValue = (uint)(value5?.GetHashCode() ?? 0); uint queuedValue2 = (uint)(value6?.GetHashCode() ?? 0); uint queuedValue3 = (uint)(value7?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); return (int)MixFinal(QueueRound(QueueRound(QueueRound(MixState(v, v2, v3, v4) + 28, queuedValue), queuedValue2), queuedValue3)); } public static int Combine<T1, T2, T3, T4, T5, T6, T7, T8>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8) { uint input = (uint)(value1?.GetHashCode() ?? 0); uint input2 = (uint)(value2?.GetHashCode() ?? 0); uint input3 = (uint)(value3?.GetHashCode() ?? 0); uint input4 = (uint)(value4?.GetHashCode() ?? 0); uint input5 = (uint)(value5?.GetHashCode() ?? 0); uint input6 = (uint)(value6?.GetHashCode() ?? 0); uint input7 = (uint)(value7?.GetHashCode() ?? 0); uint input8 = (uint)(value8?.GetHashCode() ?? 0); Initialize(out var v, out var v2, out var v3, out var v4); v = Round(v, input); v2 = Round(v2, input2); v3 = Round(v3, input3); v4 = Round(v4, input4); v = Round(v, input5); v2 = Round(v2, input6); v3 = Round(v3, input7); v4 = Round(v4, input8); return (int)MixFinal(MixState(v, v2, v3, v4) + 32); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint Rol(uint value, int count) { return (value << count) | (value >> 32 - count); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4) { v1 = (uint)((int)s_seed + -1640531535 + -2048144777); v2 = s_seed + 2246822519u; v3 = s_seed; v4 = s_seed - 2654435761u; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint Round(uint hash, uint input) { hash += (uint)((int)input * -2048144777); hash = Rol(hash, 13); hash *= 2654435761u; return hash; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint QueueRound(uint hash, uint queuedValue) { hash += (uint)((int)queuedValue * -1028477379); return Rol(hash, 17) * 668265263; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint MixState(uint v1, uint v2, uint v3, uint v4) { return Rol(v1, 1) + Rol(v2, 7) + Rol(v3, 12) + Rol(v4, 18); } private static uint MixEmptyState() { return s_seed + 374761393; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint MixFinal(uint hash) { hash ^= hash >> 15; hash *= 2246822519u; hash ^= hash >> 13; hash *= 3266489917u; hash ^= hash >> 16; return hash; } public void Add<T>(T value) { Add(value?.GetHashCode() ?? 0); } public void Add<T>(T value, IEqualityComparer<T> comparer) { Add(comparer?.GetHashCode(value) ?? value?.GetHashCode() ?? 0); } private void Add(int value) { uint num = _length++; switch (num % 4) { case 0u: _queue1 = (uint)value; return; case 1u: _queue2 = (uint)value; return; case 2u: _queue3 = (uint)value; return; } if (num == 3) { Initialize(out _v1, out _v2, out _v3, out _v4); } _v1 = Round(_v1, _queue1); _v2 = Round(_v2, _queue2); _v3 = Round(_v3, _queue3); _v4 = Round(_v4, (uint)value); } public int ToHashCode() { uint length = _length; uint num = length % 4; uint num2 = ((length < 4) ? MixEmptyState() : MixState(_v1, _v2, _v3, _v4)); num2 += length * 4; if (num != 0) { num2 = QueueRound(num2, _queue1); if (num > 1) { num2 = QueueRound(num2, _queue2); if (num > 2) { num2 = QueueRound(num2, _queue3); } } } return (int)MixFinal(num2); } [Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes. Use ToHashCode to retrieve the computed hash code.", true)] [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { throw new NotSupportedException("Equality not supported"); } [Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes.", true)] [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { throw new NotSupportedException("Equality not supported"); } } internal static class MathF { public const float PI = 3.1415927f; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Abs(float f) { return Math.Abs(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Atan2(float y, float x) { return (float)Math.Atan2(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Ceiling(float f) { return (float)Math.Ceiling(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Cos(float f) { return (float)Math.Cos(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Exp(float f) { return (float)Math.Exp(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Floor(float f) { return (float)Math.Floor(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Max(float val1, float val2) { return Math.Max(val1, val2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Min(float val1, float val2) { return Math.Min(val1, val2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Pow(float x, float y) { return (float)Math.Pow(x, y); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Round(float f) { return (float)Math.Round(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Round(float f, MidpointRounding mode) { return (float)Math.Round(f, mode); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Sin(float f) { return (float)Math.Sin(f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Sqrt(float f) { return (float)Math.Sqrt(f); } } } namespace SixLabors { [DebuggerStepThrough] internal static class DebugGuard { [Conditional("DEBUG")] public static void IsTrue(bool target, string message) { if (!target) { throw new InvalidOperationException(message); } } [Conditional("DEBUG")] public static void NotDisposed(bool isDisposed, string objectName) { if (isDisposed) { throw new ObjectDisposedException(objectName); } } [Conditional("DEBUG")] public static void MustBeSameSized<T>(ReadOnlySpan<T> target, ReadOnlySpan<T> other, string parameterName) where T : struct { if (target.Length != other.Length) { throw new ArgumentException("Span-s must be the same size!", parameterName); } } [Conditional("DEBUG")] public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> target, ReadOnlySpan<T> minSpan, string parameterName) where T : struct { if (target.Length < minSpan.Length) { throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName); } } [Conditional("DEBUG")] public static void NotNull<TValue>(TValue value, string parameterName) where TValue : class { if (value == null) { ThrowArgumentNullException(parameterName); } } [Conditional("DEBUG")] public static void NotNullOrWhiteSpace(string value, string parameterName) { if (value == null) { ThrowArgumentNullException(parameterName); } if (string.IsNullOrWhiteSpace(value)) { ThrowArgumentException("Must not be empty or whitespace.", parameterName); } } [Conditional("DEBUG")] public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(max) >= 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}."); } } [Conditional("DEBUG")] public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(max) > 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}."); } } [Conditional("DEBUG")] public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) <= 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than {min}."); } } [Conditional("DEBUG")] public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) < 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}."); } } [Conditional("DEBUG")] public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) { ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}."); } } [Conditional("DEBUG")] public static void IsTrue(bool target, string parameterName, string message) { if (!target) { ThrowArgumentException(message, parameterName); } } [Conditional("DEBUG")] public static void IsFalse(bool target, string parameterName, string message) { if (target) { ThrowArgumentException(message, parameterName); } } [Conditional("DEBUG")] public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> source, int minLength, string parameterName) { if (source.Length < minLength) { ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName); } } [Conditional("DEBUG")] public static void MustBeSizedAtLeast<T>(Span<T> source, int minLength, string parameterName) { if (source.Length < minLength) { ThrowArgumentException($"The size must be at least {minLength}.", parameterName); } } [Conditional("DEBUG")] public static void DestinationShouldNotBeTooShort<TSource, TDest>(ReadOnlySpan<TSource> source, Span<TDest> destination, string destinationParamName) { if (destination.Length < source.Length) { ThrowArgumentException("Destination span is too short!", destinationParamName); } } [Conditional("DEBUG")] public static void DestinationShouldNotBeTooShort<TSource, TDest>(Span<TSource> source, Span<TDest> destination, string destinationParamName) { if (destination.Length < source.Length) { ThrowArgumentException("Destination span is too short!", destinationParamName); } } [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentException(string message, string parameterName) { throw new ArgumentException(message, parameterName); } [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentOutOfRangeException(string parameterName, string message) { throw new ArgumentOutOfRangeException(parameterName, message); } [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentNullException(string parameterName) { throw new ArgumentNullException(parameterName); } } [DebuggerStepThrough] internal static class Guard { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeValueType<TValue>(TValue value, string parameterName) { if (!value.GetType().IsValueType) { ThrowHelper.ThrowArgumentException("Type must be a struct.", parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void NotNull<TValue>(TValue value, string parameterName) where TValue : class { if (value == null) { ThrowHelper.ThrowArgumentNullExceptionForNotNull(parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void NotNullOrWhiteSpace(string value, string parameterName) { if (string.IsNullOrWhiteSpace(value)) { ThrowHelper.ThrowArgumentExceptionForNotNullOrWhitespace(value, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(max) >= 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(max) > 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) <= 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) < 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName) where TValue : IComparable<TValue> { if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsTrue(bool target, string parameterName, string message) { if (!target) { ThrowHelper.ThrowArgumentException(message, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsFalse(bool target, string parameterName, string message) { if (target) { ThrowHelper.ThrowArgumentException(message, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> source, int minLength, string parameterName) { if (source.Length < minLength) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(minLength, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeSizedAtLeast<T>(Span<T> source, int minLength, string parameterName) { if (source.Length < minLength) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(minLength, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void DestinationShouldNotBeTooShort<TSource, TDest>(ReadOnlySpan<TSource> source, Span<TDest> destination, string destinationParamName) { if (destination.Length < source.Length) { ThrowHelper.ThrowArgumentException("Destination span is too short!", destinationParamName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void DestinationShouldNotBeTooShort<TSource, TDest>(Span<TSource> source, Span<TDest> destination, string destinationParamName) { if (destination.Length < source.Length) { ThrowHelper.ThrowArgumentException("Destination span is too short!", destinationParamName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(byte value, byte max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(byte value, byte max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(byte value, byte min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(byte value, byte min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(byte value, byte min, byte max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(sbyte value, sbyte max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(sbyte value, sbyte max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(sbyte value, sbyte min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(sbyte value, sbyte min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(sbyte value, sbyte min, sbyte max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(short value, short max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(short value, short max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(short value, short min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(short value, short min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(short value, short min, short max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(ushort value, ushort max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(ushort value, ushort max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(ushort value, ushort min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(ushort value, ushort min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(ushort value, ushort min, ushort max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(char value, char max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(char value, char max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(char value, char min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(char value, char min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(char value, char min, char max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(int value, int max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(int value, int max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(int value, int min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(int value, int min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(int value, int min, int max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(uint value, uint max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(uint value, uint max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(uint value, uint min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(uint value, uint min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(uint value, uint min, uint max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(float value, float max, string parameterName) { if (!(value < max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(float value, float max, string parameterName) { if (!(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(float value, float min, string parameterName) { if (!(value > min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(float value, float min, string parameterName) { if (!(value >= min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(float value, float min, float max, string parameterName) { if (!(value >= min) || !(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(long value, long max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(long value, long max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(long value, long min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(long value, long min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(long value, long min, long max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(ulong value, ulong max, string parameterName) { if (value >= max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(ulong value, ulong max, string parameterName) { if (value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(ulong value, ulong min, string parameterName) { if (value <= min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(ulong value, ulong min, string parameterName) { if (value < min) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(ulong value, ulong min, ulong max, string parameterName) { if (value < min || value > max) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(double value, double max, string parameterName) { if (!(value < max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(double value, double max, string parameterName) { if (!(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(double value, double min, string parameterName) { if (!(value > min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(double value, double min, string parameterName) { if (!(value >= min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(double value, double min, double max, string parameterName) { if (!(value >= min) || !(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(decimal value, decimal max, string parameterName) { if (!(value < max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(decimal value, decimal max, string parameterName) { if (!(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(decimal value, decimal min, string parameterName) { if (!(value > min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(decimal value, decimal min, string parameterName) { if (!(value >= min)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(decimal value, decimal min, decimal max, string parameterName) { if (!(value >= min) || !(value <= max)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } } } internal static class ThrowHelper { [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentNullExceptionForNotNull(string name) { ThrowArgumentNullException(name, "Parameter \"" + name + "\" must be not null."); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForNotNullOrWhitespace(string value, string name) { if (value == null) { ThrowArgumentNullException(name, "Parameter \"" + name + "\" must be not null."); } else { ThrowArgumentException(name, "Parameter \"" + name + "\" must not be empty or whitespace."); } } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeLessThan<T>(T value, T max, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be less than {max}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo<T>(T value, T maximum, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be less than or equal to {maximum}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan<T>(T value, T minimum, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be greater than {minimum}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo<T>(T value, T minimum, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be greater than or equal to {minimum}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo<T>(T value, T minimum, T maximum, string name) { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be between or equal to {minimum} and {maximum}, was {value}"); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(int minLength, string parameterName) { ThrowArgumentException($"Spans must be at least of length {minLength}!", parameterName); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentException(string message, string name) { throw new ArgumentException(message, name); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentNullException(string name, string message) { throw new ArgumentNullException(name, message); } [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeException(string name, string message) { throw new ArgumentOutOfRangeException(name, message); } } } namespace SixLabors.ImageSharp { public readonly struct Color : IEquatable<Color> { private readonly Rgba64 data; private readonly IPixel boxedHighPrecisionPixel; private static readonly Lazy<Dictionary<string, Color>> NamedColorsLookupLazy = new Lazy<Dictionary<string, Color>>(CreateNamedColorsLookup, isThreadSafe: true); public static readonly Color AliceBlue = FromRgba(240, 248, byte.MaxValue, byte.MaxValue); public static readonly Color AntiqueWhite = FromRgba(250, 235, 215, byte.MaxValue); public static readonly Color Aqua = FromRgba(0, byte.MaxValue, byte.MaxValue, byte.MaxValue); public static readonly Color Aquamarine = FromRgba(127, byte.MaxValue, 212, byte.MaxValue); public static readonly Color Azure = FromRgba(240, byte.MaxValue, byte.MaxValue, byte.MaxValue); public static readonly Color Beige = FromRgba(245, 245, 220, byte.MaxValue); public static readonly Color Bisque = FromRgba(byte.MaxValue, 228, 196, byte.MaxValue); public static readonly Color Black = FromRgba(0, 0, 0, byte.MaxValue); public static readonly Color BlanchedAlmond = FromRgba(byte.MaxValue, 235, 205, byte.MaxValue); public static readonly Color Blue = FromRgba(0, 0, byte.MaxValue, byte.MaxValue); public static readonly Color BlueViolet = FromRgba(138, 43, 226, byte.MaxValue); public static readonly Color Brown = FromRgba(165, 42, 42, byte.MaxValue); public static readonly Color BurlyWood = FromRgba(222, 184, 135, byte.MaxValue); public static readonly Color CadetBlue = FromRgba(95, 158, 160, byte.MaxValue); public static readonly Color Chartreuse = FromRgba(127, byte.MaxValue, 0, byte.MaxValue); public static readonly Color Chocolate = FromRgba(210, 105, 30, byte.MaxValue); public static readonly Color Coral = FromRgba(byte.MaxValue, 127, 80, byte.MaxValue); public static readonly Color CornflowerBlue = FromRgba(100, 149, 237, byte.MaxValue); public static readonly Color Cornsilk = FromRgba(byte.MaxValue, 248, 220, byte.MaxValue); public static readonly Color Crimson = FromRgba(220, 20, 60, byte.MaxValue); public static readonly Color Cyan = Aqua; public static readonly Color DarkBlue = FromRgba(0, 0, 139, byte.MaxValue); public static readonly Color DarkCyan = FromRgba(0, 139, 139, byte.MaxValue); public static readonly Color DarkGoldenrod = FromRgba(184, 134, 11, byte.MaxValue); public static readonly Color DarkGray = FromRgba(169, 169, 169, byte.MaxValue); public static readonly Color DarkGreen = FromRgba(0, 100, 0, byte.MaxValue); public static readonly Color DarkGrey = DarkGray; public static readonly Color DarkKhaki = FromRgba(189, 183, 107, byte.MaxValue); public static readonly Color DarkMagenta = FromRgba(139, 0, 139, byte.MaxValue); public static readonly Color DarkOliveGreen = FromRgba(85, 107, 47, byte.MaxValue); public static readonly Color DarkOrange = FromRgba(byte.MaxValue, 140, 0, byte.MaxValue); public static readonly Color DarkOrchid = FromRgba(153, 50, 204, byte.MaxValue); public static readonly Color DarkRed = FromRgba(139, 0, 0, byte.MaxValue); public static readonly Color DarkSalmon = FromRgba(233, 150, 122, byte.MaxValue); public static readonly Color DarkSeaGreen = FromRgba(143, 188, 143, byte.MaxValue); public static readonly Color DarkSlateBlue = FromRgba(72, 61, 139, byte.MaxValue); public static readonly Color DarkSlateGray = FromRgba(47, 79, 79, byte.MaxValue); public static readonly Color DarkSlateGrey = DarkSlateGray; public static readonly Color DarkTurquoise = FromRgba(0, 206, 209, byte.MaxValue); public static readonly Color DarkViolet = FromRgba(148, 0, 211, byte.MaxValue); public static readonly Color DeepPink = FromRgba(byte.MaxValue, 20, 147, byte.MaxValue); public static readonly Color DeepSkyBlue = FromRgba(0, 191, byte.MaxValue, byte.MaxValue); public static readonly Color DimGray = FromRgba(105, 105, 105, byte.MaxValue); public static readonly Color DimGrey = DimGray; public static readonly Color DodgerBlue = FromRgba(30, 144, byte.MaxValue, byte.MaxValue); public static readonly Color Firebrick = FromRgba(178, 34, 34, byte.MaxValue); public static readonly Color FloralWhite = FromRgba(byte.MaxValue, 250, 240, byte.MaxValue); public static readonly Color ForestGreen = FromRgba(34, 139, 34, byte.MaxValue); public static readonly Color Fuchsia = FromRgba(byte.MaxValue, 0, byte.MaxValue, byte.MaxValue); public static readonly Color Gainsboro = FromRgba(220, 220, 220, byte.MaxValue); public static readonly Color GhostWhite = FromRgba(248, 248, byte.MaxValue, byte.MaxValue); public static readonly Color Gold = FromRgba(byte.MaxValue, 215, 0, byte.MaxValue); public static readonly Color Goldenrod = FromRgba(218, 165, 32, byte.MaxValue); public static readonly Color Gray = FromRgba(128, 128, 128, byte.MaxValue); public static readonly Color Green = FromRgba(0, 128, 0, byte.MaxValue); public static readonly Color GreenYellow = FromRgba(173, byte.MaxValue, 47, byte.MaxValue); public static readonly Color Grey = Gray; public static readonly Color Honeydew = FromRgba(240, byte.MaxValue, 240, byte.MaxValue); public static readonly Color HotPink = FromRgba(byte.MaxValue, 105, 180, byte.MaxValue); public static readonly Color IndianRed = FromRgba(205, 92, 92, byte.MaxValue); public static readonly Color Indigo = FromRgba(75, 0, 130, byte.MaxValue); public static readonly Color Ivory = FromRgba(byte.MaxValue, byte.MaxValue, 240, byte.MaxValue); public static readonly Color Khaki = FromRgba(240, 230, 140, byte.MaxValue); public static readonly Color Lavender = FromRgba(230, 230, 250, byte.MaxValue); public static readonly Color LavenderBlush = FromRgba(byte.MaxValue, 240, 245, byte.MaxValue); public static readonly Color LawnGreen = FromRgba(124, 252, 0, byte.MaxValue); public static readonly Color LemonChiffon = FromRgba(byte.MaxValue, 250, 205, byte.MaxValue); public static readonly Color LightBlue = FromRgba(173, 216, 230, byte.MaxValue); public static readonly Color LightCoral = FromRgba(240, 128, 128, byte.MaxValue); public static readonly Color LightCyan = FromRgba(224, byte.MaxValue, byte.MaxValue, byte.MaxValue); public static readonly Color LightGoldenrodYellow = FromRgba(250, 250, 210, byte.MaxValue); public static readonly Color LightGray = FromRgba(211, 211, 211, byte.MaxValue); public static readonly Color LightGreen = FromRgba(144, 238, 144, byte.MaxValue); public static readonly Color LightGrey = LightGray; public static readonly Color LightPink = FromRgba(byte.MaxValue, 182, 193, byte.MaxValue); public static readonly Color LightSalmon = FromRgba(byte.MaxValue, 160, 122, byte.MaxValue); public static readonly Color LightSeaGreen = FromRgba(32, 178, 170, byte.MaxValue); public static readonly Color LightSkyBlue = FromRgba(135, 206, 250, byte.MaxValue); public static readonly Color LightSlateGray = FromRgba(119, 136, 153, byte.MaxValue); public static readonly Color LightSlateGrey = LightSlateGray; public static readonly Color LightSteelBlue = FromRgba(176, 196, 222, byte.MaxValue); public static readonly Color LightYellow = FromRgba(byte.MaxValue, byte.MaxValue, 224, byte.MaxValue); public static readonly Color Lime = FromRgba(0, byte.MaxValue, 0, byte.MaxValue); public static readonly Color LimeGreen = FromRgba(50, 205, 50, byte.MaxValue); public static readonly Color Linen = FromRgba(250, 240, 230, byte.MaxValue); public static readonly Color Magenta = Fuchsia; public static readonly Color Maroon = FromRgba(128, 0, 0, byte.MaxValue); public static readonly Color MediumAquamarine = FromRgba(102, 205, 170, byte.MaxValue); public static readonly Color MediumBlue = FromRgba(0, 0, 205, byte.MaxValue); public static readonly Color MediumOrchid = FromRgba(186, 85, 211, byte.MaxValue); public static readonly Color MediumPurple = FromRgba(147, 112, 219, byte.MaxValue); public static readonly Color MediumSeaGreen = FromRgba(60, 179, 113, byte.MaxValue); public static readonly Color MediumSlateBlue = FromRgba(123, 104, 238, byte.MaxValue); public static readonly Color MediumSpringGreen = FromRgba(0, 250, 154, byte.MaxValue); public static readonly Color MediumTurquoise = FromRgba(72, 209, 204, byte.MaxValue); public static readonly Color MediumVioletRed = FromRgba(199, 21, 133, byte.MaxValue); public static readonly Color MidnightBlue = FromRgba(25, 25, 112, byte.MaxValue); public static readonly Color MintCream = FromRgba(245, byte.MaxValue, 250, byte.MaxValue); public static readonly Color MistyRose = FromRgba(byte.MaxValue, 228, 225, byte.MaxValue); public static readonly Color Moccasin = FromRgba(byte.MaxValue, 228, 181, byte.MaxValue); public static readonly Color NavajoWhite = FromRgba(byte.MaxValue, 222, 173, byte.MaxValue); public static readonly Color Navy = FromRgba(0, 0, 128, byte.MaxValue); public static readonly Color OldLace = FromRgba(253, 245, 230, byte.MaxValue); public static readonly Color Olive = FromRgba(128, 128, 0, byte.MaxValue); public static readonly Color OliveDrab = FromRgba(107, 142, 35, byte.MaxValue); public static readonly Color Orange = FromRgba(byte.MaxValue, 165, 0, byte.MaxValue); public static readonly Color OrangeRed = FromRgba(byte.MaxValue, 69, 0, byte.MaxValue); public static readonly Color Orchid = FromRgba(218, 112, 214, byte.MaxValue); public static readonly Color PaleGoldenrod = FromRgba(238, 232, 170, byte.MaxValue); public static readonly Color PaleGreen = FromRgba(152, 251, 152, byte.MaxValue); public static readonly Color PaleTurquoise = FromRgba(175, 238, 238, byte.MaxValue); public static readonly Color PaleVioletRed = FromRgba(219, 112, 147, byte.MaxValue); public static readonly Color PapayaWhip = FromRgba(byte.MaxValue, 239, 213, byte.MaxValue); public static readonly Color PeachPuff = FromRgba(byte.MaxValue, 218, 185, byte.MaxValue); public static readonly Color Peru = FromRgba(205, 133, 63, byte.MaxValue); public static readonly Color Pink = FromRgba(byte.MaxValue, 192, 203, byte.MaxValue); public static readonly Color Plum = FromRgba(221, 160, 221, byte.MaxValue); public static readonly Color PowderBlue = FromRgba(176, 224, 230, byte.MaxValue); public static readonly Color Purple = FromRgba(128, 0, 128, byte.MaxValue); public static readonly Color RebeccaPurple = FromRgba(102, 51, 153, byte.MaxValue); public static readonly Color Red = FromRgba(byte.MaxValue, 0, 0, byte.MaxValue); public static readonly Color RosyBrown = FromRgba(188, 143, 143, byte.MaxValue); public static readonly Color RoyalBlue = FromRgba(65, 105, 225, byte.MaxValue); public static readonly Color SaddleBrown = FromRgba(139, 69, 19, byte.MaxValue); public static readonly Color Salmon = FromRgba(250, 128, 114, byte.MaxValue); public static readonly Color SandyBrown = FromRgba(244, 164, 96, byte.MaxValue); public static readonly Color SeaGreen = FromRgba(46, 139, 87, byte.MaxValue); public static readonly Color SeaShell = FromRgba(byte.MaxValue, 245, 238, byte.MaxValue); public static readonly Color Sienna = FromRgba(160, 82, 45, byte.MaxValue); public static readonly Color Silver = FromRgba(192, 192, 192, byte.MaxValue); public static readonly Color SkyBlue = FromRgba(135, 206, 235, byte.MaxValue); public static readonly Color SlateBlue = FromRgba(106, 90, 205, byte.MaxValue); public static readonly Color SlateGray = FromRgba(112, 128, 144, byte.MaxValue); public static readonly Color SlateGrey = SlateGray; public static readonly Color Snow = FromRgba(byte.MaxValue, 250, 250, byte.MaxValue); public static readonly Color SpringGreen = FromRgba(0, byte.MaxValue, 127, byte.MaxValue); public static readonly Color SteelBlue = FromRgba(70, 130, 180, byte.MaxValue); public static readonly Color Tan = FromRgba(210, 180, 140, byte.MaxValue); public static readonly Color Teal = FromRgba(0, 128, 128, byte.MaxValue); public static readonly Color Thistle = FromRgba(216, 191, 216, byte.MaxValue); public static readonly Color Tomato = FromRgba(byte.MaxValue, 99, 71, byte.MaxValue); public static readonly Color Transparent = FromRgba(0, 0, 0, 0); public static readonly Color Turquoise = FromRgba(64, 224, 208, byte.MaxValue); public static readonly Color Violet = FromRgba(238, 130, 238, byte.MaxValue); public static readonly Color Wheat = FromRgba(245, 222, 179, byte.MaxValue); public static readonly Color White = FromRgba(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); public static readonly Color WhiteSmoke = FromRgba(245, 245, 245, byte.MaxValue); public static readonly Color Yellow = FromRgba(byte.MaxValue, byte.MaxValue, 0, byte.MaxValue); public static readonly Color YellowGreen = FromRgba(154, 205, 50, byte.MaxValue); private static readonly Lazy<Color[]> WebSafePaletteLazy = new Lazy<Color[]>(CreateWebSafePalette, isThreadSafe: true); private static readonly Lazy<Color[]> WernerPaletteLazy = new Lazy<Color[]>(CreateWernerPalette, isThreadSafe: true); public static ReadOnlyMemory<Color> WebSafePalette => WebSafePaletteLazy.Value; public static ReadOnlyMemory<Color> WernerPalette => WernerPaletteLazy.Value; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Rgba64 pixel) { data = pixel; boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Rgb48 pixel) { data = new Rgba64(pixel.R, pixel.G, pixel.B, ushort.MaxValue); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(La32 pixel) { data = new Rgba64(pixel.L, pixel.L, pixel.L, pixel.A); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(L16 pixel) { data = new Rgba64(pixel.PackedValue, pixel.PackedValue, pixel.PackedValue, ushort.MaxValue); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Rgba32 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Argb32 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Bgra32 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Abgr32 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Rgb24 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Bgr24 pixel) { data = new Rgba64(pixel); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color(Vector4 vector) { vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One); boxedHighPrecisionPixel = new RgbaVector(vector.X, vector.Y, vector.Z, vector.W); data = default(Rgba64); } public static explicit operator Vector4(Color color) { return color.ToVector4(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator Color(Vector4 source) { return new Color(source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Rgba32 ToRgba32() { if (boxedHighPrecisionPixel == null) { return data.ToRgba32(); } Rgba32 dest = default(Rgba32); boxedHighPrecisionPixel.ToRgba32(ref dest); return dest; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Bgra32 ToBgra32() { if (boxedHighPrecisionPixel == null) { return data.ToBgra32(); } Bgra32 result = default(Bgra32); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Argb32 ToArgb32() { if (boxedHighPrecisionPixel == null) { return data.ToArgb32(); } Argb32 result = default(Argb32); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Abgr32 ToAbgr32() { if (boxedHighPrecisionPixel == null) { return data.ToAbgr32(); } Abgr32 result = default(Abgr32); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Rgb24 ToRgb24() { if (boxedHighPrecisionPixel == null) { return data.ToRgb24(); } Rgb24 result = default(Rgb24); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Bgr24 ToBgr24() { if (boxedHighPrecisionPixel == null) { return data.ToBgr24(); } Bgr24 result = default(Bgr24); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Vector4 ToVector4() { if (boxedHighPrecisionPixel == null) { return data.ToScaledVector4(); } return boxedHighPrecisionPixel.ToScaledVector4(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private Color(byte r, byte g, byte b, byte a) { data = new Rgba64(ColorNumerics.UpscaleFrom8BitTo16Bit(r), ColorNumerics.UpscaleFrom8BitTo16Bit(g), ColorNumerics.UpscaleFrom8BitTo16Bit(b), ColorNumerics.UpscaleFrom8BitTo16Bit(a)); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private Color(byte r, byte g, byte b) { data = new Rgba64(ColorNumerics.UpscaleFrom8BitTo16Bit(r), ColorNumerics.UpscaleFrom8BitTo16Bit(g), ColorNumerics.UpscaleFrom8BitTo16Bit(b), ushort.MaxValue); boxedHighPrecisionPixel = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private Color(IPixel pixel) { boxedHighPrecisionPixel = pixel; data = default(Rgba64); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Color left, Color right) { return left.Equals(right); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(Color left, Color right) { return !left.Equals(right); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Color FromRgba(byte r, byte g, byte b, byte a) { return new Color(r, g, b, a); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Color FromRgb(byte r, byte g, byte b) { return new Color(r, g, b); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Color FromPixel<TPixel>(TPixel pixel) where TPixel : unmanaged, IPixel<TPixel> { if (typeof(TPixel) == typeof(Rgba64)) { return new Color((Rgba64)(object)pixel); } if (typeof(TPixel) == typeof(Rgb48)) { return new Color((Rgb48)(object)pixel); } if (typeof(TPixel) == typeof(La32)) { return new Color((La32)(object)pixel); } if (typeof(TPixel) == typeof(L16)) { return new Color((L16)(object)pixel); } if (System.Runtime.CompilerServices.Unsafe.SizeOf<TPixel>() <= System.Runtime.CompilerServices.Unsafe.SizeOf<Rgba32>()) { Rgba32 dest = default(Rgba32); pixel.ToRgba32(ref dest); return new Color(dest); } return new Color(pixel); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Color ParseHex(string hex) { return new Color(Rgba32.ParseHex(hex)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryParseHex(string hex, out Color result) { result = default(Color); if (Rgba32.TryParseHex(hex, out var result2)) { result = new Color(result2); return true; } return false; } public static Color Parse(string input) { Guard.NotNull(input, "input"); if (!TryParse(input, out var result)) { throw new ArgumentException("Input string is not in the correct format.", "input"); } return result; } public static bool TryParse(string input, out Color result) { result = default(Color); if (string.IsNullOrWhiteSpace(input)) { return false; } if (NamedColorsLookupLazy.Value.TryGetValue(input, out result)) { return true; } return TryParseHex(input, out result); } public Color WithAlpha(float alpha) { Vector4 vector = (Vector4)this; vector.W = alpha; return new Color(vector); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ToHex() { return data.ToRgba32().ToHex(); } public override string ToString() { return ToHex(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public TPixel ToPixel<TPixel>() where TPixel : unmanaged, IPixel<TPixel> { IPixel pixel = boxedHighPrecisionPixel; if (pixel is TPixel) { return (TPixel)pixel; } TPixel result; if (boxedHighPrecisionPixel == null) { result = default(TPixel); result.FromRgba64(data); return result; } result = default(TPixel); result.FromScaledVector4(boxedHighPrecisionPixel.ToScaledVector4()); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ToPixel<TPixel>(Configuration configuration, ReadOnlySpan<Color> source, Span<TPixel> destination) where TPixel : unmanaged, IPixel<TPixel> { Guard.DestinationShouldNotBeTooShort(source, destination, "destination"); for (int i = 0; i < source.Length; i++) { destination[i] = source[i].ToPixel<TPixel>(); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Color other) { if (boxedHighPrecisionPixel == null && other.boxedHighPrecisionPixel == null) { return data.PackedValue == other.data.PackedValue; } return boxedHighPrecisionPixel?.Equals(other.boxedHighPrecisionPixel) ?? false; } public override bool Equals(object obj) { if (obj is Color other) { return Equals(other); } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() { if (boxedHighPrecisionPixel == null) { return data.PackedValue.GetHashCode(); } return boxedHighPrecisionPixel.GetHashCode(); } private static Dictionary<string, Color> CreateNamedColorsLookup() { return new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase) { { "AliceBlue", AliceBlue }, { "AntiqueWhite", AntiqueWhite }, { "Aqua", Aqua }, { "Aquamarine", Aquamarine }, { "Azure", Azure }, { "Beige", Beige }, { "Bisque", Bisque }, { "Black", Black }, { "BlanchedAlmond", BlanchedAlmond }, { "Blue", Blue }, { "BlueViolet", BlueViolet }, { "Brown", Brown }, { "BurlyWood", BurlyWood }, { "CadetBlue", CadetBlue }, { "Chartreuse", Chartreuse }, { "Chocolate", Chocolate }, { "Coral", Coral }, { "CornflowerBlue", CornflowerBlue }, { "Cornsilk", Cornsilk }, { "Crimson", Crimson }, { "Cyan", Cyan }, { "DarkBlue", DarkBlue }, { "DarkCyan", DarkCyan }, { "DarkGoldenrod", DarkGoldenrod }, { "DarkGray", DarkGray }, { "DarkGreen", DarkGreen }, { "DarkGrey", DarkGrey }, { "DarkKhaki", DarkKhaki }, { "DarkMagenta", DarkMagenta }, { "DarkOliveGreen", DarkOliveGreen }, { "DarkOrange", DarkOrange }, { "DarkOrchid", DarkOrchid }, { "DarkRed", DarkRed }, { "DarkSalmon", DarkSalmon }, { "DarkSeaGreen", DarkSeaGreen }, { "DarkSlateBlue", DarkSlateBlue }, { "DarkSlateGray", DarkSlateGray }, { "DarkSlateGrey", DarkSlateGrey }, { "DarkTurquoise", DarkTurquoise }, { "DarkViolet", DarkViolet }, { "DeepPink", DeepPink }, { "DeepSkyBlue", DeepSkyBlue }, { "DimGray", DimGray }, { "DimGrey", DimGrey }, { "DodgerBlue", DodgerBlue }, { "Firebrick", Firebrick }, { "FloralWhite", FloralWhite }, { "ForestGreen", ForestGreen }, { "Fuchsia", Fuchsia }, { "Gainsboro", Gainsboro }, { "GhostWhite", GhostWhite }, { "Gold", Gold }, { "Goldenrod", Goldenrod }, { "Gray", Gray }, { "Green", Green }, { "GreenYellow", GreenYellow }, { "Grey", Grey }, { "Honeydew", Honeydew }, { "HotPink", HotPink }, { "IndianRed", IndianRed }, { "Indigo", Indigo }, { "Ivory", Ivory }, { "Khaki", Khaki }, { "Lavender", Lavender }, { "LavenderBlush", LavenderBlush }, { "LawnGreen", LawnGreen }, { "LemonChiffon", LemonChiffon }, { "LightBlue", LightBlue }, { "LightCoral", LightCoral }, { "LightCyan", LightCyan }, { "LightGoldenrodYellow", LightGoldenrodYellow }, { "LightGray", LightGray }, { "LightGreen", LightGreen }, { "LightGrey", LightGrey }, { "LightPink", LightPink }, { "LightSalmon", LightSalmon }, { "LightSeaGreen", LightSeaGreen }, { "LightSkyBlue", LightSkyBlue }, { "LightSlateGray", LightSlateGray }, { "LightSlateGrey", LightSlateGrey }, { "LightSteelBlue", LightSteelBlue }, { "LightYellow", LightYellow }, { "Lime", Lime }, { "LimeGreen", LimeGreen }, { "Linen", Linen }, { "Magenta", Magenta }, { "Maroon", Maroon }, { "MediumAquamarine", MediumAquamarine }, { "MediumBlue", MediumBlue }, { "MediumOrchid", MediumOrchid }, { "MediumPurple", MediumPurple }, { "MediumSeaGreen", MediumSeaGreen }, { "MediumSlateBlue", MediumSlateBlue }, { "MediumSpringGreen", MediumSpringGreen }, { "MediumTurquoise", MediumTurquoise }, { "MediumVioletRed", MediumVioletRed }, { "MidnightBlue", MidnightBlue }, { "MintCream", MintCream }, { "MistyRose", MistyRose }, { "Moccasin", Moccasin }, { "NavajoWhite", NavajoWhite }, { "Navy", Navy }, { "OldLace", OldLace }, { "Olive", Olive }, { "OliveDrab", OliveDrab }, { "Orange", Orange }, { "OrangeRed", OrangeRed }, { "Orchid", Orchid }, { "PaleGoldenrod", PaleGoldenrod }, { "PaleGreen", PaleGreen }, { "PaleTurquoise", PaleTurquoise }, { "PaleVioletRed", PaleVioletRed }, { "PapayaWhip", PapayaWhip }, { "PeachPuff", PeachPuff }, { "Peru", Peru }, { "Pink", Pink }, { "Plum", Plum }, { "PowderBlue", PowderBlue }, { "Purple", Purple }, { "RebeccaPurple", RebeccaPurple }, { "Red", Red }, { "RosyBrown", RosyBrown }, { "RoyalBlue", RoyalBlue }, { "SaddleBrown", SaddleBrown }, { "Salmon", Salmon }, { "SandyBrown", SandyBrown }, { "SeaGreen", SeaGreen }, { "SeaShell", SeaShell }, { "Sienna", Sienna }, { "Silver", Silver }, { "SkyBlue", SkyBlue }, { "SlateBlue", SlateBlue }, { "SlateGray", SlateGray }, { "SlateGrey", SlateGrey }, { "Snow", Snow }, { "SpringGreen", SpringGreen }, { "SteelBlue", SteelBlue }, { "Tan", Tan }, { "Teal", Teal }, { "Thistle", Thistle }, { "Tomato", Tomato }, { "Transparent", Transparent }, { "Turquoise", Turquoise }, { "Violet", Violet }, { "Wheat", Wheat }, { "White", White }, { "WhiteSmoke", WhiteSmoke }, { "Yellow", Yellow }, { "YellowGreen", YellowGreen } }; } private static Color[] CreateWebSafePalette() { return new Color[142] { AliceBlue, AntiqueWhite, Aqua, Aquamarine, Azure, Beige, Bisque, Black, BlanchedAlmond, Blue, BlueViolet, Brown, BurlyWood, CadetBlue, Chartreuse, Chocolate, Coral, CornflowerBlue, Cornsilk, Crimson, Cyan, DarkBlue, DarkCyan, DarkGoldenrod, DarkGray, DarkGreen, DarkKhaki, DarkMagenta, DarkOliveGreen, DarkOrange, DarkOrchid, DarkRed, DarkSalmon, DarkSeaGreen, DarkSlateBlue, DarkSlateGray, DarkTurquoise, DarkViolet, DeepPink, DeepSkyBlue, DimGray, DodgerBlue, Firebrick, FloralWhite, ForestGreen, Fuchsia, Gainsboro, GhostWhite, Gold, Goldenrod, Gray, Green, GreenYellow, Honeydew, HotPink, IndianRed, Indigo, Ivory, Khaki, Lavender, LavenderBlush, LawnGreen, LemonChiffon, LightBlue, LightCoral, LightCyan, LightGoldenrodYellow, LightGray, LightGreen, LightPink, LightSalmon, LightSeaGreen, LightSkyBlue, LightSlateGray, LightSteelBlue, LightYellow, Lime, LimeGreen, Linen, Magenta, Maroon, MediumAquamarine, MediumBlue, MediumOrchid, MediumPurple, MediumSeaGreen, MediumSlateBlue, MediumSpringGreen, MediumTurquoise, MediumVioletRed, MidnightBlue, MintCream, MistyRose, Moccasin, NavajoWhite, Navy, OldLace, Olive, OliveDrab, Orange, OrangeRed, Orchid, PaleGoldenrod, PaleGreen, PaleTurquoise, PaleVioletRed, PapayaWhip, PeachPuff, Peru, Pink, Plum, PowderBlue, Purple, RebeccaPurple, Red, RosyBrown, RoyalBlue, SaddleBrown, Salmon, SandyBrown, SeaGreen, SeaShell, Sienna, Silver, SkyBlue, SlateBlue, SlateGray, Snow, SpringGreen, SteelBlue, Tan, Teal, Thistle, Tomato, Transparent, Turquoise, Violet, Wheat, White, WhiteSmoke, Yellow, YellowGreen }; } private static Color[] CreateWernerPalette() { return new Color[110] { ParseHex("#f1e9cd"), ParseHex("#f2e7cf"), ParseHex("#ece6d0"), ParseHex("#f2eacc"), ParseHex("#f3e9ca"), ParseHex("#f2ebcd"), ParseHex("#e6e1c9"), ParseHex("#e2ddc6"), ParseHex("#cbc8b7"), ParseHex("#bfbbb0"), ParseHex("#bebeb3"), ParseHex("#b7b5ac"), ParseHex("#bab191"), ParseHex("#9c9d9a"), ParseHex("#8a8d84"), ParseHex("#5b5c61"), ParseHex("#555152"), ParseHex("#413f44"), ParseHex("#454445"), ParseHex("#423937"), ParseHex("#433635"), ParseHex("#252024"), ParseHex("#241f20"), ParseHex("#281f3f"), ParseHex("#1c1949"), ParseHex("#4f638d"), ParseHex("#383867"), ParseHex("#5c6b8f"), ParseHex("#657abb"), ParseHex("#6f88af"), ParseHex("#7994b5"), ParseHex("#6fb5a8"), ParseHex("#719ba2"), ParseHex("#8aa1a6"), ParseHex("#d0d5d3"), ParseHex("#8590ae"), ParseHex("#3a2f52"), ParseHex("#39334a"), ParseHex("#6c6d94"), ParseHex("#584c77"), ParseHex("#533552"), ParseHex("#463759"), ParseHex("#bfbac0"), ParseHex("#77747f"), ParseHex("#4a475c"), ParseHex("#b8bfaf"), ParseHex("#b2b599"), ParseHex("#979c84"), ParseHex("#5d6161"), ParseHex("#61ac86"), ParseHex("#a4b6a7"), ParseHex("#adba98"), ParseHex("#93b778"), ParseHex("#7d8c55"), ParseHex("#33431e"), ParseHex("#7c8635"), ParseHex("#8e9849"), ParseHex("#c2c190"), ParseHex("#67765b"), ParseHex("#ab924b"), ParseHex("#c8c76f"), ParseHex("#ccc050"), ParseHex("#ebdd99"), ParseHex("#ab9649"), ParseHex("#dbc364"), ParseHex("#e6d058"), ParseHex("#ead665"), ParseHex("#d09b2c"), ParseHex("#a36629"), ParseHex("#a77d35"), ParseHex("#f0d696"), ParseHex("#d7c485"), ParseHex("#f1d28c"), ParseHex("#efcc83"), ParseHex("#f3daa7"), ParseHex("#dfa837"), ParseHex("#ebbc71"), ParseHex("#d17c3f"), ParseHex("#92462f"), ParseHex("#be7249"), ParseHex("#bb603c"), ParseHex("#c76b4a"), ParseHex("#a75536"), ParseHex("#b63e36"), ParseHex("#b5493a"), ParseHex("#cd6d57"), ParseHex("#711518"), ParseHex("#e9c49d"), ParseHex("#eedac3"), ParseHex("#eecfbf"), ParseHex("#ce536b"), ParseHex("#b74a70"), ParseHex("#b7757c"), ParseHex("#612741"), ParseHex("#7a4848"), ParseHex("#3f3033"), ParseHex("#8d746f"), ParseHex("#4d3635"), ParseHex("#6e3b31"), ParseHex("#864735"), ParseHex("#553d3a"), ParseHex("#613936"), ParseHex("#7a4b3a"), ParseHex("#946943"), ParseHex("#c39e6d"), ParseHex("#513e32"), ParseHex("#8b7859"), ParseHex("#9b856b"), ParseHex("#766051"), ParseHex("#453b32") }; } } public enum ByteOrder { BigEndian, LittleEndian } internal static class Constants { public static readonly float Epsilon = 0.001f; public static readonly float EpsilonSquared = Epsilon * Epsilon; } public class ImageFormatException : Exception { internal ImageFormatException(string errorMessage) : base(errorMessage) { } internal ImageFormatException(string errorMessage, Exception innerException) : base(errorMessage, innerException) { } } public sealed class ImageProcessingException : Exception { public ImageProcessingException() { } public ImageProcessingException(string errorMessage) : base(errorMessage) { } public ImageProcessingException(string errorMessage, Exception innerException) : base(errorMessage, innerException) { } } public sealed class InvalidImageContentException : ImageFormatException { public InvalidImageContentException(string errorMessage) : base(errorMessage) { } public InvalidImageContentException(string errorMessage, Exception innerException) : base(errorMessage, innerException) { } internal InvalidImageContentException(Size size, InvalidMemoryOperationException memoryException) : this($"Cannot decode image. Failed to allocate buffers for possibly degenerate dimensions: {size.Width}x{size.Height}.", memoryException) { } } public sealed class UnknownImageFormatException : ImageFormatException { public UnknownImageFormatException(string errorMessage) : base(errorMessage) { } } internal static class ConfigurationExtensions { public static ParallelOptions GetParallelOptions(this Configuration configuration) { return new ParallelOptions { MaxDegreeOfParallelism = configuration.MaxDegreeOfParallelism }; } } internal static class EncoderExtensions { public unsafe static string GetString(this Encoding encoding, ReadOnlySpan<byte> buffer) { if (buffer.Length == 0) { return string.Empty; } fixed (byte* bytes = buffer) { return encoding.GetString(bytes, buffer.Length); } } } internal static class EnumerableExtensions { [CompilerGenerated] private sealed class <RangeIterator>d__1 : IEnumerable<int>, IEnumerable, IEnumerator<int>, IDisposable, IEnumerator { private int <>1__state; private int <>2__current; private int <>l__initialThreadId; private int fromInclusive; public int <>3__fromInclusive; private int step; public int <>3__step; private Func<int, bool> toDelegate; public Func<int, bool> <>3__toDelegate; private int <i>5__2; int IEnumerator<int>.Current { [DebuggerHidden] get { return <>2__current; } } object IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } [DebuggerHidden] public <RangeIterator>d__1(int <>1__state) { this.<>1__state = <>1__state; <>l__initialThreadId = Environment.CurrentManagedThreadId; } [DebuggerHidden] void IDisposable.Dispose() { <>1__state = -2; } private bool MoveNext() { switch (<>1__state) { default: return false; case 0: <>1__state = -1; <i>5__2 = fromInclusive; break; case 1: <>1__state = -1; <i>5__2 += step; break; } if (toDelegate(<i>5__2)) { <>2__current = <i>5__2; <>1__state = 1; return true; } return false; } bool IEnumerator.MoveNext() { //ILSpy generated this explicit interface implementation from .override directive in MoveNext return this.MoveNext(); } [DebuggerHidden] void IEnumerator.Reset() { throw new NotSupportedException(); } [DebuggerHidden] IEnumerator<int> IEnumerable<int>.GetEnumerator() { <RangeIterator>d__1 <RangeIterator>d__; if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId) { <>1__state = 0; <RangeIterator>d__ = this; } else { <RangeIterator>d__ = new <RangeIterator>d__1(0); } <RangeIterator>d__.fromInclusive = <>3__fromInclusive; <RangeIterator>d__.toDelegate = <>3__toDelegate; <RangeIterator>d__.step = <>3__step; return <RangeIterator>d__; } [DebuggerHidden] IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable<int>)this).GetEnumerator(); } } public static IEnumerable<int> SteppedRange(int fromInclusive, Func<int, bool> toDelegate, int step) { return RangeIterator(fromInclusive, toDelegate, step); } [IteratorStateMachine(typeof(<RangeIterator>d__1))] private static IEnumerable<int> RangeIterator(int fromInclusive, Func<int, bool> toDelegate, int step) { //yield-return decompiler failed: Unexpected instruction in Iterator.Dispose() return new <RangeIterator>d__1(-2) { <>3__fromInclusive = fromInclusive, <>3__toDelegate = toDelegate, <>3__step = step }; } } internal static class StreamExtensions { public static void Write(this Stream stream, Span<byte> buffer, int offset, int count) { Write(stream, buffer.Slice(offset, count)); } public static int Read(this Stream stream, Span<byte> buffer, int offset, int count) { return Read(stream, buffer.Slice(offset, count)); } public static void Skip(this Stream stream, int count) { if (count < 1) { return; } if (stream.CanSeek) { stream.Seek(count, SeekOrigin.Current); return; } byte[] array = ArrayPool<byte>.Shared.Rent(count); try { while (count > 0) { int num = stream.Read(array, 0, count); if (num == 0) { break; } count -= num; } } finally { ArrayPool<byte>.Shared.Return(array); } } public static int Read(this Stream stream, Span<byte> buffer) { byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length); try { int num = stream.Read(array, 0, buffer.Length); if ((uint)num > (uint)buffer.Length) { throw new IOException("Stream was too long."); } new Span<byte>(array, 0, num).CopyTo(buffer); return num; } finally { ArrayPool<byte>.Shared.Return(array); } } public static void Write(this Stream stream, ReadOnlySpan<byte> buffer) { byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length); try { buffer.CopyTo(array); stream.Write(array, 0, buffer.Length); } finally { ArrayPool<byte>.Shared.Return(array); } } } internal static class ColorNumerics { private static readonly Vector4 Bt709 = new Vector4(0.2126f, 0.7152f, 0.0722f, 0f); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetBT709Luminance(ref Vector4 vector, int luminanceLevels) { return (int)MathF.Round(Vector4.Dot(vector, Bt709) * (float)(luminanceLevels - 1)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte Get8BitBT709Luminance(byte r, byte g, byte b) { return (byte)((float)(int)r * 0.2126f + (float)(int)g * 0.7152f + (float)(int)b * 0.0722f + 0.5f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ushort Get16BitBT709Luminance(ushort r, ushort g, ushort b) { return (ushort)((float)(int)r * 0.2126f + (float)(int)g * 0.7152f + (float)(int)b * 0.0722f + 0.5f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ushort Get16BitBT709Luminance(float r, float g, float b) { return (ushort)(r * 0.2126f + g * 0.7152f + b * 0.0722f + 0.5f); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte DownScaleFrom16BitTo8Bit(ushort component) { return (byte)(component * 255 + 32895 >> 16); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ushort UpscaleFrom8BitTo16Bit(byte component) { return (ushort)(component * 257); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetBitsNeededForColorDepth(int colors) { return Math.Max(1, (int)Math.Ceiling(Math.Log(colors, 2.0))); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetColorCountForBitDepth(int bitDepth) { return 1 << bitDepth; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Transform(ref Vector4 vector, ref ColorMatrix matrix) { float x = vector.X; float y = vector.Y; float z = vector.Z; float w = vector.W; vector.X = x * matrix.M11 + y * matrix.M21 + z * matrix.M31 + w * matrix.M41 + matrix.M51; vector.Y = x * matrix.M12 + y * matrix.M22 + z * matrix.M32 + w * matrix.M42 + matrix.M52; vector.Z = x * matrix.M13 + y * matrix.M23 + z * matrix.M33 + w * matrix.M43 + matrix.M53; vector.W = x * matrix.M14 + y * matrix.M24 + z * matrix.M34 + w * matrix.M44 + matrix.M54; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Transform(Span<Vector4> vectors, ref ColorMatrix matrix) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); for (int i = 0; i < vectors.Length; i++) { Transform(ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, i), ref matrix); } } } internal static class EnumUtils { public static TEnum Parse<TEnum>(int value, TEnum defaultValue) where TEnum : Enum { foreach (TEnum value2 in Enum.GetValues(typeof(TEnum))) { TEnum val2 = value2; if (value == System.Runtime.CompilerServices.Unsafe.As<TEnum, int>(ref val2)) { return value2; } } return defaultValue; } public static bool HasFlag<TEnum>(TEnum value, TEnum flag) where TEnum : Enum { uint num = System.Runtime.CompilerServices.Unsafe.As<TEnum, uint>(ref flag); return (System.Runtime.CompilerServices.Unsafe.As<TEnum, uint>(ref value) & num) == num; } } internal static class InliningOptions { public const MethodImplOptions AlwaysInline = MethodImplOptions.AggressiveInlining; public const MethodImplOptions HotPath = MethodImplOptions.AggressiveInlining; public const MethodImplOptions ShortMethod = MethodImplOptions.AggressiveInlining; public const MethodImplOptions ColdPath = MethodImplOptions.NoInlining; } internal static class Numerics { private static ReadOnlySpan<byte> Log2DeBruijn => new byte[32] { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GreatestCommonDivisor(int a, int b) { while (b != 0) { int num = b; b = a % b; a = num; } return a; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LeastCommonMultiple(int a, int b) { return a / GreatestCommonDivisor(a, b) * b; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Modulo2(int x) { return x & 1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Modulo4(int x) { return x & 3; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Modulo8(int x) { return x & 7; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int ModuloP2(int x, int m) { return x & (m - 1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Abs(int x) { int num = x >> 31; return (x ^ num) - num; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Pow2(float x) { return x * x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Pow3(float x) { return x * x * x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Gaussian(float x, float sigma) { float num = MathF.Sqrt((float)Math.PI * 2f) * sigma; float num2 = (0f - x) * x; float num3 = 2f * Pow2(sigma); float num4 = 1f / num; float num5 = MathF.Exp(num2 / num3); return num4 * num5; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float SinC(float f) { if (MathF.Abs(f) > Constants.Epsilon) { f *= (float)Math.PI; float num = MathF.Sin(f) / f; if (!(MathF.Abs(num) < Constants.Epsilon)) { return num; } return 0f; } return 1f; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte Clamp(byte value, byte min, byte max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint Clamp(uint value, uint min, uint max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Clamp(int value, int min, int max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float Clamp(float value, float min, float max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double Clamp(double value, double min, double max) { if (value > max) { return max; } if (value < min) { return min; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Clamp(Vector4 value, Vector4 min, Vector4 max) { return Vector4.Min(Vector4.Max(value, min), max); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<byte> span, byte min, byte max) { Span<byte> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref byte reference = ref MemoryMarshal.GetReference(span2); ref byte reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<byte>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<byte>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<byte>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<uint> span, uint min, uint max) { Span<uint> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref uint reference = ref MemoryMarshal.GetReference(span2); ref uint reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<uint>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<uint>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<uint>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<int> span, int min, int max) { Span<int> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref int reference = ref MemoryMarshal.GetReference(span2); ref int reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<int>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<int>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<int>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<float> span, float min, float max) { Span<float> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref float reference = ref MemoryMarshal.GetReference(span2); ref float reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<float>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<float>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<float>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Clamp(Span<double> span, double min, double max) { Span<double> span2 = span.Slice(ClampReduce(span, min, max)); if (span2.Length > 0) { ref double reference = ref MemoryMarshal.GetReference(span2); ref double reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<double>(ref reference, span2.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<double>(ref reference, ref reference2)) { reference = Clamp(reference, min, max); reference = ref System.Runtime.CompilerServices.Unsafe.Add<double>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int ClampReduce<T>(Span<T> span, T min, T max) where T : unmanaged { if (Vector.IsHardwareAccelerated && span.Length >= Vector<T>.Count) { int num = ModuloP2(span.Length, Vector<T>.Count); int num2 = span.Length - num; if (num2 > 0) { ClampImpl(span.Slice(0, num2), min, max); } return num2; } return 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void ClampImpl<T>(Span<T> span, T min, T max) where T : unmanaged { _ = ref MemoryMarshal.GetReference(span); Vector<T> left = new Vector<T>(min); Vector<T> right = new Vector<T>(max); int num = span.Length / Vector<T>.Count; int num2 = Modulo4(num); int num3 = num - num2; ref Vector<T> reference = ref System.Runtime.CompilerServices.Unsafe.As<T, Vector<T>>(ref MemoryMarshal.GetReference(span)); ref Vector<T> reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 1); ref Vector<T> reference3 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 2); ref Vector<T> reference4 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 3); ref Vector<T> reference5 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, num3); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector<T>>(ref reference, ref reference5)) { reference = Vector.Min(Vector.Max(left, reference), right); reference2 = Vector.Min(Vector.Max(left, reference2), right); reference3 = Vector.Min(Vector.Max(left, reference3), right); reference4 = Vector.Min(Vector.Max(left, reference4), right); reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 4); reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference2, 4); reference3 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference3, 4); reference4 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference4, 4); } if (num2 > 0) { reference = ref reference5; reference5 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference5, num2); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector<T>>(ref reference, ref reference5)) { reference = Vector.Min(Vector.Max(left, reference), right); reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector<T>>(ref reference, 1); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Premultiply(ref Vector4 source) { float w = source.W; source *= w; source.W = w; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void UnPremultiply(ref Vector4 source) { float w = source.W; source /= w; source.W = w; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Premultiply(Span<Vector4> vectors) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); ref Vector4 reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, vectors.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector4>(ref reference, ref reference2)) { Premultiply(ref reference); reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, 1); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void UnPremultiply(Span<Vector4> vectors) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); ref Vector4 reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, vectors.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector4>(ref reference, ref reference2)) { UnPremultiply(ref reference); reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, 1); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void CubePowOnXYZ(Span<Vector4> vectors) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); ref Vector4 reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, vectors.Length); while (System.Runtime.CompilerServices.Unsafe.IsAddressLessThan<Vector4>(ref reference, ref reference2)) { Vector4 vector = reference; float w = vector.W; vector = vector * vector * vector; vector.W = w; reference = vector; reference = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, 1); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void CubeRootOnXYZ(Span<Vector4> vectors) { ref Vector4 reference = ref MemoryMarshal.GetReference(vectors); ref Vector4 reference2 = ref System.Runtime.CompilerServices.Unsafe.Add<Vector4>(ref reference, vectors.Length); Vector4 vector2 = default(Vector4); while (System.Runtime.CompilerServices.Unsa