Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API cleanup (related to #907) #911

Merged
merged 11 commits into from
May 23, 2019
11 changes: 5 additions & 6 deletions src/ImageSharp.Drawing/Processing/EllipticGradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace SixLabors.ImageSharp.Processing
/// a point on the longest extension of the ellipse and
/// the ratio between longest and shortest extension.
/// </summary>
public sealed class EllipticGradientBrush : GradientBrushBase
public sealed class EllipticGradientBrush : GradientBrush
{
private readonly PointF center;

private readonly PointF referenceAxisEnd;

private readonly float axisRatio;

/// <inheritdoc cref="GradientBrushBase" />
/// <inheritdoc cref="GradientBrush" />
/// <param name="center">The center of the elliptical gradient and 0 for the color stops.</param>
/// <param name="referenceAxisEnd">The end point of the reference axis of the ellipse.</param>
/// <param name="axisRatio">
Expand Down Expand Up @@ -60,7 +60,7 @@ public override BrushApplicator<TPixel> CreateApplicator<TPixel>(
this.RepetitionMode);

/// <inheritdoc />
private sealed class RadialGradientBrushApplicator<TPixel> : GradientBrushApplicatorBase<TPixel>
private sealed class RadialGradientBrushApplicator<TPixel> : GradientBrushApplicator<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private readonly PointF center;
Expand Down Expand Up @@ -149,8 +149,7 @@ private float AngleBetween(PointF junction, PointF a, PointF b)
{
var vA = a - junction;
var vB = b - junction;
return (float)(Math.Atan2(vB.Y, vB.X)
- Math.Atan2(vA.Y, vA.X));
return MathF.Atan2(vB.Y, vB.X) - MathF.Atan2(vA.Y, vA.X);
}

private float DistanceBetween(
Expand All @@ -162,7 +161,7 @@ private float DistanceBetween(

float dY = p1.Y - p2.Y;
float dYsquared = dY * dY;
return (float)Math.Sqrt(dXsquared + dYsquared);
return MathF.Sqrt(dXsquared + dYsquared);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace SixLabors.ImageSharp.Processing
/// <summary>
/// Base class for Gradient brushes
/// </summary>
public abstract class GradientBrushBase : IBrush
public abstract class GradientBrush : IBrush
{
/// <inheritdoc cref="IBrush"/>
/// <param name="repetitionMode">Defines how the colors are repeated beyond the interval [0..1]</param>
/// <param name="colorStops">The gradient colors.</param>
protected GradientBrushBase(
protected GradientBrush(
GradientRepetitionMode repetitionMode,
params ColorStop[] colorStops)
{
Expand Down Expand Up @@ -46,7 +46,7 @@ public abstract BrushApplicator<TPixel> CreateApplicator<TPixel>(
/// <summary>
/// Base class for gradient brush applicators
/// </summary>
internal abstract class GradientBrushApplicatorBase<TPixel> : BrushApplicator<TPixel>
internal abstract class GradientBrushApplicator<TPixel> : BrushApplicator<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private static readonly TPixel Transparent = Color.Transparent.ToPixel<TPixel>();
Expand All @@ -56,13 +56,13 @@ internal abstract class GradientBrushApplicatorBase<TPixel> : BrushApplicator<TP
private readonly GradientRepetitionMode repetitionMode;

/// <summary>
/// Initializes a new instance of the <see cref="GradientBrushApplicatorBase{TPixel}"/> class.
/// Initializes a new instance of the <see cref="GradientBrushApplicator{TPixel}"/> class.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="options">The options.</param>
/// <param name="colorStops">An array of color stops sorted by their position.</param>
/// <param name="repetitionMode">Defines if and how the gradient should be repeated.</param>
protected GradientBrushApplicatorBase(
protected GradientBrushApplicator(
ImageFrame<TPixel> target,
GraphicsOptions options,
ColorStop[] colorStops,
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp.Drawing/Processing/LinearGradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Processing
/// Supported right now:
/// - a set of colors in relative distances to each other.
/// </summary>
public sealed class LinearGradientBrush : GradientBrushBase
public sealed class LinearGradientBrush : GradientBrush
{
private readonly PointF p1;

Expand Down Expand Up @@ -53,7 +53,7 @@ public override BrushApplicator<TPixel> CreateApplicator<TPixel>(
/// <summary>
/// The linear gradient brush applicator.
/// </summary>
private sealed class LinearGradientBrushApplicator<TPixel> : GradientBrushApplicatorBase<TPixel>
private sealed class LinearGradientBrushApplicator<TPixel> : GradientBrushApplicator<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private readonly PointF start;
Expand Down Expand Up @@ -121,7 +121,7 @@ public LinearGradientBrushApplicator(

// some helpers:
this.alongsSquared = (this.alongX * this.alongX) + (this.alongY * this.alongY);
this.length = (float)Math.Sqrt(this.alongsSquared);
this.length = MathF.Sqrt(this.alongsSquared);
}

protected override float PositionOnGradient(float x, float y)
Expand All @@ -145,7 +145,7 @@ protected override float PositionOnGradient(float x, float y)
float y4 = y + (k * this.alongX);

// get distance from (x4,y4) to start
float distance = (float)Math.Sqrt(Math.Pow(x4 - this.start.X, 2) + Math.Pow(y4 - this.start.Y, 2));
float distance = MathF.Sqrt(MathF.Pow(x4 - this.start.X, 2) + MathF.Pow(y4 - this.start.Y, 2));

// get and return ratio
float ratio = distance / this.length;
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp.Drawing/Processing/RadialGradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace SixLabors.ImageSharp.Processing
/// <summary>
/// A Circular Gradient Brush, defined by center point and radius.
/// </summary>
public sealed class RadialGradientBrush : GradientBrushBase
public sealed class RadialGradientBrush : GradientBrush
{
private readonly PointF center;

private readonly float radius;

/// <inheritdoc cref="GradientBrushBase" />
/// <inheritdoc cref="GradientBrush" />
/// <param name="center">The center of the circular gradient and 0 for the color stops.</param>
/// <param name="radius">The radius of the circular gradient and 1 for the color stops.</param>
/// <param name="repetitionMode">Defines how the colors in the gradient are repeated.</param>
Expand Down Expand Up @@ -47,7 +47,7 @@ public override BrushApplicator<TPixel> CreateApplicator<TPixel>(
this.RepetitionMode);

/// <inheritdoc />
private sealed class RadialGradientBrushApplicator<TPixel> : GradientBrushApplicatorBase<TPixel>
private sealed class RadialGradientBrushApplicator<TPixel> : GradientBrushApplicator<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private readonly PointF center;
Expand Down Expand Up @@ -90,7 +90,7 @@ public override void Dispose()
/// <returns>the position on the color gradient.</returns>
protected override float PositionOnGradient(float x, float y)
{
float distance = (float)Math.Sqrt(Math.Pow(this.center.X - x, 2) + Math.Pow(this.center.Y - y, 2));
float distance = MathF.Sqrt(MathF.Pow(this.center.X - x, 2) + MathF.Pow(this.center.Y - y, 2));
return distance / this.radius;
}

Expand Down
48 changes: 2 additions & 46 deletions src/ImageSharp/Color/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,11 @@ private Color(byte r, byte g, byte b)
/// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax.
/// </param>
/// <returns>Returns a <see cref="Color"/> that represents the color defined by the provided RGBA hex string.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static Color FromHex(string hex)
{
Guard.NotNullOrWhiteSpace(hex, nameof(hex));

hex = ToRgbaHex(hex);

if (hex is null || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint packedValue))
{
throw new ArgumentException("Hexadecimal string is not in the correct format.", nameof(hex));
}
Rgba32 rgba = Rgba32.FromHex(hex);

var rgba = new Rgba32(BinaryPrimitives.ReverseEndianness(packedValue));
return new Color(rgba);
}

Expand Down Expand Up @@ -189,42 +182,5 @@ internal static void ToPixel<TPixel>(
ReadOnlySpan<Rgba64> rgba64Span = MemoryMarshal.Cast<Color, Rgba64>(source);
PixelOperations<TPixel>.Instance.FromRgba64(Configuration.Default, rgba64Span, destination);
}

/// <summary>
/// Converts the specified hex value to an rrggbbaa hex value.
/// </summary>
/// <param name="hex">The hex value to convert.</param>
/// <returns>
/// A rrggbbaa hex value.
/// </returns>
private static string ToRgbaHex(string hex)
{
if (hex[0] == '#')
{
hex = hex.Substring(1);
}

if (hex.Length == 8)
{
return hex;
}

if (hex.Length == 6)
{
return hex + "FF";
}

if (hex.Length < 3 || hex.Length > 4)
{
return null;
}

char r = hex[0];
char g = hex[1];
char b = hex[2];
char a = hex.Length == 3 ? 'F' : hex[3];

return new string(new[] { r, r, g, g, b, b, a, a });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public void Convert(ReadOnlySpan<YCbCr> source, Span<CieXyz> destination)
/// </summary>
/// <param name="workingSpace">The source working space</param>
/// <returns>The <see cref="LinearRgbToCieXyzConverter"/></returns>
private LinearRgbToCieXyzConverter GetLinearRgbToCieXyzConverter(RgbWorkingSpaceBase workingSpace)
private LinearRgbToCieXyzConverter GetLinearRgbToCieXyzConverter(RgbWorkingSpace workingSpace)
{
if (this.linearRgbToCieXyzConverter?.SourceWorkingSpace.Equals(workingSpace) == true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class ColorSpaceConverter
private readonly CieXyz targetLuvWhitePoint;
private readonly CieXyz targetLabWhitePoint;
private readonly CieXyz targetHunterLabWhitePoint;
private readonly RgbWorkingSpaceBase targetRgbWorkingSpace;
private readonly RgbWorkingSpace targetRgbWorkingSpace;
private readonly IChromaticAdaptation chromaticAdaptation;
private readonly bool performChromaticAdaptation;
private readonly CieXyzAndLmsConverter cieXyzAndLmsConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ColorSpaceConverterOptions
/// Gets or sets the target working space used *when creating* RGB colors. (RGB colors on the input already contain the working space information)
/// Defaults to: <see cref="Rgb.DefaultWorkingSpace"/>.
/// </summary>
public RgbWorkingSpaceBase TargetRgbWorkingSpace { get; set; } = Rgb.DefaultWorkingSpace;
public RgbWorkingSpace TargetRgbWorkingSpace { get; set; } = Rgb.DefaultWorkingSpace;

/// <summary>
/// Gets or sets the chromatic adaptation method used. When <value>null</value>, no adaptation will be performed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public CieXyzToLinearRgbConverter()
/// Initializes a new instance of the <see cref="CieXyzToLinearRgbConverter"/> class.
/// </summary>
/// <param name="workingSpace">The target working space.</param>
public CieXyzToLinearRgbConverter(RgbWorkingSpaceBase workingSpace)
public CieXyzToLinearRgbConverter(RgbWorkingSpace workingSpace)
{
this.TargetWorkingSpace = workingSpace;

Expand All @@ -38,7 +38,7 @@ public CieXyzToLinearRgbConverter(RgbWorkingSpaceBase workingSpace)
/// <summary>
/// Gets the target working space.
/// </summary>
public RgbWorkingSpaceBase TargetWorkingSpace { get; }
public RgbWorkingSpace TargetWorkingSpace { get; }

/// <summary>
/// Performs the conversion from the <see cref="CieXyz"/> input to an instance of <see cref="LinearRgb"/> type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal abstract class LinearRgbAndCieXyzConverterBase
/// </summary>
/// <param name="workingSpace">The Rgb working space.</param>
/// <returns>The <see cref="Matrix4x4"/> based on the chromaticity and working space.</returns>
public static Matrix4x4 GetRgbToCieXyzMatrix(RgbWorkingSpaceBase workingSpace)
public static Matrix4x4 GetRgbToCieXyzMatrix(RgbWorkingSpace workingSpace)
{
DebugGuard.NotNull(workingSpace, nameof(workingSpace));
RgbPrimariesChromaticityCoordinates chromaticity = workingSpace.ChromaticityCoordinates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public LinearRgbToCieXyzConverter()
/// Initializes a new instance of the <see cref="LinearRgbToCieXyzConverter"/> class.
/// </summary>
/// <param name="workingSpace">The target working space.</param>
public LinearRgbToCieXyzConverter(RgbWorkingSpaceBase workingSpace)
public LinearRgbToCieXyzConverter(RgbWorkingSpace workingSpace)
{
this.SourceWorkingSpace = workingSpace;
this.conversionMatrix = GetRgbToCieXyzMatrix(workingSpace);
Expand All @@ -34,7 +34,7 @@ public LinearRgbToCieXyzConverter(RgbWorkingSpaceBase workingSpace)
/// <summary>
/// Gets the source working space
/// </summary>
public RgbWorkingSpaceBase SourceWorkingSpace { get; }
public RgbWorkingSpace SourceWorkingSpace { get; }

/// <summary>
/// Performs the conversion from the <see cref="LinearRgb"/> input to an instance of <see cref="CieXyz"/> type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
{
/// <summary>
/// Represents the chromaticity coordinates of RGB primaries.
/// One of the specifiers of <see cref="RgbWorkingSpaceBase"/>.
/// One of the specifiers of <see cref="RgbWorkingSpace"/>.
/// </summary>
public readonly struct RgbPrimariesChromaticityCoordinates : IEquatable<RgbPrimariesChromaticityCoordinates>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
/// <summary>
/// The gamma working space.
/// </summary>
public sealed class GammaWorkingSpace : RgbWorkingSpaceBase
public sealed class GammaWorkingSpace : RgbWorkingSpace
{
/// <summary>
/// Initializes a new instance of the <see cref="GammaWorkingSpace" /> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
/// <summary>
/// L* working space.
/// </summary>
public sealed class LWorkingSpace : RgbWorkingSpaceBase
public sealed class LWorkingSpace : RgbWorkingSpace
{
/// <summary>
/// Initializes a new instance of the <see cref="LWorkingSpace" /> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
/// <summary>
/// Rec. 2020 (ITU-R Recommendation BT.2020F) working space.
/// </summary>
public sealed class Rec2020WorkingSpace : RgbWorkingSpaceBase
public sealed class Rec2020WorkingSpace : RgbWorkingSpace
{
/// <summary>
/// Initializes a new instance of the <see cref="Rec2020WorkingSpace" /> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
/// <summary>
/// Rec. 709 (ITU-R Recommendation BT.709) working space.
/// </summary>
public sealed class Rec709WorkingSpace : RgbWorkingSpaceBase
public sealed class Rec709WorkingSpace : RgbWorkingSpace
{
/// <summary>
/// Initializes a new instance of the <see cref="Rec709WorkingSpace" /> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
{
/// <summary>
/// Base class for all implementations of <see cref="RgbWorkingSpaceBase"/>.
/// Base class for all implementations of <see cref="RgbWorkingSpace"/>.
/// </summary>
public abstract class RgbWorkingSpaceBase
public abstract class RgbWorkingSpace
{
/// <summary>
/// Initializes a new instance of the <see cref="RgbWorkingSpaceBase"/> class.
/// Initializes a new instance of the <see cref="RgbWorkingSpace"/> class.
/// </summary>
/// <param name="referenceWhite">The reference white point.</param>
/// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
protected RgbWorkingSpaceBase(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates)
protected RgbWorkingSpace(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates)
{
this.WhitePoint = referenceWhite;
this.ChromaticityCoordinates = chromaticityCoordinates;
Expand Down Expand Up @@ -66,7 +66,7 @@ public override bool Equals(object obj)
return true;
}

if (obj is RgbWorkingSpaceBase other)
if (obj is RgbWorkingSpace other)
{
return this.WhitePoint.Equals(other.WhitePoint)
&& this.ChromaticityCoordinates.Equals(other.ChromaticityCoordinates);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation
/// <summary>
/// The sRgb working space.
/// </summary>
public sealed class SRgbWorkingSpace : RgbWorkingSpaceBase
public sealed class SRgbWorkingSpace : RgbWorkingSpace
{
/// <summary>
/// Initializes a new instance of the <see cref="SRgbWorkingSpace" /> class.
Expand Down
Loading