Moved Clamp extension method and added MathExtensions.cs

This commit is contained in:
miku-666
2023-07-17 19:10:48 +02:00
parent 99439c612a
commit a6b01eaa35
4 changed files with 25 additions and 13 deletions

View File

@@ -15,17 +15,10 @@ namespace PckStudio.Extensions
return new Vector4(color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f);
}
internal static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
{
if (value.CompareTo(min) < 0) return min;
if (value.CompareTo(max) > 0) return max;
return value;
}
internal static byte BlendValues(float source, float overlay, BlendMode blendType)
{
source = Clamp(source, 0.0f, 1.0f);
overlay = Clamp(overlay, 0.0f, 1.0f);
source = MathExtensions.Clamp(source, 0.0f, 1.0f);
overlay = MathExtensions.Clamp(overlay, 0.0f, 1.0f);
float resultValue = blendType switch
{
BlendMode.Add => source + overlay,
@@ -37,18 +30,18 @@ namespace PckStudio.Extensions
BlendMode.Screen => 1f - (1f - source) * (1f - overlay),
_ => 0.0f
};
return (byte)Clamp(resultValue * 255, 0, 255);
return (byte)MathExtensions.Clamp(resultValue * 255, 0, 255);
}
internal static byte Mix(double ratio, byte val1, byte val2)
{
ratio = Clamp(ratio, 0.0, 1.0);
ratio = MathExtensions.Clamp(ratio, 0.0, 1.0);
return (byte)(ratio * val1 + (1.0 - ratio) * val2);
}
internal static Color Mix(this Color c1, Color c2, double ratio)
{
ratio = Clamp(ratio, 0.0, 1.0);
ratio = MathExtensions.Clamp(ratio, 0.0, 1.0);
return Color.FromArgb(c1.A,
Mix(ratio, c1.R, c2.R),
Mix(ratio, c1.G, c2.G),

View File

@@ -210,7 +210,7 @@ namespace PckStudio.Extensions
internal static Image Interpolate(this Image image1, Image image2, double delta)
{
delta = ColorExtensions.Clamp(delta, 0.0, 1.0);
delta = MathExtensions.Clamp(delta, 0.0, 1.0);
if (image1 is not Bitmap baseImage || image2 is not Bitmap overlayImage ||
image1.Width != image2.Width || image1.Height != image2.Height)
return image1;

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PckStudio.Extensions
{
internal class MathExtensions
{
internal static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
{
if (value.CompareTo(min) < 0) return min;
if (value.CompareTo(max) > 0) return max;
return value;
}
}
}

View File

@@ -180,6 +180,7 @@
<Compile Include="Extensions\ImageLayoutDirection.cs" />
<Compile Include="Extensions\ImageSection.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Extensions\MathExtensions.cs" />
<Compile Include="Extensions\PckFileExtensions.cs" />
<Compile Include="Forms\Additional-Popups\NumericPrompt.cs">
<SubType>Form</SubType>