Skip to content

Use Target Type new only when apparent #2958

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ csharp_style_deconstructed_variable_declaration = true:warning
csharp_style_prefer_index_operator = true:warning
csharp_style_prefer_range_operator = true:warning
csharp_style_implicit_object_creation_when_type_is_apparent = true:error
# ReSharper inspection severities
resharper_arrange_object_creation_when_type_evident_highlighting = error
resharper_arrange_object_creation_when_type_not_evident_highlighting = error
# "Null" checking preferences
csharp_style_throw_expression = true:warning
csharp_style_conditional_delegate_call = true:warning
Expand Down
2 changes: 1 addition & 1 deletion shared-infrastructure
4 changes: 2 additions & 2 deletions src/ImageSharp/Advanced/ParallelExecutionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ParallelExecutionSettings MultiplyMinimumPixelsPerTask(int multiplier)
{
Guard.MustBeGreaterThan(multiplier, 0, nameof(multiplier));

return new(
return new ParallelExecutionSettings(
this.MaxDegreeOfParallelism,
this.MinimumPixelsProcessedPerTask * multiplier,
this.MemoryAllocator);
Expand All @@ -92,6 +92,6 @@ public ParallelExecutionSettings MultiplyMinimumPixelsPerTask(int multiplier)
/// <returns>The <see cref="ParallelExecutionSettings"/>.</returns>
public static ParallelExecutionSettings FromConfiguration(Configuration configuration)
{
return new(configuration.MaxDegreeOfParallelism, configuration.MemoryAllocator);
return new ParallelExecutionSettings(configuration.MaxDegreeOfParallelism, configuration.MemoryAllocator);
}
}
4 changes: 2 additions & 2 deletions src/ImageSharp/Advanced/ParallelRowIterator.Wrappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void Invoke(int i)
}

int yMax = Math.Min(yMin + this.stepY, this.maxY);
RowInterval rows = new RowInterval(yMin, yMax);
RowInterval rows = new(yMin, yMax);

// Skip the safety copy when invoking a potentially impure method on a readonly field
Unsafe.AsRef(in this.operation).Invoke(in rows);
Expand Down Expand Up @@ -185,7 +185,7 @@ public void Invoke(int i)
}

int yMax = Math.Min(yMin + this.stepY, this.maxY);
RowInterval rows = new RowInterval(yMin, yMax);
RowInterval rows = new(yMin, yMax);

using IMemoryOwner<TBuffer> buffer = this.allocator.Allocate<TBuffer>(this.bufferLength);

Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Color/Color.WernerPalette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SixLabors.ImageSharp;
/// </content>
public partial struct Color
{
private static readonly Lazy<Color[]> WernerPaletteLazy = new Lazy<Color[]>(CreateWernerPalette, true);
private static readonly Lazy<Color[]> WernerPaletteLazy = new(CreateWernerPalette, true);

/// <summary>
/// Gets a collection of colors as defined in the original second edition of Werner’s Nomenclature of Colours 1821.
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/Color/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public static Color FromPixel<TPixel>(TPixel source)
PixelTypeInfo info = TPixel.GetPixelTypeInfo();
if (info.ComponentInfo.HasValue && info.ComponentInfo.Value.GetMaximumComponentPrecision() <= (int)PixelComponentBitDepth.Bit32)
{
return new(source.ToScaledVector4());
return new Color(source.ToScaledVector4());
}

return new(source);
return new Color(source);
}

/// <summary>
Expand Down Expand Up @@ -120,7 +120,7 @@ public static void FromPixel<TPixel>(ReadOnlySpan<TPixel> source, Span<Color> de
{
for (int i = 0; i < destination.Length; i++)
{
destination[i] = new(source[i]);
destination[i] = new Color(source[i]);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/ColorProfiles/CieLab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Vector4 ToScaledVector4()
v3 += this.AsVector3Unsafe();
v3 += new Vector3(0, 128F, 128F);
v3 /= new Vector3(100F, 255F, 255F);
return new(v3, 1F);
return new Vector4(v3, 1F);
}

/// <inheritdoc/>
Expand All @@ -97,7 +97,7 @@ public static CieLab FromScaledVector4(Vector4 source)
Vector3 v3 = source.AsVector3();
v3 *= new Vector3(100F, 255, 255);
v3 -= new Vector3(0, 128F, 128F);
return new(v3);
return new CieLab(v3);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -145,7 +145,7 @@ public static CieLab FromProfileConnectingSpace(ColorConversionOptions options,
float a = 500F * (fx - fy);
float b = 200F * (fy - fz);

return new(l, a, b);
return new CieLab(l, a, b);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -182,7 +182,7 @@ public CieXyz ToProfileConnectingSpace(ColorConversionOptions options)
Vector3 wxyz = new(whitePoint.X, whitePoint.Y, whitePoint.Z);
Vector3 xyzr = new(xr, yr, zr);

return new(xyzr * wxyz);
return new CieXyz(xyzr * wxyz);
}

/// <inheritdoc/>
Expand Down
10 changes: 5 additions & 5 deletions src/ImageSharp/ColorProfiles/CieLch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.ColorProfiles;
/// <param name="h">The hue in degrees.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CieLch(float l, float c, float h)
: this(new(l, c, h))
: this(new Vector3(l, c, h))
{
}

Expand Down Expand Up @@ -100,7 +100,7 @@ public Vector4 ToScaledVector4()
v3 += this.AsVector3Unsafe();
v3 += new Vector3(0, 200, 0);
v3 /= new Vector3(100, 400, 360);
return new(v3, 1F);
return new Vector4(v3, 1F);
}

/// <inheritdoc/>
Expand All @@ -109,7 +109,7 @@ public static CieLch FromScaledVector4(Vector4 source)
Vector3 v3 = source.AsVector3();
v3 *= new Vector3(100, 400, 360);
v3 -= new Vector3(0, 200, 0);
return new(v3, true);
return new CieLch(v3, true);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -155,7 +155,7 @@ public static CieLch FromProfileConnectingSpace(ColorConversionOptions options,
hDegrees += 360;
}

return new(l, c, hDegrees);
return new CieLch(l, c, hDegrees);
}

/// <inheritdoc/>
Expand All @@ -181,7 +181,7 @@ public CieLab ToProfileConnectingSpace(ColorConversionOptions options)
float a = c * MathF.Cos(hRadians);
float b = c * MathF.Sin(hRadians);

return new(l, a, b);
return new CieLab(l, a, b);
}

/// <inheritdoc/>
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/ColorProfiles/CieLchuv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.ColorProfiles;
/// <param name="h">The hue in degrees.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CieLchuv(float l, float c, float h)
: this(new(l, c, h))
: this(new Vector3(l, c, h))
{
}

Expand Down Expand Up @@ -97,7 +97,7 @@ public Vector4 ToScaledVector4()
v3 += this.AsVector3Unsafe();
v3 += new Vector3(0, 200, 0);
v3 /= new Vector3(100, 400, 360);
return new(v3, 1F);
return new Vector4(v3, 1F);
}

/// <inheritdoc/>
Expand All @@ -106,7 +106,7 @@ public static CieLchuv FromScaledVector4(Vector4 source)
Vector3 v3 = source.AsVector3();
v3 *= new Vector3(100, 400, 360);
v3 -= new Vector3(0, 200, 0);
return new(v3, true);
return new CieLchuv(v3, true);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -154,7 +154,7 @@ public static CieLchuv FromProfileConnectingSpace(ColorConversionOptions options
hDegrees += 360;
}

return new(l, c, hDegrees);
return new CieLchuv(l, c, hDegrees);
}

/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/ColorProfiles/CieLuv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static CieLuv FromProfileConnectingSpace(ColorConversionOptions options,
v = 0;
}

return new((float)l, (float)u, (float)v);
return new CieLuv((float)l, (float)u, (float)v);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -188,7 +188,7 @@ public CieXyz ToProfileConnectingSpace(ColorConversionOptions options)
z = 0;
}

return new((float)x, (float)y, (float)z);
return new CieXyz((float)x, (float)y, (float)z);
}

/// <inheritdoc/>
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/ColorProfiles/CieXyy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ public static CieXyy FromProfileConnectingSpace(ColorConversionOptions options,

if (float.IsNaN(x) || float.IsNaN(y))
{
return new(0, 0, source.Y);
return new CieXyy(0, 0, source.Y);
}

return new(x, y, source.Y);
return new CieXyy(x, y, source.Y);
}

/// <inheritdoc/>
Expand All @@ -144,14 +144,14 @@ public CieXyz ToProfileConnectingSpace(ColorConversionOptions options)
{
if (MathF.Abs(this.Y) < Constants.Epsilon)
{
return new(0, 0, this.Yl);
return new CieXyz(0, 0, this.Yl);
}

float x = (this.X * this.Yl) / this.Y;
float y = this.Yl;
float z = ((1 - this.X - this.Y) * y) / this.Y;

return new(x, y, z);
return new CieXyz(x, y, z);
}

/// <inheritdoc/>
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/ColorProfiles/CieXyz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal Vector4 ToVector4()
{
Vector3 v3 = default;
v3 += this.AsVector3Unsafe();
return new(v3, 1F);
return new Vector4(v3, 1F);
}

/// <inheritdoc/>
Expand All @@ -97,21 +97,21 @@ public Vector4 ToScaledVector4()
Vector3 v3 = default;
v3 += this.AsVector3Unsafe();
v3 *= 32768F / 65535;
return new(v3, 1F);
return new Vector4(v3, 1F);
}

internal static CieXyz FromVector4(Vector4 source)
{
Vector3 v3 = source.AsVector3();
return new(v3);
return new CieXyz(v3);
}

/// <inheritdoc/>
public static CieXyz FromScaledVector4(Vector4 source)
{
Vector3 v3 = source.AsVector3();
v3 *= 65535 / 32768F;
return new(v3);
return new CieXyz(v3);
}

/// <inheritdoc/>
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/ColorProfiles/Cmyk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.ColorProfiles;
/// <param name="k">The keyline black component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Cmyk(float c, float m, float y, float k)
: this(new(c, m, y, k))
: this(new Vector4(c, m, y, k))
{
}

Expand Down Expand Up @@ -138,12 +138,12 @@ public static Cmyk FromProfileConnectingSpace(ColorConversionOptions options, in

if (k.X >= 1F - Constants.Epsilon)
{
return new(0, 0, 0, 1F);
return new Cmyk(0, 0, 0, 1F);
}

cmy = (cmy - k) / (Vector3.One - k);

return new(cmy.X, cmy.Y, cmy.Z, k.X);
return new Cmyk(cmy.X, cmy.Y, cmy.Z, k.X);
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/ColorProfiles/ColorProfileConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ColorProfileConverter
/// Initializes a new instance of the <see cref="ColorProfileConverter"/> class.
/// </summary>
public ColorProfileConverter()
: this(new())
: this(new ColorConversionOptions())
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ internal static TTo ConvertUsingIccProfile<TFrom, TTo>(this ColorProfileConverte
ConversionParams sourceParams = new(converter.Options.SourceIccProfile, toPcs: true);
ConversionParams targetParams = new(converter.Options.TargetIccProfile, toPcs: false);

ColorProfileConverter pcsConverter = new(new()
ColorProfileConverter pcsConverter = new(new ColorConversionOptions
{
MemoryAllocator = converter.Options.MemoryAllocator,
SourceWhitePoint = new(converter.Options.SourceIccProfile.Header.PcsIlluminant),
TargetWhitePoint = new(converter.Options.TargetIccProfile.Header.PcsIlluminant),
SourceWhitePoint = new CieXyz(converter.Options.SourceIccProfile.Header.PcsIlluminant),
TargetWhitePoint = new CieXyz(converter.Options.TargetIccProfile.Header.PcsIlluminant),
});

// Normalize the source, then convert to the PCS space.
Expand Down Expand Up @@ -101,11 +101,11 @@ internal static void ConvertUsingIccProfile<TFrom, TTo>(this ColorProfileConvert
ConversionParams sourceParams = new(converter.Options.SourceIccProfile, toPcs: true);
ConversionParams targetParams = new(converter.Options.TargetIccProfile, toPcs: false);

ColorProfileConverter pcsConverter = new(new()
ColorProfileConverter pcsConverter = new(new ColorConversionOptions
{
MemoryAllocator = converter.Options.MemoryAllocator,
SourceWhitePoint = new(converter.Options.SourceIccProfile.Header.PcsIlluminant),
TargetWhitePoint = new(converter.Options.TargetIccProfile.Header.PcsIlluminant),
SourceWhitePoint = new CieXyz(converter.Options.SourceIccProfile.Header.PcsIlluminant),
TargetWhitePoint = new CieXyz(converter.Options.TargetIccProfile.Header.PcsIlluminant),
});

using IMemoryOwner<Vector4> pcsBuffer = converter.Options.MemoryAllocator.Allocate<Vector4>(source.Length);
Expand Down Expand Up @@ -355,7 +355,7 @@ private static Vector4 GetTargetPcsWithPerceptualAdjustment(
vector = Vector3.Max(vector, Vector3.Zero);
}

xyz = new(AdjustPcsFromV2BlackPoint(vector));
xyz = new CieXyz(AdjustPcsFromV2BlackPoint(vector));
}

// when converting from PCS to device with v2 perceptual intent
Expand All @@ -371,7 +371,7 @@ private static Vector4 GetTargetPcsWithPerceptualAdjustment(
vector = Vector3.Max(vector, Vector3.Zero);
}

xyz = new(vector);
xyz = new CieXyz(vector);
}

switch (targetParams.PcsType)
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/ColorProfiles/Hsl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace SixLabors.ImageSharp.ColorProfiles;
/// <param name="l">The l value (lightness) component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Hsl(float h, float s, float l)
: this(new(h, s, l))
: this(new Vector3(h, s, l))
{
}

Expand Down Expand Up @@ -141,7 +141,7 @@ public static Hsl FromProfileConnectingSpace(ColorConversionOptions options, in

if (MathF.Abs(chroma) < Constants.Epsilon)
{
return new(0F, s, l);
return new Hsl(0F, s, l);
}

if (MathF.Abs(r - max) < Constants.Epsilon)
Expand Down Expand Up @@ -172,7 +172,7 @@ public static Hsl FromProfileConnectingSpace(ColorConversionOptions options, in
s = chroma / (2F - max - min);
}

return new(h, s, l);
return new Hsl(h, s, l);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -213,7 +213,7 @@ public Rgb ToProfileConnectingSpace(ColorConversionOptions options)
}
}

return new(r, g, b);
return new Rgb(r, g, b);
}

/// <inheritdoc/>
Expand Down
Loading
Loading