using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using ComputeSharp.Win32;
namespace ComputeSharp.D2D1.Extensions;
///
/// Helper methods for and .
///
internal static class RECTExtensions
{
///
/// Modifies a target instance to be a D2D1 infinite rectangle.
///
/// The target instance to modify.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MakeD2D1Infinite(this ref RECT rect)
{
rect.left = int.MinValue;
rect.top = int.MinValue;
rect.right = int.MaxValue;
rect.bottom = int.MaxValue;
}
///
/// Creates a from a value.
///
/// The input value.
/// The resulting value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle ToRectangle(this in RECT rect)
{
int left = Math.Clamp(rect.left, int.MinValue / 2, int.MaxValue / 2);
int top = Math.Clamp(rect.top, int.MinValue / 2, int.MaxValue / 2);
int right = Math.Clamp(rect.right, int.MinValue / 2, int.MaxValue / 2);
int bottom = Math.Clamp(rect.bottom, int.MinValue / 2, int.MaxValue / 2);
int minX = Math.Min(left, right);
int minY = Math.Min(top, bottom);
int maxX = Math.Max(left, right);
int maxY = Math.Max(top, bottom);
return new(minX, minY, maxX - minX, maxY - minY);
}
///
/// Creates a from a value.
///
/// The input value.
/// The resulting value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RECT ToRECT(this in Rectangle rectangle)
{
if (rectangle.IsD2D1Infinite())
{
return new()
{
left = int.MinValue,
top = int.MinValue,
right = int.MaxValue,
bottom = int.MaxValue
};
}
return new()
{
left = rectangle.Left,
top = rectangle.Top,
right = rectangle.Right,
bottom = rectangle.Bottom
};
}
///
/// Creates a with the area being the union of two other values.
///
/// The first input instance to read.
/// The second input instance to read.
/// A with the area being the union of that of and .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RECT Union(this in RECT rect, in RECT other)
{
int left = Math.Min(rect.left, other.left);
int top = Math.Min(rect.top, other.top);
int right = Math.Max(rect.right, other.right);
int bottom = Math.Max(rect.bottom, other.bottom);
return new()
{
left = left,
top = top,
right = right,
bottom = bottom
};
}
}