Skip to content

Commit f1e7a4f

Browse files
committed
adding colorpicker window, adding settings window
1 parent 1b761fd commit f1e7a4f

9 files changed

+295
-12
lines changed

PixelArtTool/ColorPicker.xaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<Window x:Class="PixelArtTool.ColorPicker"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:PixelArtTool"
7+
mc:Ignorable="d"
8+
Title="ColorPicker" Height="318" Width="346" Background="#FF252525">
9+
<Window.Resources>
10+
<!-- Change this to any pure hue i.e. no more than 2 rgb components set and at least 1 set to FF -->
11+
<Color x:Key="CurrentColor">#00FF00</Color>
12+
13+
<VisualBrush x:Key="LevelSaturationBrush" TileMode="None">
14+
<VisualBrush.Visual>
15+
<Canvas Background="Black" Width="1" Height="1" SnapsToDevicePixels="True">
16+
<Rectangle x:Name="rectGradientSingle" Width="1" Height="1" SnapsToDevicePixels="True">
17+
<Rectangle.Fill>
18+
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
19+
<GradientStop Color="White" Offset="0" />
20+
<GradientStop Color="{DynamicResource CurrentColor}" Offset="1" />
21+
</LinearGradientBrush>
22+
</Rectangle.Fill>
23+
<Rectangle.OpacityMask>
24+
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
25+
<GradientStop Color="#FFFFFFFF" Offset="0"/>
26+
<GradientStop Color="#00FFFFFF" Offset="1"/>
27+
</LinearGradientBrush>
28+
</Rectangle.OpacityMask>
29+
</Rectangle>
30+
</Canvas>
31+
</VisualBrush.Visual>
32+
</VisualBrush>
33+
34+
<LinearGradientBrush x:Key="HueBrush" StartPoint="0,0" EndPoint="0,1">
35+
<GradientStop Color="#FF0000" Offset="0" />
36+
<GradientStop Color="#FFFF00" Offset="0.167" />
37+
<GradientStop Color="#00FF00" Offset="0.333" />
38+
<GradientStop Color="#00FFFF" Offset="0.5" />
39+
<GradientStop Color="#0000FF" Offset="0.667" />
40+
<GradientStop Color="#FF00FF" Offset="0.833" />
41+
<GradientStop Color="#FF0000" Offset="1" />
42+
</LinearGradientBrush>
43+
44+
</Window.Resources>
45+
<Grid>
46+
<Rectangle x:Name="tempRect" Width="200" Height="200" Margin="58,24,0,0" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" UseLayoutRounding="False" Fill="Black" />
47+
<Rectangle x:Name="rectSaturation" Fill="{StaticResource LevelSaturationBrush}" Width="200" Height="200" Margin="58,24,0,0" StrokeThickness="1" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" MouseMove="OnLevelSaturationMouseMoved" MouseDown="OnLevelSaturationMouseDown" />
48+
<Rectangle x:Name="rectHueBar" Fill="{StaticResource HueBrush}" Width="20" Height="200" Margin="263,24,0,0" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" MouseMove="OnHueRectangleMouseMoved" MouseDown="rectHueBar_MouseDown" />
49+
<Rectangle x:Name="rectCurrentColor" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="28" Margin="10,196,0,0" Stroke="Black" VerticalAlignment="Top" Width="28"/>
50+
<Button x:Name="okButton" IsDefault="True" Content="OK" Click="OnOkButtonClick" Margin="258,233,5,4"/>
51+
<Button x:Name="cancelButton" IsCancel="True" Content="Cancel" Margin="178,233,85,4"/>
52+
53+
</Grid>
54+
</Window>

PixelArtTool/ColorPicker.xaml.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System.Windows;
2+
using System.Windows.Input;
3+
using System.Windows.Media;
4+
using static PixelArtTool.Tools;
5+
6+
namespace PixelArtTool
7+
{
8+
/// <summary>
9+
/// Interaction logic for ColorPicker.xaml
10+
/// </summary>
11+
public partial class ColorPicker : Window
12+
{
13+
public ColorPicker()
14+
{
15+
InitializeComponent();
16+
}
17+
18+
private void OnOkButtonClick(object sender, RoutedEventArgs e)
19+
{
20+
this.DialogResult = true;
21+
}
22+
23+
private void OnLevelSaturationMouseMoved(object sender, MouseEventArgs e)
24+
{
25+
if (rectSaturation.IsMouseOver == false) return;
26+
if (e.LeftButton == MouseButtonState.Pressed) OnLevelSaturationMouseDown(null, null);
27+
}
28+
29+
private void rectHueBar_MouseDown(object sender, MouseButtonEventArgs e)
30+
{
31+
CustomPoint cursor;
32+
GetCursorPos(out cursor);
33+
var c = Win32GetScreenPixel((int)cursor.X, (int)cursor.Y);
34+
//Console.WriteLine("color:"+c);
35+
var f = rectSaturation.Fill;
36+
37+
// build hue gradient
38+
LinearGradientBrush myBrush = new LinearGradientBrush();
39+
var c1 = new Color();
40+
c1.R = 255;
41+
c1.G = 255;
42+
c1.B = 255;
43+
c1.A = 255;
44+
var c2 = new Color();
45+
c2.R = c.R;
46+
c2.G = c.G;
47+
c2.B = c.B;
48+
c2.A = 255;
49+
myBrush.StartPoint = new Point(0, 0);
50+
myBrush.EndPoint = new Point(1, 0);
51+
52+
var g1 = new GradientStop(c1, 0.0);
53+
myBrush.GradientStops.Add(g1);
54+
55+
var g2 = new GradientStop(c2, 1.0);
56+
myBrush.GradientStops.Add(g2);
57+
rectSaturation.Fill = myBrush;
58+
59+
// set opacity mask
60+
var opacityBrush = new LinearGradientBrush();
61+
opacityBrush.StartPoint = new Point(0, 0);
62+
opacityBrush.EndPoint = new Point(0, 1);
63+
var g1b = new GradientStop(c1, 0.0);
64+
opacityBrush.GradientStops.Add(g1b);
65+
c2.A = 0;
66+
c2.R = 0;
67+
c2.G = 0;
68+
c2.B = 0;
69+
var g2b = new GradientStop(c2, 1.0);
70+
opacityBrush.GradientStops.Add(g2b);
71+
rectSaturation.OpacityMask = opacityBrush;
72+
73+
}
74+
75+
private void OnLevelSaturationMouseDown(object sender, MouseButtonEventArgs e)
76+
{
77+
CustomPoint cursor;
78+
GetCursorPos(out cursor);
79+
var c1 = Win32GetScreenPixel((int)cursor.X, (int)cursor.Y);
80+
var c2 = new PixelColor();
81+
c2.Alpha = c1.A;
82+
c2.Red = c1.R;
83+
c2.Green = c1.G;
84+
c2.Blue = c1.B;
85+
//currentColor = c2;
86+
rectCurrentColor.Fill = new SolidColorBrush(Color.FromArgb(c2.Alpha, c2.Red, c2.Green, c2.Blue));
87+
//ResetCurrentBrightnessPreview(currentColor);
88+
}
89+
90+
private void OnHueRectangleMouseMoved(object sender, MouseEventArgs e)
91+
{
92+
if (rectHueBar.IsMouseOver == false) return;
93+
if (e.LeftButton == MouseButtonState.Pressed) rectHueBar_MouseDown(null, null);
94+
}
95+
}
96+
}

PixelArtTool/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
</Rectangle.Fill>
242242
</Rectangle>
243243
<Rectangle x:Name="rectPixelPos" IsHitTestVisible="False" HorizontalAlignment="Left" VerticalAlignment="Top" Height="16" Stroke="Black" Width="16" Margin="90,50,0,0"/>
244+
<Button x:Name="btnSettings" Content="Settings" HorizontalAlignment="Left" Margin="675,6,0,0" VerticalAlignment="Top" Width="50" Click="btnSettings_Click"/>
244245

245246
</Grid>
246247
</Window>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Windows.Media;
1212
using System.Windows.Media.Imaging;
1313
using static PixelArtTool.Tools;
14+
1415
namespace PixelArtTool
1516
{
1617
public partial class MainWindow : Window, INotifyPropertyChanged
@@ -100,7 +101,8 @@ public bool IsModified
100101
{
101102
window.Title = window.Title + "*";
102103
}
103-
} else // not modified, remove mark
104+
}
105+
else // not modified, remove mark
104106
{
105107
if (window.Title.IndexOf("*") > -1)
106108
{
@@ -401,7 +403,8 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
401403
previousToolMode = CurrentTool;
402404
CurrentTool = ToolMode.Fill;
403405
wasDoubleClick = true;
404-
} else // keep old color
406+
}
407+
else // keep old color
405408
{
406409
previousPixelColor = GetPixel(x, y);
407410
}
@@ -508,15 +511,17 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
508511
break;
509512
}
510513

511-
} else if (e.RightButton == MouseButtonState.Pressed)
514+
}
515+
else if (e.RightButton == MouseButtonState.Pressed)
512516
{
513517
ErasePixel(x, y);
514518
// mirror
515519
if (chkMirrorX.IsChecked == true)
516520
{
517521
ErasePixel(canvasResolutionX - x, y);
518522
}
519-
} else if (e.MiddleButton == MouseButtonState.Pressed)
523+
}
524+
else if (e.MiddleButton == MouseButtonState.Pressed)
520525
{
521526
currentColor = GetPixel(x, y);
522527
ResetCurrentBrightnessPreview(currentColor);
@@ -640,7 +645,8 @@ private void OnSaveButton(object sender, RoutedEventArgs e)
640645
{
641646
SaveImageAsPng(saveFile);
642647
IsModified = false;
643-
} else // save as
648+
}
649+
else // save as
644650
{
645651
if (saveFileDialog.ShowDialog() == true)
646652
{
@@ -1031,11 +1037,13 @@ void UpdateOutline()
10311037
if (centerPix == 0)
10321038
{
10331039
c.Alpha = 255;
1034-
} else
1040+
}
1041+
else
10351042
{
10361043
c.Alpha = 0;
10371044
}
1038-
} else
1045+
}
1046+
else
10391047
{
10401048
c.Alpha = 0;
10411049
}
@@ -1130,7 +1138,8 @@ private void chkOutline_Click(object sender, RoutedEventArgs e)
11301138
if (chkOutline.IsChecked == true)
11311139
{
11321140
UpdateOutline();
1133-
} else // clear
1141+
}
1142+
else // clear
11341143
{
11351144
ClearImage(outlineBitmap, emptyRect, emptyPixels, emptyStride);
11361145
}
@@ -1281,7 +1290,7 @@ private void DrawLine(int startX, int startY, int endX, int endY)
12811290
DrawPixel(startX, y);
12821291
// yield return new Coord(startX, y);
12831292

1284-
// yield break;
1293+
// yield break;
12851294
return;
12861295
}
12871296

@@ -1345,6 +1354,23 @@ private void DrawLine(int startX, int startY, int endX, int endY)
13451354
}
13461355
}
13471356

1357+
// show settings window
1358+
private void btnSettings_Click(object sender, RoutedEventArgs e)
1359+
{
1360+
var dlg = new Settings();
1361+
dlg.Owner = this;
1362+
var result = dlg.ShowDialog();
1363+
switch (result)
1364+
{
1365+
case true: // ok
1366+
break;
1367+
case false: // cancelled
1368+
break;
1369+
default:
1370+
Console.WriteLine("Unknown error..");
1371+
break;
1372+
}
1373+
}
13481374
} // class
13491375

13501376
} // namespace

PixelArtTool/NewImageDialog.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Title="New Image" Height="201" Width="281"
1010
ResizeMode="NoResize"
1111
ShowInTaskbar="False"
12-
WindowStartupLocation="CenterOwner"
12+
WindowStartupLocation="CenterOwner" Background="#FF252525"
1313
>
1414
<Grid>
1515
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4" FocusManager.FocusedElement="{Binding ElementName=sliderResolution}">
@@ -20,8 +20,8 @@
2020
<Run Text="{Binding ElementName=sliderResolution, Path=Value, StringFormat={}{0:#}}"/>
2121
</TextBlock>
2222
<Slider x:Name="sliderResolution" Minimum="8" Maximum="64" SmallChange="8" TickFrequency="8" LargeChange="8" TickPlacement="Both" Value="16" AutoToolTipPlacement="TopLeft" IsSnapToTickEnabled="True"/>
23-
<Button x:Name="okButton" IsDefault="True" Content="OK" Click="OnOkButtonClick"/>
24-
<Button x:Name="cancelButton" IsCancel="True" Content="Cancel"/>
23+
<Button x:Name="okButton" IsDefault="True" Content="OK" Click="OnOkButtonClick" Height="40"/>
24+
<Button x:Name="cancelButton" IsCancel="True" Content="Cancel" Margin="0,5,0,0"/>
2525
</StackPanel>
2626
</Grid>
2727
</Window>

PixelArtTool/NewImageDialog.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public partial class NewImageDialog : Window
1010
public NewImageDialog()
1111
{
1212
InitializeComponent();
13+
sliderResolution.Focus();
1314
}
1415

1516
private void OnOkButtonClick(object sender, RoutedEventArgs e)

PixelArtTool/PixelArtTool.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@
5959
<Generator>MSBuild:Compile</Generator>
6060
<SubType>Designer</SubType>
6161
</ApplicationDefinition>
62+
<Compile Include="ColorPicker.xaml.cs">
63+
<DependentUpon>ColorPicker.xaml</DependentUpon>
64+
</Compile>
6265
<Compile Include="NewImageDialog.xaml.cs">
6366
<DependentUpon>NewImageDialog.xaml</DependentUpon>
6467
</Compile>
68+
<Page Include="ColorPicker.xaml">
69+
<SubType>Designer</SubType>
70+
<Generator>MSBuild:Compile</Generator>
71+
</Page>
6572
<Page Include="MainWindow.xaml">
6673
<Generator>MSBuild:Compile</Generator>
6774
<SubType>Designer</SubType>
@@ -71,6 +78,9 @@
7178
<SubType>Code</SubType>
7279
</Compile>
7380
<Compile Include="EnumsAndStructs.cs" />
81+
<Compile Include="Settings.xaml.cs">
82+
<DependentUpon>Settings.xaml</DependentUpon>
83+
</Compile>
7484
<Compile Include="Tools.cs" />
7585
<Compile Include="MainWindow.xaml.cs">
7686
<DependentUpon>MainWindow.xaml</DependentUpon>
@@ -80,6 +90,10 @@
8090
<SubType>Designer</SubType>
8191
<Generator>MSBuild:Compile</Generator>
8292
</Page>
93+
<Page Include="Settings.xaml">
94+
<SubType>Designer</SubType>
95+
<Generator>MSBuild:Compile</Generator>
96+
</Page>
8397
</ItemGroup>
8498
<ItemGroup>
8599
<Compile Include="Properties\AssemblyInfo.cs">

PixelArtTool/Settings.xaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Window
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:PixelArtTool"
7+
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="PixelArtTool.Settings"
8+
mc:Ignorable="d"
9+
Title="Settings" Height="330" Width="300" Background="#FF252525">
10+
<Grid>
11+
<StackPanel>
12+
<Separator Height="20" Margin="0"/>
13+
<StackPanel Orientation="Horizontal">
14+
<CheckBox Content="Show grid"/>
15+
<Label>Light Color</Label>
16+
<Rectangle x:Name="settingsLightColor" Fill="#FFFFFFFF" HorizontalAlignment="Left" Height="28" Stroke="Black" VerticalAlignment="Top" Width="28" MouseDown="settingsLightColor_MouseDown"/>
17+
<Label>Dark Color</Label>
18+
<Rectangle x:Name="settingsDarkColor" Fill="#00000000" HorizontalAlignment="Left" Height="28" Stroke="Black" VerticalAlignment="Top" Width="28" MouseDown="settingsDarkColor_MouseDown"/>
19+
</StackPanel>
20+
21+
<Separator Height="20" Margin="0"/>
22+
<Label>Default Resolution</Label>
23+
<TextBlock HorizontalAlignment="Center" FontWeight="Bold" Margin="117.07,0,133.93,0"><Run Text="{Binding Value, ElementName=sliderResolution, StringFormat=\{0:#\}}"/><Run Text=" "/><Run Text="x"/><Run Text=" "/><Run Text="{Binding Value, ElementName=sliderResolution, StringFormat=\{0:#\}}"/></TextBlock>
24+
<Slider x:Name="sliderResolution" Minimum="8" Maximum="64" SmallChange="8" TickFrequency="8" LargeChange="8" TickPlacement="Both" Value="16" AutoToolTipPlacement="TopLeft" IsSnapToTickEnabled="True" Margin="0,0,0,0"/>
25+
26+
<Separator Height="20" Margin="0"/>
27+
<Rectangle x:Name="defaultColor" Fill="#FF000000" HorizontalAlignment="Left" Height="28" Stroke="Black" VerticalAlignment="Top" Width="28"/>
28+
</StackPanel>
29+
</Grid>
30+
</Window>

0 commit comments

Comments
 (0)