Compare commits
10 commits
imagegener
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b9e47001e | ||
| d1ec026786 | |||
| 27c1ca6a9b | |||
| 18f8c7efb3 | |||
|
|
5dd28a0f20 | ||
|
|
4ef9eab48f | ||
|
|
cdc438c030 | ||
|
|
056e58a297 | ||
| 0bf7ef09b0 | |||
|
|
21395633d2 |
14 changed files with 523 additions and 150 deletions
201
LICENSE
Normal file
201
LICENSE
Normal file
|
|
@ -0,0 +1,201 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
@ -18,16 +18,25 @@
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
<StackPanel Grid.Row="0" Orientation="Horizontal" Height="25" VerticalAlignment="Top">
|
<StackPanel Grid.Row="0" Orientation="Horizontal" Height="25" VerticalAlignment="Top">
|
||||||
<Label Width="50" Content="Bild:"/>
|
<Label Width="Auto" Content="Source Image:"/>
|
||||||
<TextBox Width="300" x:Name="tbImagePath" />
|
<TextBox Width="265" x:Name="tbImagePath" />
|
||||||
<Button Width="50" Name="btnImageChooser" Content="..." Click="btnImageChooser_Click"/>
|
<Button Width="50" Name="btnImageChooser" Content="..." Click="btnImageChooser_Click"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
<StackPanel Grid.Row="2" Orientation="Vertical" Name="parentStackPanel" VerticalAlignment="Top">
|
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" Height="25" VerticalAlignment="Top" Margin="5, 0 , 0, 0">
|
||||||
|
<Button Width="200" Name="btnInstructionsPersister" Content="Save Instructions" Click="btnInstructionsPersister_Click"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
|
|
||||||
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="sourceImage" Stretch="None"/>
|
<StackPanel Grid.Row="2" Orientation="Vertical" VerticalAlignment="Top">
|
||||||
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="scaledImage" Stretch="None"/>
|
<Label Name="lblLegoArtSet" />
|
||||||
|
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Name="parentStackPanel">
|
||||||
|
</StackPanel>
|
||||||
|
</StackPanel>
|
||||||
|
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Vertical">
|
||||||
|
<Label Name="lblSourceImage" Visibility="Hidden">Source Image:</Label>
|
||||||
|
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="sourceImage" Stretch="None"/>
|
||||||
|
<Label Name="lblLegoArtImage" Visibility="Hidden">LEGO® Art Image:</Label>
|
||||||
|
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="legoArtImage" Stretch="None"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,15 @@
|
||||||
using LegoArtTool;
|
using System.Drawing;
|
||||||
|
using LegoArtTool;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Shapes;
|
using Rectangle = System.Windows.Shapes.Rectangle;
|
||||||
|
using LegoArtTool.LegoArtColor;
|
||||||
|
using LegoArtTool.BuildingInstruction;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace LegoArt
|
namespace LegoArt
|
||||||
{
|
{
|
||||||
|
|
@ -17,7 +21,10 @@ namespace LegoArt
|
||||||
private readonly LegoArtColorService legoArtColorService;
|
private readonly LegoArtColorService legoArtColorService;
|
||||||
private readonly BitmapHelperService bitmapHelperService;
|
private readonly BitmapHelperService bitmapHelperService;
|
||||||
private readonly ImageHelperService imageHelperService;
|
private readonly ImageHelperService imageHelperService;
|
||||||
private LegoArtImageGenerationService legoArtImageGenerationService;
|
private readonly LegoArtImageGenerationService legoArtImageGenerationService;
|
||||||
|
private readonly BuildingInstructionService buildingInstructionService;
|
||||||
|
|
||||||
|
private string currentLegoArtSet;
|
||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
|
|
@ -27,6 +34,10 @@ namespace LegoArt
|
||||||
bitmapHelperService = new BitmapHelperService();
|
bitmapHelperService = new BitmapHelperService();
|
||||||
imageHelperService = new ImageHelperService();
|
imageHelperService = new ImageHelperService();
|
||||||
legoArtImageGenerationService = new LegoArtImageGenerationService();
|
legoArtImageGenerationService = new LegoArtImageGenerationService();
|
||||||
|
buildingInstructionService = new BuildingInstructionService();
|
||||||
|
|
||||||
|
btnInstructionsPersister.Visibility = Visibility.Hidden;
|
||||||
|
currentLegoArtSet = legoArtColorService.LegoArtSets.First();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void btnImageChooser_Click(object sender, RoutedEventArgs e)
|
private void btnImageChooser_Click(object sender, RoutedEventArgs e)
|
||||||
|
|
@ -36,33 +47,59 @@ namespace LegoArt
|
||||||
{
|
{
|
||||||
tbImagePath.Text = openFileDialog.FileName;
|
tbImagePath.Text = openFileDialog.FileName;
|
||||||
|
|
||||||
Application.Current.Dispatcher.Invoke(() =>
|
ShowWaitCursor();
|
||||||
{
|
|
||||||
Mouse.OverrideCursor = Cursors.Wait;
|
|
||||||
});
|
|
||||||
|
|
||||||
LoadImage(openFileDialog.FileName);
|
LoadImage(openFileDialog.FileName);
|
||||||
|
|
||||||
Application.Current.Dispatcher.Invoke(() =>
|
HideWaitCursor();
|
||||||
{
|
|
||||||
Mouse.OverrideCursor = null;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ShowWaitCursor()
|
||||||
|
{
|
||||||
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
Mouse.OverrideCursor = Cursors.Wait;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HideWaitCursor()
|
||||||
|
{
|
||||||
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
Mouse.OverrideCursor = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void btnInstructionsPersister_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
ShowWaitCursor();
|
||||||
|
|
||||||
|
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), true);
|
||||||
|
var outputPath = buildingInstructionService.PersistBuildingInstructions(buildingInstructionBitmap, tbImagePath.Text);
|
||||||
|
|
||||||
|
HideWaitCursor();
|
||||||
|
|
||||||
|
MessageBox.Show($"Instructions saved to {outputPath}", "Saved", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||||
|
}
|
||||||
|
|
||||||
private void LoadImage(string path)
|
private void LoadImage(string path)
|
||||||
{
|
{
|
||||||
var bitmap = new System.Drawing.Bitmap(path);
|
var sourceBitmap = new Bitmap(path);
|
||||||
var reducedBitMap = legoArtImageGenerationService.GenerateLegoArtImageFromFullColorImage(bitmap);
|
sourceImage.Source = imageHelperService.LoadToImage(bitmapHelperService.Scale(sourceBitmap, 1));
|
||||||
|
lblSourceImage.Visibility = Visibility.Visible;
|
||||||
|
|
||||||
var legoArtColors = legoArtColorService.ParseImage(reducedBitMap);
|
var reducedBitMap = legoArtImageGenerationService.GenerateLegoArtImageFromFullColorImage(sourceBitmap, currentLegoArtSet);
|
||||||
|
legoArtImage.Source = imageHelperService.LoadToImage(reducedBitMap);
|
||||||
|
lblLegoArtImage.Visibility = Visibility.Visible;
|
||||||
|
|
||||||
|
var legoArtColors = legoArtColorService.ParseImage(reducedBitMap, currentLegoArtSet);
|
||||||
if (legoArtColors != null)
|
if (legoArtColors != null)
|
||||||
{
|
{
|
||||||
var sourceBitmap = bitmapHelperService.Scale(reducedBitMap, 1);
|
btnInstructionsPersister.Visibility = Visibility.Visible;
|
||||||
sourceImage.Source = imageHelperService.LoadToImage(sourceBitmap);
|
lblLegoArtSet.Content = currentLegoArtSet;
|
||||||
|
|
||||||
var convertedBitmap = bitmapHelperService.ConvertToPixelMatrix(sourceBitmap, 20);
|
|
||||||
scaledImage.Source = imageHelperService.LoadToImage(convertedBitmap);
|
|
||||||
|
|
||||||
parentStackPanel.Children.Clear();
|
parentStackPanel.Children.Clear();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using LegoArtTool.Pixels;
|
using System.Drawing;
|
||||||
using System.Drawing;
|
|
||||||
|
|
||||||
namespace LegoArtTool
|
namespace LegoArtTool
|
||||||
{
|
{
|
||||||
|
|
@ -28,11 +27,5 @@ namespace LegoArtTool
|
||||||
|
|
||||||
return scaledBitmap;
|
return scaledBitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap ConvertToPixelMatrix(Bitmap bitmap, int pixelSize)
|
|
||||||
{
|
|
||||||
var pixelMatrix = new PixelMatrix(pixelSize, bitmap);
|
|
||||||
return pixelMatrix.ConvertBitmap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
|
namespace LegoArtTool.BuildingInstruction
|
||||||
|
{
|
||||||
|
public class BuildingInstructionElement
|
||||||
|
{
|
||||||
|
public Color Color { get; private set; }
|
||||||
|
public int Number { get; private set; }
|
||||||
|
|
||||||
|
public const int PixelSize = 50;
|
||||||
|
private const int NumberSize = 25;
|
||||||
|
|
||||||
|
public BuildingInstructionElement(Color color, int number)
|
||||||
|
{
|
||||||
|
Color = color;
|
||||||
|
Number = number;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bitmap GetBitmap(bool useDots)
|
||||||
|
{
|
||||||
|
var bitmap = new Bitmap(PixelSize, PixelSize);
|
||||||
|
|
||||||
|
if (useDots)
|
||||||
|
{
|
||||||
|
DrawDot(bitmap, Color);
|
||||||
|
DrawNumber(bitmap, Number, Color);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DrawPixelBlock(bitmap, Color);
|
||||||
|
DrawNumberInCircle(bitmap, Number);
|
||||||
|
}
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
var font = g.GetIdealFontSize(NumberSize, "15", new Font("Arial", 8));
|
||||||
|
g.DrawText(Brushes.Black, font, PixelSize, StringAlignment.Center, number.ToString());
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,21 +1,19 @@
|
||||||
using System;
|
using LegoArtTool.LegoArtColor;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace LegoArtTool.Pixels
|
namespace LegoArtTool.BuildingInstruction
|
||||||
{
|
{
|
||||||
public class PixelMatrix
|
public class BuildingInstructionMatrix
|
||||||
{
|
{
|
||||||
public Pixel[,] Pixels { get; private set; }
|
public BuildingInstructionElement[,] Pixels { get; private set; }
|
||||||
public int MatrixSize { get; private set; }
|
public int MatrixSize { get; private set; }
|
||||||
public int PixelSize { get; private set; }
|
|
||||||
|
|
||||||
private Random rnd = new Random();
|
public BuildingInstructionMatrix(Bitmap sourceBitmap, List<LegoArtColorInfo> legoArtColors)
|
||||||
|
|
||||||
public PixelMatrix(int pixelSize, Bitmap sourceBitmap)
|
|
||||||
{
|
{
|
||||||
MatrixSize = sourceBitmap.Width;
|
MatrixSize = sourceBitmap.Width;
|
||||||
PixelSize = pixelSize;
|
Pixels = new BuildingInstructionElement[MatrixSize, MatrixSize];
|
||||||
Pixels = new Pixel[MatrixSize, MatrixSize];
|
|
||||||
|
|
||||||
for (int x = 0; x < MatrixSize; x++)
|
for (int x = 0; x < MatrixSize; x++)
|
||||||
{
|
{
|
||||||
|
|
@ -23,14 +21,15 @@ namespace LegoArtTool.Pixels
|
||||||
{
|
{
|
||||||
var pixel = sourceBitmap.GetPixel(x, y);
|
var pixel = sourceBitmap.GetPixel(x, y);
|
||||||
var sourceColor = Color.FromArgb(pixel.ToArgb());
|
var sourceColor = Color.FromArgb(pixel.ToArgb());
|
||||||
Pixels[x, y] = new Pixel(sourceColor, PixelSize, 9);
|
var legoNumber = legoArtColors.Single(l => l.Color == sourceColor).LegoNumber;
|
||||||
|
Pixels[x, y] = new BuildingInstructionElement(sourceColor, legoNumber);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap ConvertBitmap()
|
public Bitmap GetBitmap(bool useDots)
|
||||||
{
|
{
|
||||||
var bitmapSize = MatrixSize * PixelSize;
|
var bitmapSize = MatrixSize * BuildingInstructionElement.PixelSize;
|
||||||
var bitmap = new Bitmap(bitmapSize, bitmapSize);
|
var bitmap = new Bitmap(bitmapSize, bitmapSize);
|
||||||
using (var g = Graphics.FromImage(bitmap))
|
using (var g = Graphics.FromImage(bitmap))
|
||||||
{
|
{
|
||||||
|
|
@ -38,7 +37,7 @@ namespace LegoArtTool.Pixels
|
||||||
{
|
{
|
||||||
for (int y = 0; y < MatrixSize; y++)
|
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);
|
g.DrawImage(pixelBitmap, x * pixelBitmap.Width, y * pixelBitmap.Height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -46,11 +45,5 @@ namespace LegoArtTool.Pixels
|
||||||
|
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Color GetRandomColor()
|
|
||||||
{
|
|
||||||
Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
|
|
||||||
return randomColor;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
using LegoArtTool.LegoArtColor;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace LegoArtTool.BuildingInstruction
|
||||||
|
{
|
||||||
|
public class BuildingInstructionService
|
||||||
|
{
|
||||||
|
public Bitmap CreateBuildingInstructionImage(Bitmap bitmap, List<LegoArtColorInfo> legoArtColors, bool useDots)
|
||||||
|
{
|
||||||
|
var pixelMatrix = new BuildingInstructionMatrix(bitmap, legoArtColors);
|
||||||
|
return pixelMatrix.GetBitmap(useDots);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string PersistBuildingInstructions(Bitmap bitmap, string path)
|
||||||
|
{
|
||||||
|
var segmentSize = bitmap.Width / 3;
|
||||||
|
var outPutDirectory = CreateOutPutDirectory(path);
|
||||||
|
|
||||||
|
var segmentNumber = 1;
|
||||||
|
for (var h = 0; h < 3; h++)
|
||||||
|
{
|
||||||
|
for (var w = 0; w < 3; w++)
|
||||||
|
{
|
||||||
|
var rectangle = new Rectangle(w * segmentSize, h * segmentSize, segmentSize, segmentSize);
|
||||||
|
var segment = bitmap.Clone(rectangle, bitmap.PixelFormat);
|
||||||
|
segment.Save($"{outPutDirectory}{Path.DirectorySeparatorChar}{segmentNumber++}.png", ImageFormat.Png);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outPutDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string CreateOutPutDirectory(string path)
|
||||||
|
{
|
||||||
|
var increment = 0;
|
||||||
|
|
||||||
|
while (Directory.Exists(GetOutputDirectoryPath(increment, path)))
|
||||||
|
{
|
||||||
|
increment++;
|
||||||
|
}
|
||||||
|
|
||||||
|
var outputDirectoryPath = GetOutputDirectoryPath(increment, path);
|
||||||
|
Directory.CreateDirectory(outputDirectoryPath);
|
||||||
|
return outputDirectoryPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetOutputDirectoryPath(int increment, string path)
|
||||||
|
{
|
||||||
|
var parent = Directory.GetParent(path);
|
||||||
|
var filename = Path.GetFileNameWithoutExtension(path);
|
||||||
|
return increment == 0 ? $"{parent}{Path.DirectorySeparatorChar}legoArtInstructions" : $"{parent}{Path.DirectorySeparatorChar}legoArtInstructions_{filename} ({increment})";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
42
LegoArtTool/BuildingInstruction/GraphicExtensions.cs
Normal file
42
LegoArtTool/BuildingInstruction/GraphicExtensions.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Drawing2D;
|
||||||
|
|
||||||
|
namespace LegoArtTool.BuildingInstruction
|
||||||
|
{
|
||||||
|
public static class GraphicsExtensions
|
||||||
|
{
|
||||||
|
public static void DrawCircle(this Graphics g, Pen pen, float centerX, float centerY, float radius)
|
||||||
|
{
|
||||||
|
g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void FillCircle(this Graphics g, Brush brush, float centerX, float centerY, float radius)
|
||||||
|
{
|
||||||
|
g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void DrawText(this Graphics g, Brush brush, Font font, int size, StringAlignment alignment, string text)
|
||||||
|
{
|
||||||
|
var format = new StringFormat();
|
||||||
|
format.LineAlignment = alignment;
|
||||||
|
format.Alignment = alignment;
|
||||||
|
|
||||||
|
var rectf = new RectangleF(0, 0, size, size);
|
||||||
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
||||||
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||||
|
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||||
|
g.DrawString(text, font, brush, rectf, format);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Font GetIdealFontSize(this Graphics g, int size, string referenceText, Font preferedFont)
|
||||||
|
{
|
||||||
|
var room = new Size(size, size);
|
||||||
|
var realSize = g.MeasureString(referenceText, preferedFont);
|
||||||
|
var heightScaleRatio = room.Height / realSize.Height;
|
||||||
|
var widthScaleRatio = room.Width / realSize.Width;
|
||||||
|
var scaleRatio = (heightScaleRatio < widthScaleRatio) ? heightScaleRatio : widthScaleRatio;
|
||||||
|
var scaleFontSize = preferedFont.Size * scaleRatio;
|
||||||
|
return new Font(preferedFont.FontFamily, scaleFontSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
|
||||||
namespace LegoArtTool
|
namespace LegoArtTool.LegoArtColor
|
||||||
{
|
{
|
||||||
public class LegoArtColorInfo
|
public class LegoArtColorInfo
|
||||||
{
|
{
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
namespace LegoArtTool
|
namespace LegoArtTool.LegoArtColor
|
||||||
{
|
{
|
||||||
public class LegoArtColorOffset
|
public class LegoArtColorOffset
|
||||||
{
|
{
|
||||||
|
|
@ -12,7 +12,5 @@
|
||||||
LegoArtColorInfo = legoArtColorInfo;
|
LegoArtColorInfo = legoArtColorInfo;
|
||||||
IsAvailable = true;
|
IsAvailable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,14 +2,20 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
|
||||||
namespace LegoArtTool
|
namespace LegoArtTool.LegoArtColor
|
||||||
{
|
{
|
||||||
public class LegoArtColorService
|
public class LegoArtColorService
|
||||||
{
|
{
|
||||||
public List<LegoArtColorInfo> GetLegoColors()
|
public List<string> LegoArtSets { get; private set;}
|
||||||
{
|
|
||||||
var legoColors = new List<LegoArtColorInfo>();
|
|
||||||
|
|
||||||
|
private readonly Dictionary<string, List<LegoArtColorInfo>> legoArtColorSets;
|
||||||
|
|
||||||
|
public LegoArtColorService()
|
||||||
|
{
|
||||||
|
legoArtColorSets = new Dictionary<string, List<LegoArtColorInfo>>();
|
||||||
|
|
||||||
|
var key = "LEGO® Art The Beatles #31198";
|
||||||
|
var legoColors = new List<LegoArtColorInfo>();
|
||||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(0, 0, 0), 1, 698));
|
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(0, 0, 0), 1, 698));
|
||||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(102, 101, 96), 2, 141));
|
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(102, 101, 96), 2, 141));
|
||||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(167, 166, 162), 3, 51));
|
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(167, 166, 162), 3, 51));
|
||||||
|
|
@ -25,13 +31,20 @@ namespace LegoArtTool
|
||||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(206, 81, 0), 13, 85));
|
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(206, 81, 0), 13, 85));
|
||||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(119, 40, 0), 14, 250));
|
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(119, 40, 0), 14, 250));
|
||||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(66, 23, 3), 15, 554));
|
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(66, 23, 3), 15, 554));
|
||||||
|
legoArtColorSets.Add(key, legoColors);
|
||||||
|
|
||||||
return legoColors;
|
LegoArtSets = new List<string>();
|
||||||
|
LegoArtSets.Add(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<LegoArtColorInfo> ParseImage(Bitmap bitmap)
|
public List<LegoArtColorInfo> GetLegoArtColorSet(string legoArtSetKey)
|
||||||
{
|
{
|
||||||
var parsedLegoColors = GetLegoColors();
|
return legoArtColorSets[legoArtSetKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<LegoArtColorInfo> ParseImage(Bitmap bitmap, string legoArtSetKey)
|
||||||
|
{
|
||||||
|
var parsedLegoColors = GetLegoArtColorSet(legoArtSetKey);
|
||||||
|
|
||||||
var width = bitmap.Width;
|
var width = bitmap.Width;
|
||||||
var height = bitmap.Height;
|
var height = bitmap.Height;
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace LegoArtTool
|
namespace LegoArtTool.LegoArtColor
|
||||||
{
|
{
|
||||||
public class Pixel
|
public class Pixel
|
||||||
{
|
{
|
||||||
public int X { get; private set; }
|
public int X { get; private set; }
|
||||||
public int Y { get; private set; }
|
public int Y { get; private set; }
|
||||||
public List<LegoArtColorOffset> LegoArtColorOffsets { get; private set; }
|
public List<LegoArtColorOffset> LegoArtColorOffsets { get; private set; }
|
||||||
|
|
||||||
public bool IsAssigned { get; set; }
|
public bool IsAssigned { get; set; }
|
||||||
|
|
||||||
public Pixel(int x, int y, List<LegoArtColorOffset> legoArtColorOffsets)
|
public Pixel(int x, int y, List<LegoArtColorOffset> legoArtColorOffsets)
|
||||||
{
|
{
|
||||||
X = x;
|
X = x;
|
||||||
|
|
@ -17,6 +17,5 @@ namespace LegoArtTool
|
||||||
LegoArtColorOffsets = legoArtColorOffsets;
|
LegoArtColorOffsets = legoArtColorOffsets;
|
||||||
IsAssigned = false;
|
IsAssigned = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using LegoArtTool.LegoArtColor;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -7,16 +8,17 @@ namespace LegoArtTool
|
||||||
{
|
{
|
||||||
public class LegoArtImageGenerationService
|
public class LegoArtImageGenerationService
|
||||||
{
|
{
|
||||||
private readonly List<LegoArtColorInfo> legoArtColorInfos;
|
private readonly LegoArtColorService legoArtColorService;
|
||||||
|
|
||||||
public LegoArtImageGenerationService()
|
public LegoArtImageGenerationService()
|
||||||
{
|
{
|
||||||
var legoArtColorService = new LegoArtColorService();
|
legoArtColorService = new LegoArtColorService();
|
||||||
legoArtColorInfos = legoArtColorService.GetLegoColors();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap GenerateLegoArtImageFromFullColorImage(Bitmap bitmap)
|
public Bitmap GenerateLegoArtImageFromFullColorImage(Bitmap bitmap, string legoArtSetKey)
|
||||||
{
|
{
|
||||||
|
var legoArtColorInfos = legoArtColorService.GetLegoArtColorSet(legoArtSetKey);
|
||||||
|
|
||||||
var width = bitmap.Width;
|
var width = bitmap.Width;
|
||||||
var height = bitmap.Height;
|
var height = bitmap.Height;
|
||||||
|
|
||||||
|
|
@ -27,7 +29,7 @@ namespace LegoArtTool
|
||||||
for (int w = 0; w < width; w++)
|
for (int w = 0; w < width; w++)
|
||||||
{
|
{
|
||||||
var color = bitmap.GetPixel(h, w);
|
var color = bitmap.GetPixel(h, w);
|
||||||
var pixel = MapPixel(color, h ,w);
|
var pixel = MapPixel(legoArtColorInfos, color, h ,w);
|
||||||
pixels.Add(pixel);
|
pixels.Add(pixel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -35,19 +37,19 @@ namespace LegoArtTool
|
||||||
var reducedBitmap = new Bitmap(bitmap.Width, bitmap.Height);
|
var reducedBitmap = new Bitmap(bitmap.Width, bitmap.Height);
|
||||||
|
|
||||||
var remainingColors = legoArtColorInfos.ToDictionary(k => k.LegoNumber, v => v.HaveCount);
|
var remainingColors = legoArtColorInfos.ToDictionary(k => k.LegoNumber, v => v.HaveCount);
|
||||||
|
|
||||||
|
|
||||||
while (pixels.Count(p => !p.IsAssigned) > 0)
|
while (pixels.Count(p => !p.IsAssigned) > 0)
|
||||||
{
|
{
|
||||||
var minOffset = pixels.Where(p => !p.IsAssigned).SelectMany(p => p.LegoArtColorOffsets.Where(o => o.IsAvailable)
|
var minOffset = pixels.Where(p => !p.IsAssigned).SelectMany(p => p.LegoArtColorOffsets.Where(o => o.IsAvailable)
|
||||||
.Select(o => o.ColorOffset))
|
.Select(o => o.ColorOffset))
|
||||||
.Min(x => x);
|
.Min(x => x);
|
||||||
|
|
||||||
var pixel = pixels.Where(p => !p.IsAssigned).First(p => p.LegoArtColorOffsets.Any(o => o.ColorOffset == minOffset));
|
var pixel = pixels.Where(p => !p.IsAssigned).First(p => p.LegoArtColorOffsets.Where(o => o.IsAvailable)
|
||||||
|
.Any(o => o.ColorOffset == minOffset));
|
||||||
var legoArtColorInfo = pixel.LegoArtColorOffsets.First(o => o.ColorOffset == minOffset).LegoArtColorInfo;
|
var legoArtColorInfo = pixel.LegoArtColorOffsets.First(o => o.ColorOffset == minOffset).LegoArtColorInfo;
|
||||||
|
|
||||||
reducedBitmap.SetPixel(pixel.X, pixel.Y, legoArtColorInfo.Color);
|
reducedBitmap.SetPixel(pixel.X, pixel.Y, legoArtColorInfo.Color);
|
||||||
|
|
||||||
|
|
||||||
remainingColors[legoArtColorInfo.LegoNumber]--;
|
remainingColors[legoArtColorInfo.LegoNumber]--;
|
||||||
pixel.IsAssigned = true;
|
pixel.IsAssigned = true;
|
||||||
|
|
||||||
|
|
@ -62,50 +64,21 @@ namespace LegoArtTool
|
||||||
return reducedBitmap;
|
return reducedBitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Pixel MapPixel(
|
private Pixel MapPixel(List<LegoArtColorInfo> legoArtColorInfos, Color inputColor, int xPosition, int yPosition)
|
||||||
Color inputColor,
|
|
||||||
int xPosition,
|
|
||||||
int yPosition)
|
|
||||||
{
|
{
|
||||||
var legoArtColorOffsets = legoArtColorInfos.Select(i => ColorOffset(inputColor, i)).ToList();
|
var legoArtColorOffsets = legoArtColorInfos.Select(i => ColorDistance(inputColor, i)).ToList();
|
||||||
return new Pixel(xPosition, yPosition, legoArtColorOffsets);
|
return new Pixel(xPosition, yPosition, legoArtColorOffsets);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static LegoArtColorOffset ColourDistance(Color inputColor, LegoArtColorInfo colorInfo)
|
private static LegoArtColorOffset ColorDistance(Color inputColor, LegoArtColorInfo colorInfo)
|
||||||
{
|
{
|
||||||
var targetColor = colorInfo.Color;
|
var targetColor = colorInfo.Color;
|
||||||
var rmean = ((long)inputColor.R + (long)targetColor.R) / 2;
|
var rmean = (inputColor.R + (long)targetColor.R) / 2;
|
||||||
var r = (long)inputColor.R - (long)targetColor.R;
|
var r = inputColor.R - (long)targetColor.R;
|
||||||
var g = (long)inputColor.G - (long)targetColor.G;
|
var g = inputColor.G - (long)targetColor.G;
|
||||||
var b = (long)inputColor.B - (long)targetColor.B;
|
var b = inputColor.B - (long)targetColor.B;
|
||||||
var offset = (decimal) Math.Sqrt((((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8));
|
var offset = (decimal) Math.Sqrt((((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8));
|
||||||
return new LegoArtColorOffset((decimal) offset, colorInfo);
|
return new LegoArtColorOffset((decimal) offset, colorInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
private LegoArtColorOffset ColorOffset(Color inputColor, LegoArtColorInfo colorInfo)
|
|
||||||
{
|
|
||||||
var offset = Math.Abs(
|
|
||||||
ColorNum(inputColor) - ColorNum(colorInfo.Color)) +
|
|
||||||
GetHueDistance(inputColor.GetHue(), colorInfo.Color.GetHue());
|
|
||||||
|
|
||||||
return new LegoArtColorOffset((decimal) offset, colorInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static float GetBrightness(Color c)
|
|
||||||
{
|
|
||||||
return (c.R * 0.299f + c.G * 0.587f + c.B *0.114f) / 256f;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static float GetHueDistance(float hue1, float hue2)
|
|
||||||
{
|
|
||||||
var d = Math.Abs(hue1 - hue2);
|
|
||||||
return d > 180 ? 360 - d : d;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static float ColorNum(Color c)
|
|
||||||
{
|
|
||||||
return c.GetSaturation() + GetBrightness(c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
using System.Drawing;
|
|
||||||
|
|
||||||
namespace LegoArtTool.Pixels
|
|
||||||
{
|
|
||||||
public class Pixel
|
|
||||||
{
|
|
||||||
public Color Color { get; private set; }
|
|
||||||
public int Number { get; private set; }
|
|
||||||
public int Size { get; private set; }
|
|
||||||
|
|
||||||
public Pixel(Color color, int size, int number)
|
|
||||||
{
|
|
||||||
Color = color;
|
|
||||||
Size = size;
|
|
||||||
Number = number;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Bitmap GetBitmap()
|
|
||||||
{
|
|
||||||
var bitmap = new Bitmap(Size, Size);
|
|
||||||
for (int x = 0; x < Size; x++)
|
|
||||||
{
|
|
||||||
for (int y = 0; y < Size; y++)
|
|
||||||
{
|
|
||||||
bitmap.SetPixel(x, y, Color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return bitmap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue