diff --git a/geometrix-api/Geometrix.Application/UseCases/GenerateImage/IGenerateImageUseCase.cs b/geometrix-api/Geometrix.Application/UseCases/GenerateImage/IGenerateImageUseCase.cs index f405b57..5b20ea2 100644 --- a/geometrix-api/Geometrix.Application/UseCases/GenerateImage/IGenerateImageUseCase.cs +++ b/geometrix-api/Geometrix.Application/UseCases/GenerateImage/IGenerateImageUseCase.cs @@ -1,9 +1,12 @@ namespace Geometrix.Application.UseCases.GenerateImage; +/// +/// Represents the use case for generating an image. +/// public interface IGenerateImageUseCase { /// - /// Executes the Use Case. + /// Executes the Use Case. /// /// Task. Task Execute( @@ -17,7 +20,7 @@ Task Execute( string foregroundColor); /// - /// Sets the Output Port. + /// Sets the Output Port. /// /// Output Port void SetOutputPort(IOutputPort outputPort); diff --git a/geometrix-api/Geometrix.Application/UseCases/GenerateImage/IOutputPort.cs b/geometrix-api/Geometrix.Application/UseCases/GenerateImage/IOutputPort.cs index c1b82e8..f03c101 100644 --- a/geometrix-api/Geometrix.Application/UseCases/GenerateImage/IOutputPort.cs +++ b/geometrix-api/Geometrix.Application/UseCases/GenerateImage/IOutputPort.cs @@ -2,15 +2,18 @@ namespace Geometrix.Application.UseCases.GenerateImage; +/// +/// Represents an output port for image processing. +/// public interface IOutputPort { /// - /// Image created. + /// Image created. /// void Ok(ImageDescription image, byte[] bytes, string fileLocation); /// - /// Invalid input. + /// Invalid input. /// void Invalid(); } \ No newline at end of file diff --git a/geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs b/geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs index 16ec3f0..17dc502 100644 --- a/geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs +++ b/geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs @@ -1,38 +1,24 @@ namespace Geometrix.Domain.ValueObjects; -public readonly struct ThemeColor : IEquatable +public readonly struct ThemeColor(string value) + : IEquatable { - public string Value { get; } + public string Value { get; } = value; - public ThemeColor(string value) - { - Value = value; - } - public override bool Equals(object? obj) - { - return obj is ThemeColor o && Equals(o); - } + => obj is ThemeColor o && Equals(o); public bool Equals(ThemeColor other) - { - return Value == other.Value; - } + => Value == other.Value; public override int GetHashCode() - { - return HashCode.Combine(Value); - } + => HashCode.Combine(Value); public static bool operator ==(ThemeColor left, ThemeColor right) - { - return left.Equals(right); - } + => left.Equals(right); public static bool operator !=(ThemeColor left, ThemeColor right) - { - return !(left == right); - } + => !(left == right); /// /// Light. diff --git a/geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs b/geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs index 500fabf..6505056 100644 --- a/geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs +++ b/geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs @@ -6,29 +6,19 @@ public readonly struct TriangleDirection(TriangleDirection.Direction value) public Direction Value { get; } = value; public override bool Equals(object? obj) - { - return obj is TriangleDirection other && Equals(other); - } + => obj is TriangleDirection other && Equals(other); public bool Equals(TriangleDirection other) - { - return Value == other.Value; - } + => Value == other.Value; public override int GetHashCode() - { - return (int) Value; - } + => (int) Value; public static bool operator ==(TriangleDirection left, TriangleDirection right) - { - return left.Equals(right); - } + => left.Equals(right); public static bool operator !=(TriangleDirection left, TriangleDirection right) - { - return !(left == right); - } + => !(left == right); public static readonly TriangleDirection None = new(Direction.None); public static readonly TriangleDirection TopLeft = new(Direction.TopLeft); @@ -38,8 +28,7 @@ public override int GetHashCode() public static readonly TriangleDirection Filled = new(Direction.Filled); public static TriangleDirection MirrorRight(TriangleDirection direction) - { - return direction.Value switch + => direction.Value switch { Direction.None => None, Direction.TopLeft => TopRight, @@ -47,13 +36,11 @@ public static TriangleDirection MirrorRight(TriangleDirection direction) Direction.BottomLeft => BottomRight, Direction.BottomRight => BottomLeft, Direction.Filled => Filled, - _ => throw new ArgumentOutOfRangeException() + _ => throw new ArgumentOutOfRangeException(nameof(direction)) }; - } public static TriangleDirection MirrorDown(TriangleDirection direction) - { - return direction.Value switch + => direction.Value switch { Direction.None => None, Direction.TopLeft => BottomLeft, @@ -61,9 +48,8 @@ public static TriangleDirection MirrorDown(TriangleDirection direction) Direction.BottomLeft => TopLeft, Direction.BottomRight => TopRight, Direction.Filled => Filled, - _ => throw new ArgumentOutOfRangeException() + _ => throw new ArgumentOutOfRangeException(nameof(direction)) }; - } public static TriangleDirection CreateRandom(Random random, bool includeEmptyAndFill) { diff --git a/geometrix-api/Geometrix.Infrastructure/DataAccess/EntityDescriptionFactory.cs b/geometrix-api/Geometrix.Infrastructure/DataAccess/EntityDescriptionFactory.cs index 07b536a..adbe744 100644 --- a/geometrix-api/Geometrix.Infrastructure/DataAccess/EntityDescriptionFactory.cs +++ b/geometrix-api/Geometrix.Infrastructure/DataAccess/EntityDescriptionFactory.cs @@ -6,14 +6,14 @@ namespace Geometrix.Infrastructure.DataAccess; public sealed class EntityDescriptionFactory : IImageDescriptionFactory { - public ImageDescription NewImage(Pattern pattern, Settings settings) => - new(pattern, settings); + public ImageDescription NewImage(Pattern pattern, Settings settings) + => new(pattern, settings); public Pattern NewPattern( int mirrorPowerHorizontal, int mirrorPowerVertical, int cellGroupLength, bool includeEmptyAndFill, - int seed) => - new(mirrorPowerHorizontal, mirrorPowerVertical, cellGroupLength, includeEmptyAndFill, seed); + int seed) + => new(mirrorPowerHorizontal, mirrorPowerVertical, cellGroupLength, includeEmptyAndFill, seed); } \ No newline at end of file diff --git a/geometrix-api/Geometrix.Infrastructure/Geometrix.Infrastructure.csproj b/geometrix-api/Geometrix.Infrastructure/Geometrix.Infrastructure.csproj index 45065c3..1d2e4b4 100644 --- a/geometrix-api/Geometrix.Infrastructure/Geometrix.Infrastructure.csproj +++ b/geometrix-api/Geometrix.Infrastructure/Geometrix.Infrastructure.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/geometrix-api/Geometrix.Infrastructure/ImageCreation/ImageCreationService.cs b/geometrix-api/Geometrix.Infrastructure/ImageCreation/ImageCreationService.cs index c2ab367..21fd1e5 100644 --- a/geometrix-api/Geometrix.Infrastructure/ImageCreation/ImageCreationService.cs +++ b/geometrix-api/Geometrix.Infrastructure/ImageCreation/ImageCreationService.cs @@ -1,5 +1,7 @@ using Geometrix.Application.Services; using Geometrix.Domain; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; namespace Geometrix.Infrastructure.ImageCreation; diff --git a/geometrix-api/Geometrix.Infrastructure/ImageCreation/ImageEditionService.cs b/geometrix-api/Geometrix.Infrastructure/ImageCreation/ImageEditionService.cs index 84111e9..86a7126 100644 --- a/geometrix-api/Geometrix.Infrastructure/ImageCreation/ImageEditionService.cs +++ b/geometrix-api/Geometrix.Infrastructure/ImageCreation/ImageEditionService.cs @@ -1,7 +1,10 @@ using Geometrix.Domain.Patterns; using Geometrix.Domain.ValueObjects; +using SixLabors.ImageSharp; using SixLabors.ImageSharp.Drawing; using SixLabors.ImageSharp.Drawing.Processing; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; namespace Geometrix.Infrastructure.ImageCreation; diff --git a/geometrix-api/Geometrix.Infrastructure/ImageCreation/TriangleService.cs b/geometrix-api/Geometrix.Infrastructure/ImageCreation/TriangleService.cs index 1b9e5d2..07a6571 100644 --- a/geometrix-api/Geometrix.Infrastructure/ImageCreation/TriangleService.cs +++ b/geometrix-api/Geometrix.Infrastructure/ImageCreation/TriangleService.cs @@ -1,12 +1,15 @@ using Geometrix.Domain.ValueObjects; +using SixLabors.ImageSharp; using SixLabors.ImageSharp.Drawing; namespace Geometrix.Infrastructure.ImageCreation; public sealed class TriangleService { + private readonly TriangleFactory _triangleFactory = new(); + /// - /// Get a triangle from a direction and a position. + /// Get a triangle from a direction and a position. /// /// The direction of the triangle. /// The x position of the triangle. @@ -16,69 +19,49 @@ public sealed class TriangleService /// direction public Polygon? GetTriangle(TriangleDirection direction, int x, int y, int cellWidthPixel) { - return direction.Value switch + if (!Enum.IsDefined(typeof(TriangleDirection.Direction), direction.Value)) { - TriangleDirection.Direction.None => null, - TriangleDirection.Direction.TopLeft => CreateTopLeftTriangle(x, y, cellWidthPixel), - TriangleDirection.Direction.TopRight => CreateTopRightTriangle(x, y, cellWidthPixel), - TriangleDirection.Direction.BottomLeft => CreateBottomLeftTriangle(x, y, cellWidthPixel), - TriangleDirection.Direction.BottomRight => CreateBottomRightTriangle(x, y, cellWidthPixel), - TriangleDirection.Direction.Filled => CreateFilled(x, y, cellWidthPixel), - _ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null) - }; - } + throw new ArgumentOutOfRangeException(nameof(direction), $"Invalid triangle direction: {direction}"); + } - private static Polygon CreateTopLeftTriangle(int x, int y, int cellWidthPixel) - { - return new Polygon(new List - { - new LinearLineSegment( - new PointF(x, y), - new PointF(x + cellWidthPixel, y), - new PointF(x, y + cellWidthPixel) - ) - }); + return direction.Value == TriangleDirection.Direction.None + ? null + : _triangleFactory.CreateTriangle(direction, x, y, cellWidthPixel); } +} - private static Polygon CreateTopRightTriangle(int x, int y, int cellWidthPixel) +public class TriangleFactory +{ + public Polygon CreateTriangle(TriangleDirection direction, int x, int y, int cellWidthPixel) { - return new Polygon(new List + return direction.Value switch { - new LinearLineSegment( - new PointF(x + cellWidthPixel, y), - new PointF(x + cellWidthPixel, y + cellWidthPixel), - new PointF(x, y) - ) - }); + TriangleDirection.Direction.TopLeft => CreateTriangle(new[] { (x, y), (x + cellWidthPixel, y), (x, y + cellWidthPixel) }), + TriangleDirection.Direction.TopRight => CreateTriangle(new[] { (x + cellWidthPixel, y), (x + cellWidthPixel, y + cellWidthPixel), (x, y) }), + TriangleDirection.Direction.BottomLeft => CreateTriangle(new[] { (x, y + cellWidthPixel), (x, y), (x + cellWidthPixel, y + cellWidthPixel) }), + TriangleDirection.Direction.BottomRight => CreateTriangle(new[] { (x + cellWidthPixel, y + cellWidthPixel), (x, y + cellWidthPixel), (x + cellWidthPixel, y) }), + TriangleDirection.Direction.Filled => CreateRectangle(x, y, cellWidthPixel), + _ => throw new ArgumentException("Invalid triangle direction", nameof(direction)) + }; } - private static Polygon CreateBottomLeftTriangle(int x, int y, int cellWidthPixel) + private Polygon CreateTriangle((int x, int y)[] points) { - return new Polygon(new List + var lineSegments = new List { new LinearLineSegment( - new PointF(x, y + cellWidthPixel), - new PointF(x, y), - new PointF(x + cellWidthPixel, y + cellWidthPixel) + new PointF(points[0].x, points[0].y), + new PointF(points[1].x, points[1].y), + new PointF(points[2].x, points[2].y) ) - }); - } + }; - private static Polygon CreateBottomRightTriangle(int x, int y, int cellWidthPixel) - { - return new Polygon(new List - { - new LinearLineSegment( - new PointF(x + cellWidthPixel, y + cellWidthPixel), - new PointF(x, y + cellWidthPixel), - new PointF(x + cellWidthPixel, y) - ) - }); + return new Polygon(lineSegments); } - private static Polygon CreateFilled(int x, int y, int cellWidthPixel) + private Polygon CreateRectangle(int x, int y, int cellWidthPixel) { - return new Polygon(new List + var lineSegments = new List { new LinearLineSegment( new PointF(x, y), @@ -86,6 +69,8 @@ private static Polygon CreateFilled(int x, int y, int cellWidthPixel) new PointF(x + cellWidthPixel, y + cellWidthPixel), new PointF(x, y + cellWidthPixel) ) - }); + }; + + return new Polygon(lineSegments); } -} \ No newline at end of file +} diff --git a/geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj b/geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj index ccd92ea..73210d4 100644 --- a/geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj +++ b/geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj @@ -32,7 +32,7 @@ - + diff --git a/geometrix-api/Geometrix.WebApi/wwwroot/Images/4-43-T-2-2-blue-indigo-64.png b/geometrix-api/Geometrix.WebApi/wwwroot/Images/4-43-T-2-2-blue-indigo-64.png new file mode 100644 index 0000000..1e1245f Binary files /dev/null and b/geometrix-api/Geometrix.WebApi/wwwroot/Images/4-43-T-2-2-blue-indigo-64.png differ diff --git a/geometrix-api/global.json b/geometrix-api/global.json new file mode 100644 index 0000000..dad2db5 --- /dev/null +++ b/geometrix-api/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "8.0.0", + "rollForward": "latestMajor", + "allowPrerelease": true + } +} \ No newline at end of file