// ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedAutoPropertyAccessor.Global // ReSharper disable UnusedMember.Global using System; using System.Drawing; using CUE.NET.Devices.Generic; namespace CUE.NET.Brushes { /// /// Represents a brush drawing an image. /// public class ImageBrush : AbstractBrush { #region Enums /// /// Contains a list of available image-scale modes. /// public enum ScaleMode { /// /// Stretches the image to fit inside the target rectangle. /// Stretch } /// /// Contains a list of available image-interpolation modes. /// public enum InterpolationMode { /// /// Selects the pixel closest to the target point. /// PixelPerfect } #endregion #region Properties & Fields /// /// Gets or sets the image drawn by the brush. If null it will default to full transparent. /// public Bitmap Image { get; set; } /// /// Gets or sets the used to scale the image if needed. /// public ScaleMode ImageScaleMode { get; set; } = ScaleMode.Stretch; /// /// Gets or sets the used to interpolate the image if needed. /// public InterpolationMode ImageInterpolationMode { get; set; } = InterpolationMode.PixelPerfect; #endregion #region Methods /// /// Gets the color at an specific point assuming the brush is drawn into the given rectangle. /// /// The rectangle in which the brush should be drawn. /// The target (key/point) from which the color should be taken. /// The color at the specified point. protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget) { if (Image == null || Image.Width == 0 || Image.Height == 0) return CorsairColor.Transparent; //TODO DarthAffe 16.03.2016: Refactor to allow more scale-/interpolation-modes float scaleX = Image.Width / rectangle.Width; float scaleY = Image.Height / rectangle.Height; int x = (int)(renderTarget.Point.X * scaleX); int y = (int)(renderTarget.Point.Y * scaleY); x = Math.Max(0, Math.Min(x, Image.Width - 1)); y = Math.Max(0, Math.Min(y, Image.Height - 1)); return Image.GetPixel(x, y); } #endregion } }