Skip to content

Commit a2efa46

Browse files
committed
add swap eraser color (shift+x), pixelcolor from color, erase with eraserColor, move get transparent color rectangle to near color picker area, fix outline canvas 1px offset, add separate outline border for canvas
1 parent 7945cbe commit a2efa46

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

PixelArtTool/EnumsAndStructs.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public PixelColor(SolidColorBrush b)
5858
Alpha = b.Color.A;
5959
}
6060

61+
public PixelColor(Color b)
62+
{
63+
ColorBGRA = (UInt32)(b.B + (b.G << 8) + (b.R << 16) + (b.A << 24));
64+
Red = b.R;
65+
Green = b.G;
66+
Blue = b.B;
67+
Alpha = b.A;
68+
}
69+
70+
6171
public SolidColorBrush AsSolidColorBrush()
6272
{
6373
return new SolidColorBrush(Color.FromArgb(Alpha, Red, Green, Blue));

PixelArtTool/MainWindow.xaml

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,23 @@
167167
<StatusBarItem Height="30" VerticalAlignment="Center">
168168
<Label x:Name="lblToolInfo" Content="-"/>
169169
</StatusBarItem>
170+
<StatusBarItem Height="30" VerticalAlignment="Center">
171+
<Label x:Name="lblCtrlInfo" Content="-"/>
172+
</StatusBarItem>
170173
</StatusBar>
171174
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="198" Margin="10,50,0,0" VerticalAlignment="Top" Width="64">
172175
<Image x:Name="paletteImage" HorizontalAlignment="Left" Height="198" Margin="0,0,0,-30" VerticalAlignment="Top" Width="64" Stretch="Fill" MouseLeftButtonDown="PaletteLeftButtonDown" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
173176
</Border>
174177
<Grid>
178+
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
179+
</Border>
175180
<Border BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
176181
<Image x:Name="gridImage" HorizontalAlignment="Left" Height="256" VerticalAlignment="Top" Width="256" Stretch="Fill" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
177182
</Border>
178183
<Border BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
179184
<Image x:Name="drawingImage" HorizontalAlignment="Left" Height="256" VerticalAlignment="Top" Width="256" Stretch="Fill" MouseMove="DrawingAreaMouseMoved" MouseRightButtonDown="DrawingRightButtonDown" MouseLeftButtonDown="DrawingLeftButtonDown" MouseDown="DrawingMiddleButtonDown" MouseUp="DrawingMouseUp" RenderOptions.BitmapScalingMode="NearestNeighbor" MouseLeave="drawingImage_MouseLeave" MouseEnter="drawingImage_MouseEnter"/>
180185
</Border>
181-
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
186+
<Border BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
182187
<Image x:Name="outlineImage" IsHitTestVisible="False" HorizontalAlignment="Left" Height="256" VerticalAlignment="Top" Width="256" Stretch="Fill" IsEnabled="False" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
183188
</Border>
184189
<Rectangle x:Name="recPreviewBg2x" Fill="Black" HorizontalAlignment="Left" Height="32" Margin="375,50,0,0" VerticalAlignment="Top" Width="32"/>
@@ -222,6 +227,12 @@
222227
<Rectangle x:Name="tempRect" Width="200" Height="200" Margin="459,130,0,0" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" UseLayoutRounding="False" Fill="Black" />
223228
<Rectangle x:Name="rectSaturation" Fill="{StaticResource LevelSaturationBrush}" Width="200" Height="200" Margin="459,130,0,0" StrokeThickness="1" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" MouseDown="OnLevelSaturationMouseDown" MouseMove="OnLevelSaturationMouseMoved" />
224229
<Rectangle x:Name="rectHueBar" Fill="{StaticResource HueBrush}" Width="20" Height="200" Margin="664,130,0,0" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" MouseDown="rectHueBar_MouseDown" HorizontalAlignment="Left" VerticalAlignment="Top" MouseMove="OnHueRectangleMouseMoved" />
230+
<Rectangle x:Name="rectGetTransparentColor" HorizontalAlignment="Left" Height="28" Margin="547,97,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" MouseDown="OnGetTransparentColorButton">
231+
<Rectangle.Fill>
232+
<ImageBrush ImageSource="Resources/Images/transparentbg.png"/>
233+
</Rectangle.Fill>
234+
</Rectangle>
235+
225236
<Grid>
226237
<Rectangle x:Name="rectCurrentHue" Width="253" Height="14" Margin="91,316,0,0" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" >
227238
<Rectangle.Fill>
@@ -235,12 +246,22 @@
235246
<Line x:Name="lineSymmetryXpositionA" X1="225" Y1="40" X2="225" Y2="50" Stroke="White" StrokeThickness="1" Visibility="Visible" />
236247
<Line x:Name="lineSymmetryXpositionB" X1="225" Y1="312" X2="225" Y2="306" Stroke="White" StrokeThickness="1" Visibility="Visible" />
237248
</Grid>
238-
<Rectangle x:Name="rectGetTransparent" HorizontalAlignment="Left" Height="28" Margin="10,289,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" MouseDown="OnGetTransparentColorButton">
239-
<Rectangle.Fill>
240-
<ImageBrush ImageSource="Resources/Images/transparentbg.png"/>
241-
</Rectangle.Fill>
242-
</Rectangle>
243-
<Rectangle x:Name="rectGetTransparentSecondary" HorizontalAlignment="Left" Height="28" Margin="47,289,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" Fill="Black" />
249+
<Grid>
250+
<Rectangle x:Name="rectGetTransparentBg" HorizontalAlignment="Left" Height="28" Margin="10,289,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" MouseDown="OnGetTransparentColorButton">
251+
<Rectangle.Fill>
252+
<ImageBrush ImageSource="Resources/Images/transparentbg.png"/>
253+
</Rectangle.Fill>
254+
</Rectangle>
255+
<Rectangle x:Name="rectEraserColor" HorizontalAlignment="Left" Height="28" Margin="10,289,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" Fill="#00000000"/>
256+
</Grid>
257+
<Grid>
258+
<Rectangle x:Name="rectGetTransparentSecondaryBg" HorizontalAlignment="Left" Height="28" Margin="47,289,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" MouseDown="OnGetTransparentColorButton">
259+
<Rectangle.Fill>
260+
<ImageBrush ImageSource="Resources/Images/transparentbg.png"/>
261+
</Rectangle.Fill>
262+
</Rectangle>
263+
<Rectangle x:Name="rectEraserColorSecondary" HorizontalAlignment="Left" Height="28" Margin="47,289,0,0" Stroke="Black" VerticalAlignment="Top" Width="28" Fill="Black" />
264+
</Grid>
244265

245266
<Rectangle x:Name="rectPixelPos" IsHitTestVisible="False" HorizontalAlignment="Left" VerticalAlignment="Top" Height="16" Stroke="Black" Width="16" Margin="89,50,0,0"/>
246267
<Button x:Name="btnSettings" Content="Settings" HorizontalAlignment="Left" Margin="674,6,0,0" VerticalAlignment="Top" Width="50" Click="btnSettings_Click"/>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public partial class MainWindow : Window, INotifyPropertyChanged
4444

4545
// colors
4646
PixelColor currentColor;
47+
PixelColor eraseColor = new PixelColor(0, 0, 0, 0);
48+
4749
PixelColor[] palette;
4850
PixelColor lightColor;
4951
PixelColor darkColor;
@@ -59,7 +61,6 @@ public partial class MainWindow : Window, INotifyPropertyChanged
5961
bool leftShiftDown = false;
6062
bool leftCtrlDown = false;
6163

62-
6364
// drawing lines
6465
private readonly int ddaMODIFIER_X = 0x7fff;
6566
private readonly int ddaMODIFIER_Y = 0x7fff;
@@ -149,7 +150,7 @@ void Start(bool loadSettings = true)
149150
darkColor.Alpha = gridAlpha;
150151

151152
// get values from settings
152-
if (loadSettings==true) LoadSettings();
153+
if (loadSettings == true) LoadSettings();
153154

154155
// setup background grid
155156
gridBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
@@ -250,12 +251,13 @@ void DrawPixel(int x, int y)
250251

251252
void ErasePixel(int x, int y)
252253
{
253-
byte[] ColorData = { 0, 0, 0, 0 }; // B G R
254+
// byte[] ColorData = { 0, 0, 0, 0 }; // B G R
254255
if (x < 0 || x > canvasResolutionX - 1) return;
255256
if (y < 0 || y > canvasResolutionY - 1) return;
256257

257-
Int32Rect rect = new Int32Rect(x, y, 1, 1);
258-
canvasBitmap.WritePixels(rect, ColorData, 4, 0);
258+
// Int32Rect rect = new Int32Rect(x, y, 1, 1);
259+
// canvasBitmap.WritePixels(rect, ColorData, 4, 0);
260+
SetPixel(canvasBitmap, x, y, (int)eraseColor.ColorBGRA);
259261
}
260262

261263
void PickPalette(MouseEventArgs e)
@@ -713,12 +715,20 @@ void OnKeyDown(object sender, KeyEventArgs e)
713715
rectCurrentColor.Fill = currentColor.AsSolidColorBrush();
714716
break;
715717
case Key.X: // swap current/secondary colors
716-
var tempcolor = rectCurrentColor.Fill;
717-
rectCurrentColor.Fill = rectSecondaryColor.Fill;
718-
rectSecondaryColor.Fill = tempcolor;
719-
// TODO move to converter
720-
var t = ((SolidColorBrush)rectCurrentColor.Fill).Color;
721-
currentColor = new PixelColor(t.R, t.G, t.B, t.A);
718+
if (leftShiftDown == true) // swap eraser colors
719+
{
720+
var tempcolor = rectEraserColor.Fill;
721+
rectEraserColor.Fill = rectEraserColorSecondary.Fill;
722+
rectEraserColorSecondary.Fill = tempcolor;
723+
eraseColor = new PixelColor(((SolidColorBrush)rectEraserColor.Fill).Color);
724+
}
725+
else // regular color
726+
{
727+
var tempcolor = rectCurrentColor.Fill;
728+
rectCurrentColor.Fill = rectSecondaryColor.Fill;
729+
rectSecondaryColor.Fill = tempcolor;
730+
currentColor = new PixelColor(((SolidColorBrush)rectCurrentColor.Fill).Color);
731+
}
722732
break;
723733
case Key.B: // brush
724734
CurrentTool = ToolMode.Draw;
@@ -732,6 +742,7 @@ void OnKeyDown(object sender, KeyEventArgs e)
732742
break;
733743
case Key.LeftCtrl: // left control
734744
leftCtrlDown = true;
745+
lblCtrlInfo.Content = "Double click to replace target colors";
735746
break;
736747
default:
737748
break;
@@ -748,6 +759,7 @@ private void OnKeyUp(object sender, KeyEventArgs e)
748759
break;
749760
case Key.LeftCtrl:
750761
leftCtrlDown = false;
762+
lblCtrlInfo.Content = "-";
751763
break;
752764
default:
753765
break;
@@ -1390,7 +1402,7 @@ private void drawingImage_MouseEnter(object sender, MouseEventArgs e)
13901402
private void OnClearButton(object sender, MouseButtonEventArgs e)
13911403
{
13921404
// shiftdown or right button, just clear without dialog
1393-
if (leftShiftDown == true || (e!=null && e.RightButton == MouseButtonState.Pressed))
1405+
if (leftShiftDown == true || (e != null && e.RightButton == MouseButtonState.Pressed))
13941406
{
13951407
ClearImage(canvasBitmap, emptyRect, emptyPixels, emptyStride);
13961408
UpdateOutline();
@@ -1429,7 +1441,6 @@ private void OnClearButton(object sender, MouseButtonEventArgs e)
14291441
Console.WriteLine("Unknown error..");
14301442
break;
14311443
}
1432-
14331444
}
14341445

14351446
public void ReplacePixels(PixelColor find, PixelColor replace)
@@ -1442,12 +1453,10 @@ public void ReplacePixels(PixelColor find, PixelColor replace)
14421453

14431454
if (pixel == find)
14441455
{
1445-
SetPixel(outlineBitmap, x, y, (int)replace.ColorBGRA);
1456+
SetPixel(canvasBitmap, x, y, (int)replace.ColorBGRA);
14461457
}
1447-
14481458
}
14491459
}
1450-
14511460
}
14521461

14531462
// current color box

0 commit comments

Comments
 (0)