// ReSharper disable VirtualMemberNeverOverriden.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable VirtualMemberNeverOverridden.Global
using System.Collections.Generic;
using System.Linq;
namespace RGB.NET.Core
{
///
/// Represents a basic brush.
///
public abstract class AbstractBrush : AbstractEffectTarget, IBrush
{
#region Properties & Fields
///
public BrushCalculationMode BrushCalculationMode { get; set; } = BrushCalculationMode.Relative;
///
public double Brightness { get; set; }
///
public double Opacity { get; set; }
///
public IList ColorCorrections { get; } = new List();
///
public Rectangle RenderedRectangle { get; protected set; }
///
public Dictionary RenderedTargets { get; } = new Dictionary();
///
protected override IBrush EffectTarget => this;
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The overall percentage brightness of the brush. (default: 1.0)
/// The overall percentage opacity of the brush. (default: 1.0)
protected AbstractBrush(double brightness = 1, double opacity = 1)
{
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(Rectangle rectangle, IEnumerable renderTargets)
{
RenderedRectangle = rectangle;
RenderedTargets.Clear();
foreach (BrushRenderTarget point in renderTargets)
RenderedTargets[point] = new Color(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)
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 Color GetColorAtPoint(Rectangle 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 void FinalizeColor(Color 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!!!
color.Value *= Brightness <= 0 ? 0 : (Brightness >= 1.0 ? 1.0 : Brightness);
color.A = (byte)(color.A * (Opacity <= 0 ? 0 : (Opacity >= 1.0 ? 1.0 : Opacity)));
}
#endregion
}
}