// ReSharper disable VirtualMemberNeverOverriden.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable VirtualMemberNeverOverridden.Global
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CUE.NET.ColorCorrection;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
using CUE.NET.Helper;
namespace CUE.NET.Brushes
{
///
/// Represents a basic brush.
///
public abstract class AbstractBrush : AbstractEffectTarget, IBrush
{
#region Properties & Fields
///
/// Gets or sets the calculation mode used for the rectangle/points used for color-selection in brushes.
///
public BrushCalculationMode BrushCalculationMode { get; set; } = BrushCalculationMode.Relative;
///
/// Gets or sets the overall percentage brightness of the brush.
///
public float Brightness { get; set; }
///
/// Gets or sets the overall percentage opacity of the brush.
///
public float Opacity { get; set; }
///
/// Gets a list of used to correct the colors of the brush.
///
public IList ColorCorrections { get; } = new List();
///
/// Gets the Rectangle used in the last render pass.
///
public RectangleF RenderedRectangle { get; protected set; }
///
/// Gets a dictionary containing all colors for points calculated in the last render pass.
///
public Dictionary RenderedTargets { get; } = new Dictionary();
///
/// Gets the strongly-typed target used for the effect.
///
protected override IBrush EffectTarget => this;
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The overall percentage brightness of the brush. (default: 1f)
/// The overall percentage opacity of the brush. (default: 1f)
protected AbstractBrush(float brightness = 1f, float opacity = 1f)
{
this.Brightness = brightness;
this.Opacity = opacity;
}
#endregion
#region Methods
///
/// Performas the render pass of the brush and calculates the raw colors for all requested points.
///
/// The rectangle in which the brush should be drawn.
/// The targets (keys/points) of which the color should be calculated.
public virtual void PerformRender(RectangleF rectangle, IEnumerable renderTargets)
{
RenderedRectangle = rectangle;
RenderedTargets.Clear();
foreach (BrushRenderTarget point in renderTargets)
RenderedTargets[point] = new CorsairColor(GetColorAtPoint(rectangle, point)); // Clone the color, we don't want to have reference issues here and brushes might return the same color multiple times!
}
///
/// Performs the finalize pass of the brush and calculates the final colors for all previously calculated points.
///
public virtual void PerformFinalize()
{
List renderTargets = RenderedTargets.Keys.ToList();
foreach (BrushRenderTarget renderTarget in renderTargets)
RenderedTargets[renderTarget] = FinalizeColor(RenderedTargets[renderTarget]); // Cloning here again shouldn't be needed since we did this above.
}
///
/// 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 abstract CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget);
///
/// Finalizes the color by appliing the overall brightness and opacity.
/// This method should always be the last call of a implementation.
/// If you overwrite this method please make sure that you never return the same color-object twice to prevent reference-issues!
///
/// The color to finalize.
/// The finalized color.
protected virtual CorsairColor FinalizeColor(CorsairColor color)
{
foreach (IColorCorrection colorCorrection in ColorCorrections)
colorCorrection.ApplyTo(color);
// Since we use HSV to calculate there is no way to make a color 'brighter' than 100%
// Be carefull with the naming: Since we use HSV the correct term is 'value' but outside we call it 'brightness'
// THIS IS NOT A HSB CALCULATION!!!
float finalBrightness = color.GetHSVValue() * (Brightness < 0 ? 0 : (Brightness > 1f ? 1f : Brightness));
byte finalAlpha = (byte)(color.A * (Opacity < 0 ? 0 : (Opacity > 1f ? 1f : Opacity)));
return ColorHelper.ColorFromHSV(color.GetHSVHue(), color.GetHSVSaturation(), finalBrightness, finalAlpha);
}
#endregion
}
}