Skip to content

Commit 373d35e

Browse files
committed
use additive color mixing, create undo buffer on mousedown
1 parent 55dec66 commit 373d35e

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

PixelArtTool/MainWindow.xaml.cs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ void LoadPalette()
136136
}
137137
}
138138

139+
// used in drawing
139140
void SetPixel(WriteableBitmap bitmap, int x, int y, int color)
140141
{
141142
try
@@ -210,24 +211,44 @@ public struct PixelColor
210211
// unsafe code to write a pixel into the back buffer.
211212
void DrawPixel(int x, int y)
212213
{
213-
/*
214-
int x = (int)(e.GetPosition(drawingImage).X / canvasScaleX);
215-
int y = (int)(e.GetPosition(drawingImage).Y / canvasScaleX);
216-
if (x < 0 || x > canvasResolutionX - 1) return;
217-
if (y < 0 || y > canvasResolutionY - 1) return;*/
218-
219214
if (x < 0 || x > canvasResolutionX - 1) return;
220215
if (y < 0 || y > canvasResolutionY - 1) return;
221216

217+
// get old color
218+
var oc = GetPixelColor(x, y, undoBufferBitmap[currentUndoIndex]);
222219

223-
//currentColorIndex = ++currentColorIndex % palette.Length;
220+
// mix colors ADDITIVE mode
221+
var newc = new PixelColor();
222+
int r = (int)(oc.Red + currentColor.Red * ((float)opacity / (float)255));
223+
int g = (int)(oc.Green + currentColor.Green * ((float)opacity / (float)255));
224+
int b = (int)(oc.Blue + currentColor.Blue * ((float)opacity / (float)255));
224225

225-
SetPixel(canvasBitmap, x, y, (int)currentColor.ColorBGRA);
226+
newc.Red = ClampToByte(r);
227+
newc.Green = ClampToByte(g);
228+
newc.Blue = ClampToByte(b);
229+
newc.Alpha = opacity;
230+
231+
//Console.WriteLine(oc.Red + "+" + currentColor.Red + "*" + (opacity / (float)255) + " = " + newc.Red);
232+
233+
// draw
234+
SetPixel(canvasBitmap, x, y, (int)newc.ColorBGRA);
226235

227236
prevX = x;
228237
prevY = y;
229238
}
230239

240+
byte Clamp(byte n)
241+
{
242+
return n <= 255 ? n : (byte)Math.Min(n, (byte)255);
243+
}
244+
245+
byte ClampToByte(int n)
246+
{
247+
var r = n <= 255 ? n : (byte)Math.Min(n, (byte)255);
248+
return (byte)r;
249+
}
250+
251+
231252
void ErasePixel(MouseEventArgs e)
232253
{
233254
byte[] ColorData = { 0, 0, 0, 0 }; // B G R
@@ -271,6 +292,24 @@ unsafe PixelColor GetPixelColor(int x, int y)
271292
return pix;
272293
}
273294

295+
unsafe PixelColor GetPixelColor(int x, int y, WriteableBitmap source)
296+
{
297+
var pix = new PixelColor();
298+
byte[] ColorData = { 0, 0, 0, 0 }; // B G R !
299+
IntPtr pBackBuffer = source.BackBuffer;
300+
byte* pBuff = (byte*)pBackBuffer.ToPointer();
301+
var b = pBuff[4 * x + (y * source.BackBufferStride)];
302+
var g = pBuff[4 * x + (y * source.BackBufferStride) + 1];
303+
var r = pBuff[4 * x + (y * source.BackBufferStride) + 2];
304+
var a = pBuff[4 * x + (y * source.BackBufferStride) + 3];
305+
pix.Red = r;
306+
pix.Green = g;
307+
pix.Blue = b;
308+
pix.Alpha = a;
309+
return pix;
310+
}
311+
312+
274313
void PaletteLeftButtonDown(object sender, MouseButtonEventArgs e)
275314
{
276315
PickPalette(e);
@@ -305,16 +344,17 @@ void DrawingMiddleButtonDown(object sender, MouseButtonEventArgs e)
305344

306345
void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
307346
{
347+
// undo test
348+
undoBufferBitmap[++currentUndoIndex] = canvasBitmap.Clone();
349+
Console.WriteLine("save undo " + currentUndoIndex);
350+
308351
int x = (int)(e.GetPosition(drawingImage).X / canvasScaleX);
309352
int y = (int)(e.GetPosition(drawingImage).Y / canvasScaleX);
310353
DrawPixel(x, y);
311354
}
312355

313356
void DrawingMouseUp(object sender, MouseButtonEventArgs e)
314357
{
315-
// undo test
316-
undoBufferBitmap[++currentUndoIndex] = canvasBitmap.Clone();
317-
Console.WriteLine("save undo " + currentUndoIndex);
318358
}
319359

320360

0 commit comments

Comments
 (0)