This commit is contained in:
Claudio Schaad 2020-11-28 17:38:10 +01:00
parent 46767da90b
commit 9f9cf46c26
11 changed files with 304 additions and 0 deletions

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################
/LegoArt/obj
/LegoArt/bin/Debug/netcoreapp3.1
/.vs/LegoArt/v16/TestStore/0

25
LegoArt.sln Normal file
View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30621.155
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LegoArt", "LegoArt\LegoArt.csproj", "{1D754A7E-DB2F-4A79-83B8-BF64A7130CCC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1D754A7E-DB2F-4A79-83B8-BF64A7130CCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D754A7E-DB2F-4A79-83B8-BF64A7130CCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D754A7E-DB2F-4A79-83B8-BF64A7130CCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D754A7E-DB2F-4A79-83B8-BF64A7130CCC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E48F3C4B-F649-4D65-AE06-10725BDFF022}
EndGlobalSection
EndGlobal

9
LegoArt/App.xaml Normal file
View file

@ -0,0 +1,9 @@
<Application x:Class="LegoArt.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LegoArt"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

17
LegoArt/App.xaml.cs Normal file
View file

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace LegoArt
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

10
LegoArt/AssemblyInfo.cs Normal file
View file

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

13
LegoArt/LegoArt.csproj Normal file
View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<ItemGroup>
<ApplicationDefinition Update="App.xaml">
<SubType>Designer</SubType>
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Page Update="MainWindow.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup>
</Project>

19
LegoArt/LegoColorInfo.cs Normal file
View file

@ -0,0 +1,19 @@
using System.Windows.Media;
namespace LegoArt
{
public class LegoColorInfo
{
public Color Color { get; private set; }
public int LegoNumber { get; private set; }
public int HaveCount { get; private set; }
public int NeedCount { get; set; }
public LegoColorInfo(Color color, int number, int haveCount)
{
Color = color;
LegoNumber = number;
HaveCount = haveCount;
}
}
}

View file

@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
namespace LegoArt
{
public class LegoColorService
{
public List<LegoColorInfo> LegoColors { get; private set; }
public LegoColorService()
{
LegoColors = new List<LegoColorInfo>();
LegoColors.Add(new LegoColorInfo(Color.FromRgb(0, 0, 0), 1, 698));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(102, 101, 96), 2, 141));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(167, 166, 162), 3, 51));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(246, 246, 248), 4, 149));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(0, 53, 94), 5, 121));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(101, 135, 162), 6, 52));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(121, 196, 236), 7, 57));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(253, 104, 0), 8, 74));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(255, 170, 0), 9, 65));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(229, 205, 135), 10, 283));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(163, 129, 93), 11, 137));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(196, 115, 54), 12, 29));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(206, 81, 0), 13, 85));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(119, 40, 0), 14, 250));
LegoColors.Add(new LegoColorInfo(Color.FromRgb(66, 23, 3), 15, 554));
}
public LegoColorInfo GetColorInfo(Color color)
{
return LegoColors.Single(l => l.Color == color);
}
}
}

26
LegoArt/MainWindow.xaml Normal file
View file

@ -0,0 +1,26 @@
<Window x:Class="LegoArt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LegoArt"
mc:Ignorable="d"
Title="Lego Art Color Info" Height="500" Width="420">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Height="25" VerticalAlignment="Top">
<Label Width="50" Content="Bild:"/>
<TextBox Width="300" x:Name="tbImagePath" />
<Button Width="50" Name="btnImageChooser" Content="..." Click="btnImageChooser_Click"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Vertical" Name="parentStackPanel" VerticalAlignment="Top">
</StackPanel>
</Grid>
</Window>

127
LegoArt/MainWindow.xaml.cs Normal file
View file

@ -0,0 +1,127 @@
using Microsoft.Win32;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace LegoArt
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnImageChooser_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
tbImagePath.Text = openFileDialog.FileName;
LoadImage(openFileDialog.FileName);
}
}
private void LoadImage(string path)
{
var legoColorService = new LegoColorService();
var bitmap = new System.Drawing.Bitmap(path);
var width = bitmap.Width;
var height = bitmap.Height;
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
var clr = bitmap.GetPixel(h, w);
var legoColor = ConvertDrawingToWindowsMediaColor(clr);
var legoColorInfo = legoColorService.GetColorInfo(legoColor);
legoColorInfo.NeedCount++;
}
}
parentStackPanel.Children.Clear();
AddLine(Colors.White, "Have", "Needed", "RGB", "#", "Difference", Colors.White);
var sortedColors = legoColorService.LegoColors.OrderBy(c => c.LegoNumber).ToList();
for (int i = 0; i < sortedColors.Count; i++)
{
var color = sortedColors[i].Color;
var textRgb = $"{color.R};{color.G};{color.B}";
var difference = sortedColors[i].HaveCount - sortedColors[i].NeedCount;
var statusColor = difference < 0 ? Colors.Red : Colors.Green;
AddLine(color, sortedColors[i].HaveCount.ToString(), sortedColors[i].NeedCount.ToString(), textRgb, sortedColors[i].LegoNumber.ToString(), difference.ToString(), statusColor);
}
var have = legoColorService.LegoColors.Sum(l => l.HaveCount);
var needed = legoColorService.LegoColors.Sum(l => l.NeedCount);
AddLine(Colors.White, have.ToString(), needed.ToString(), "Total:", "", "", Colors.White);
}
private void AddLine(Color color, string textHave, string textNeed, string textRgb, string textNumber, string textDifference, Color statusColor)
{
var rect = new Rectangle();
rect.Width = 20;
rect.Height = 20;
rect.Fill = new SolidColorBrush(color);
var textBlockHave = new TextBlock();
textBlockHave.Width = 40;
textBlockHave.Height = 20;
textBlockHave.TextAlignment = TextAlignment.Center;
textBlockHave.Text = textHave;
var textBlockNeed = new TextBlock();
textBlockNeed.Width = 50;
textBlockNeed.Height = 20;
textBlockNeed.TextAlignment = TextAlignment.Center;
textBlockNeed.Text = textNeed;
var textBlockRgb = new TextBlock();
textBlockRgb.Width = 100;
textBlockRgb.Height = 20;
textBlockRgb.TextAlignment = TextAlignment.Left;
textBlockRgb.Text = textRgb;
var legoNumber = new TextBlock();
legoNumber.Width = 20;
legoNumber.Height = 20;
legoNumber.TextAlignment = TextAlignment.Center;
legoNumber.Text = textNumber;
var differenceTextBlock = new TextBlock();
differenceTextBlock.Width = 55;
differenceTextBlock.Height = 20;
differenceTextBlock.TextAlignment = TextAlignment.Center;
differenceTextBlock.Text = textDifference;
var status = new Rectangle();
status.Width = 20;
status.Height = 20;
status.Fill = new SolidColorBrush(statusColor);
var stackPanel = new StackPanel();
stackPanel.Orientation = Orientation.Horizontal;
stackPanel.Margin = new Thickness(2, 2, 2, 2);
stackPanel.Children.Add(legoNumber);
stackPanel.Children.Add(rect);
stackPanel.Children.Add(textBlockRgb);
stackPanel.Children.Add(textBlockHave);
stackPanel.Children.Add(textBlockNeed);
stackPanel.Children.Add(differenceTextBlock);
stackPanel.Children.Add(status);
parentStackPanel.Children.Add(stackPanel);
}
private Color ConvertDrawingToWindowsMediaColor(System.Drawing.Color inputColor)
{
return Color.FromArgb(inputColor.A, inputColor.R, inputColor.G, inputColor.B);
}
}
}