diff --git a/RGB.NET.Presets/Extensions/HPPHExtensions.cs b/RGB.NET.Presets/Extensions/HPPHExtensions.cs new file mode 100644 index 0000000..363b9bc --- /dev/null +++ b/RGB.NET.Presets/Extensions/HPPHExtensions.cs @@ -0,0 +1,27 @@ +using HPPH; +using RGB.NET.Core; + +namespace RGB.NET.Presets.Extensions; + +/// +/// Offers some extensions related to HPPH. +/// +public static class HPPHExtensions +{ + /// + /// Converts the given HPPH to a RGB.NET . + /// + /// The color to convert. + /// The converted color. + public static Color ToColor(this IColor color) => new(color.A, color.R, color.G, color.B); + + /// + /// Converts the given HPPH to a RGB.NET . + /// + /// The color to convert. + /// The color-type of the HPPH color. + /// The converted color. + public static Color ToColor(this T color) + where T : struct, IColor + => new(color.A, color.R, color.G, color.B); +} \ No newline at end of file diff --git a/RGB.NET.Presets/RGB.NET.Presets.csproj b/RGB.NET.Presets/RGB.NET.Presets.csproj index f598320..d495956 100644 --- a/RGB.NET.Presets/RGB.NET.Presets.csproj +++ b/RGB.NET.Presets/RGB.NET.Presets.csproj @@ -57,6 +57,10 @@ + + + + diff --git a/RGB.NET.Presets/Textures/ImageTexture.cs b/RGB.NET.Presets/Textures/ImageTexture.cs new file mode 100644 index 0000000..fc05b74 --- /dev/null +++ b/RGB.NET.Presets/Textures/ImageTexture.cs @@ -0,0 +1,76 @@ +using System; +using HPPH; +using RGB.NET.Core; +using RGB.NET.Presets.Extensions; + +namespace RGB.NET.Presets.Textures; + +/// +/// +/// Represents a texture drawing an . +/// +public sealed class ImageTexture : ITexture +{ + #region Properties & Fields + + private readonly IImage _image; + + /// + public Size Size { get; } + + /// + public Color this[Point point] + { + get + { + int x = (int)MathF.Round((Size.Width - 1) * point.X.Clamp(0, 1)); + int y = (int)MathF.Round((Size.Height - 1) * point.Y.Clamp(0, 1)); + + return _image[x, y].ToColor(); + } + } + + /// + public Color this[Rectangle rectangle] + { + get + { + int x = (int)MathF.Round((Size.Width - 1) * rectangle.Location.X.Clamp(0, 1)); + int y = (int)MathF.Round((Size.Height - 1) * rectangle.Location.Y.Clamp(0, 1)); + int width = (int)MathF.Round(Size.Width * rectangle.Size.Width.Clamp(0, 1)); + int height = (int)MathF.Round(Size.Height * rectangle.Size.Height.Clamp(0, 1)); + + if ((width == 0) && (rectangle.Size.Width > 0)) width = 1; + if ((height == 0) && (rectangle.Size.Height > 0)) height = 1; + + return this[x, y, width, height]; + } + } + + /// + /// Gets the sampled color inside the specified region. + /// + /// The x-location of the region. + /// The y-location of the region. + /// The with of the region. + /// The height of the region. + /// The sampled color. + public Color this[int x, int y, int width, int height] => _image[x, y, width, height].Average().ToColor(); + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + /// The image represented by the texture. + public ImageTexture(IImage image) + { + this._image = image; + + Size = new Size(image.Width, image.Height); + } + + #endregion +} \ No newline at end of file diff --git a/RGB.NET.sln.DotSettings b/RGB.NET.sln.DotSettings index f0e61dd..a13f331 100644 --- a/RGB.NET.sln.DotSettings +++ b/RGB.NET.sln.DotSettings @@ -278,6 +278,7 @@ GEZ GSDK HID + HPPH HS HSV IBAN