mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/PCK-Studio.git
synced 2026-05-26 15:27:07 +00:00
Moved Clamp extension method and added MathExtensions.cs
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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;
|
||||
|
||||
18
PCK-Studio/Extensions/MathExtensions.cs
Normal file
18
PCK-Studio/Extensions/MathExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user