From d1ec02678696a21cea331d11a55335abe1678c36 Mon Sep 17 00:00:00 2001 From: Claudio Schaad Date: Thu, 3 Dec 2020 23:00:13 +0100 Subject: [PATCH] Draw instruction with dots --- LegoArt/MainWindow.xaml.cs | 2 +- .../BuildingInstructionElement.cs | 66 +++++++++++++++---- .../BuildingInstructionMatrix.cs | 4 +- .../BuildingInstructionService.cs | 4 +- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/LegoArt/MainWindow.xaml.cs b/LegoArt/MainWindow.xaml.cs index 7e60ac1..bbbb751 100644 --- a/LegoArt/MainWindow.xaml.cs +++ b/LegoArt/MainWindow.xaml.cs @@ -77,7 +77,7 @@ namespace LegoArt var d = new DataObject(DataFormats.Bitmap, legoArtImage.Source, true); var legoArtBitmap = d.GetData(typeof(Bitmap)) as Bitmap; - var buildingInstructionBitmap = buildingInstructionService.CreateBuildingInstructionImage(legoArtBitmap, legoArtColorService.GetLegoArtColorSet(currentLegoArtSet)); + var buildingInstructionBitmap = buildingInstructionService.CreateBuildingInstructionImage(legoArtBitmap, legoArtColorService.GetLegoArtColorSet(currentLegoArtSet), true); var outputPath = buildingInstructionService.PersistBuildingInstructions(buildingInstructionBitmap, tbImagePath.Text); HideWaitCursor(); diff --git a/LegoArtTool/BuildingInstruction/BuildingInstructionElement.cs b/LegoArtTool/BuildingInstruction/BuildingInstructionElement.cs index 641d76c..72d0dca 100644 --- a/LegoArtTool/BuildingInstruction/BuildingInstructionElement.cs +++ b/LegoArtTool/BuildingInstruction/BuildingInstructionElement.cs @@ -1,4 +1,5 @@ -using System.Drawing; +using System; +using System.Drawing; namespace LegoArtTool.BuildingInstruction { @@ -8,7 +9,7 @@ namespace LegoArtTool.BuildingInstruction public int Number { get; private set; } public const int PixelSize = 50; - private const int NumberSize = 20; + private const int NumberSize = 25; public BuildingInstructionElement(Color color, int number) { @@ -16,25 +17,57 @@ namespace LegoArtTool.BuildingInstruction Number = number; } - public Bitmap GetBitmap() + public Bitmap GetBitmap(bool useDots) { var bitmap = new Bitmap(PixelSize, PixelSize); - for (int x = 0; x < PixelSize; x++) - { - for (int y = 0; y < PixelSize; y++) - { - bitmap.SetPixel(x, y, Color); - } - } - DrawNumber(bitmap, Number); + if (useDots) + { + DrawDot(bitmap, Color); + DrawNumber(bitmap, Number, Color); + } + else + { + DrawPixelBlock(bitmap, Color); + DrawNumberInCircle(bitmap, Number); + } return bitmap; } - private void DrawNumber(Bitmap bitmap, int number) + private void DrawDot(Bitmap bitmap, Color color) { + using (var g = Graphics.FromImage(bitmap)) + { + g.FillCircle(new SolidBrush(color), PixelSize / 2, PixelSize / 2, PixelSize / 2); + g.Flush(); + } + } + private void DrawPixelBlock(Bitmap bitmap, Color color) + { + for (int x = 0; x < PixelSize; x++) + { + for (int y = 0; y < PixelSize; y++) + { + bitmap.SetPixel(x, y, color); + } + } + } + + private void DrawNumber(Bitmap bitmap, int number, Color color) + { + using (var g = Graphics.FromImage(bitmap)) + { + var font = g.GetIdealFontSize(PixelSize, "15", new Font("Arial", 8)); + var fontColor = IdealTextColor(color); + g.DrawText(new SolidBrush(fontColor), font, PixelSize, StringAlignment.Center, number.ToString()); + g.Flush(); + } + } + + private void DrawNumberInCircle(Bitmap bitmap, int number) + { using (var g = Graphics.FromImage(bitmap)) { g.FillCircle(Brushes.White, PixelSize / 2, PixelSize / 2, NumberSize / 2); @@ -43,5 +76,14 @@ namespace LegoArtTool.BuildingInstruction g.Flush(); } } + + public Color IdealTextColor(Color bg) + { + int nThreshold = 105; + int bgDelta = Convert.ToInt32((bg.R * 0.299) + (bg.G * 0.587) + (bg.B * 0.114)); + + var foreColor = (255 - bgDelta < nThreshold) ? Color.Black : Color.White; + return foreColor; + } } } diff --git a/LegoArtTool/BuildingInstruction/BuildingInstructionMatrix.cs b/LegoArtTool/BuildingInstruction/BuildingInstructionMatrix.cs index a194aaa..e59332e 100644 --- a/LegoArtTool/BuildingInstruction/BuildingInstructionMatrix.cs +++ b/LegoArtTool/BuildingInstruction/BuildingInstructionMatrix.cs @@ -27,7 +27,7 @@ namespace LegoArtTool.BuildingInstruction } } - public Bitmap GetBitmap() + public Bitmap GetBitmap(bool useDots) { var bitmapSize = MatrixSize * BuildingInstructionElement.PixelSize; var bitmap = new Bitmap(bitmapSize, bitmapSize); @@ -37,7 +37,7 @@ namespace LegoArtTool.BuildingInstruction { for (int y = 0; y < MatrixSize; y++) { - var pixelBitmap = Pixels[x, y].GetBitmap(); + var pixelBitmap = Pixels[x, y].GetBitmap(useDots); g.DrawImage(pixelBitmap, x * pixelBitmap.Width, y * pixelBitmap.Height); } } diff --git a/LegoArtTool/BuildingInstruction/BuildingInstructionService.cs b/LegoArtTool/BuildingInstruction/BuildingInstructionService.cs index 6857dce..9042b99 100644 --- a/LegoArtTool/BuildingInstruction/BuildingInstructionService.cs +++ b/LegoArtTool/BuildingInstruction/BuildingInstructionService.cs @@ -9,10 +9,10 @@ namespace LegoArtTool.BuildingInstruction { public class BuildingInstructionService { - public Bitmap CreateBuildingInstructionImage(Bitmap bitmap, List legoArtColors) + public Bitmap CreateBuildingInstructionImage(Bitmap bitmap, List legoArtColors, bool useDots) { var pixelMatrix = new BuildingInstructionMatrix(bitmap, legoArtColors); - return pixelMatrix.GetBitmap(); + return pixelMatrix.GetBitmap(useDots); } public string PersistBuildingInstructions(Bitmap bitmap, string path)