Skip to content

Commit d9e9ea2

Browse files
committed
cleanup, add PixelColor constructor, add PixelColor to SolidColorBrush converter
1 parent 403fdeb commit d9e9ea2

File tree

2 files changed

+32
-48
lines changed

2 files changed

+32
-48
lines changed

PixelArtTool/EnumsAndStructs.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Runtime.InteropServices;
33
using System.Windows;
44
using System.Windows.Data;
5+
using System.Windows.Media;
56

67
namespace PixelArtTool
78
{
@@ -38,6 +39,21 @@ public struct PixelColor
3839
[FieldOffset(1)] public byte Green;
3940
[FieldOffset(2)] public byte Red;
4041
[FieldOffset(3)] public byte Alpha;
42+
43+
public PixelColor(byte r, byte g, byte b, byte a)
44+
{
45+
ColorBGRA = (UInt32)(b + (g << 8) + (r << 16) + (a << 24));
46+
Red = r;
47+
Green = g;
48+
Blue = b;
49+
Alpha = a;
50+
}
51+
52+
public SolidColorBrush AsSolidColorBrush()
53+
{
54+
return new SolidColorBrush(Color.FromArgb(Alpha, Red, Green, Blue));
55+
}
56+
4157
}
4258

4359
// helper for converting bool<>enum for xaml linked values

PixelArtTool/MainWindow.xaml.cs

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -610,12 +610,7 @@ void WindowMouseWheel(object sender, MouseWheelEventArgs e)
610610
if (hueIndicatorLocation > 1) hueIndicatorLocation = 1;
611611

612612
var c = GetColorByOffset(currentBrightnessBrushGradient.GradientStops, hueIndicatorLocation);
613-
var cc = new PixelColor();
614-
cc.Red = c.R;
615-
cc.Green = c.G;
616-
cc.Blue = c.B;
617-
cc.Alpha = 255;
618-
currentColor = cc;
613+
currentColor = new PixelColor(c.R, c.G, c.B, 255);
619614
SetCurrentColorPreviewBox(rectCurrentColor, currentColor);
620615
//ResetCurrentBrightnessPreview(currentColor);
621616

@@ -693,34 +688,24 @@ private void OnModeSelectionChanged(object sender, SelectionChangedEventArgs e)
693688
// if key is pressed down globally
694689
void OnKeyDown(object sender, KeyEventArgs e)
695690
{
696-
// TODO: add tool shortcut keys
697691
switch (e.Key)
698692
{
699-
case Key.I: // TEST global color picker
700-
CustomPoint cursor;
701-
GetCursorPos(out cursor);
702-
var c1 = Win32GetScreenPixel((int)cursor.X, (int)cursor.Y);
703-
var c2 = new PixelColor();
704-
c2.Alpha = c1.A;
705-
c2.Red = c1.R;
706-
c2.Green = c1.G;
707-
c2.Blue = c1.B;
708-
currentColor = c2;
709-
rectCurrentColor.Fill = new SolidColorBrush(Color.FromArgb(c2.Alpha, c2.Red, c2.Green, c2.Blue));
710-
// Console.WriteLine(cursor.X + "," + cursor.Y + " = " + c1);
693+
case Key.D: // reset to default colors (current, secondary)
694+
currentColor = new PixelColor(255, 255, 255, 255); // white
695+
SetCurrentColorPreviewBox(rectCurrentColor, currentColor);
696+
rectSecondaryColor.Fill = new PixelColor(0, 0, 0, 255).AsSolidColorBrush();
697+
break;
698+
case Key.I: // global color picker
699+
currentColor = Win32GetScreenPixelColor();
700+
rectCurrentColor.Fill = currentColor.AsSolidColorBrush();
711701
break;
712702
case Key.X: // swap current/secondary colors
713703
var tempcolor = rectCurrentColor.Fill;
714704
rectCurrentColor.Fill = rectSecondaryColor.Fill;
715705
rectSecondaryColor.Fill = tempcolor;
716706
// TODO move to converter
717-
var c = new PixelColor();
718707
var t = ((SolidColorBrush)rectCurrentColor.Fill).Color;
719-
c.Red = t.R;
720-
c.Green = t.G;
721-
c.Blue = t.B;
722-
c.Alpha = t.A;
723-
currentColor = c;
708+
currentColor = new PixelColor(t.R, t.G, t.B, t.A);
724709
break;
725710
case Key.B: // brush
726711
CurrentTool = ToolMode.Draw;
@@ -899,13 +884,8 @@ void OnPasteImageFromClipboard()
899884
for (int y = 0; y < canvasResolutionY; y++)
900885
{
901886
var cc = GetPixelColor(x, y, target);
902-
Console.WriteLine();
903-
var ccc = new PixelColor();
904-
ccc.Red = cc.Red;
905-
ccc.Green = cc.Green;
906-
ccc.Blue = cc.Blue;
907-
ccc.Alpha = 255;
908-
SetPixel(canvasBitmap, x, y, (int)ccc.ColorBGRA);
887+
cc.Alpha = 255;
888+
SetPixel(canvasBitmap, x, y, (int)cc.ColorBGRA);
909889
}
910890
}
911891
}
@@ -1186,13 +1166,9 @@ private void rectHueBar_MouseDown(object sender, MouseButtonEventArgs e)
11861166

11871167
private void OnGetTransparentColorButton(object sender, MouseButtonEventArgs e)
11881168
{
1189-
var c = new PixelColor();
1190-
c.Red = 255;
1191-
c.Green = 255;
1192-
c.Blue = 255;
1193-
c.Alpha = 0;
1169+
var c = new PixelColor(255,255,255,0);
11941170
currentColor = c;
1195-
rectCurrentColor.Fill = new SolidColorBrush(Color.FromArgb(c.Alpha, c.Red, c.Green, c.Blue));
1171+
rectCurrentColor.Fill = c.AsSolidColorBrush();
11961172
ResetCurrentBrightnessPreview(currentColor);
11971173
}
11981174

@@ -1216,16 +1192,8 @@ private void OnLevelSaturationMouseMoved(object sender, MouseEventArgs e)
12161192

12171193
private void OnLevelSaturationMouseDown(object sender, MouseButtonEventArgs e)
12181194
{
1219-
CustomPoint cursor;
1220-
GetCursorPos(out cursor);
1221-
var c1 = Win32GetScreenPixel((int)cursor.X, (int)cursor.Y);
1222-
var c2 = new PixelColor();
1223-
c2.Alpha = c1.A;
1224-
c2.Red = c1.R;
1225-
c2.Green = c1.G;
1226-
c2.Blue = c1.B;
1227-
currentColor = c2;
1228-
rectCurrentColor.Fill = new SolidColorBrush(Color.FromArgb(c2.Alpha, c2.Red, c2.Green, c2.Blue));
1195+
currentColor = Win32GetScreenPixelColor();
1196+
rectCurrentColor.Fill = currentColor.AsSolidColorBrush();
12291197
ResetCurrentBrightnessPreview(currentColor);
12301198
}
12311199

0 commit comments

Comments
 (0)