@@ -136,6 +136,7 @@ void LoadPalette()
136
136
}
137
137
}
138
138
139
+ // used in drawing
139
140
void SetPixel ( WriteableBitmap bitmap , int x , int y , int color )
140
141
{
141
142
try
@@ -210,24 +211,44 @@ public struct PixelColor
210
211
// unsafe code to write a pixel into the back buffer.
211
212
void DrawPixel ( int x , int y )
212
213
{
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
-
219
214
if ( x < 0 || x > canvasResolutionX - 1 ) return ;
220
215
if ( y < 0 || y > canvasResolutionY - 1 ) return ;
221
216
217
+ // get old color
218
+ var oc = GetPixelColor ( x , y , undoBufferBitmap [ currentUndoIndex ] ) ;
222
219
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 ) ) ;
224
225
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 ) ;
226
235
227
236
prevX = x ;
228
237
prevY = y ;
229
238
}
230
239
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
+
231
252
void ErasePixel ( MouseEventArgs e )
232
253
{
233
254
byte [ ] ColorData = { 0 , 0 , 0 , 0 } ; // B G R
@@ -271,6 +292,24 @@ unsafe PixelColor GetPixelColor(int x, int y)
271
292
return pix ;
272
293
}
273
294
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
+
274
313
void PaletteLeftButtonDown ( object sender , MouseButtonEventArgs e )
275
314
{
276
315
PickPalette ( e ) ;
@@ -305,16 +344,17 @@ void DrawingMiddleButtonDown(object sender, MouseButtonEventArgs e)
305
344
306
345
void DrawingLeftButtonDown ( object sender , MouseButtonEventArgs e )
307
346
{
347
+ // undo test
348
+ undoBufferBitmap [ ++ currentUndoIndex ] = canvasBitmap . Clone ( ) ;
349
+ Console . WriteLine ( "save undo " + currentUndoIndex ) ;
350
+
308
351
int x = ( int ) ( e . GetPosition ( drawingImage ) . X / canvasScaleX ) ;
309
352
int y = ( int ) ( e . GetPosition ( drawingImage ) . Y / canvasScaleX ) ;
310
353
DrawPixel ( x , y ) ;
311
354
}
312
355
313
356
void DrawingMouseUp ( object sender , MouseButtonEventArgs e )
314
357
{
315
- // undo test
316
- undoBufferBitmap [ ++ currentUndoIndex ] = canvasBitmap . Clone ( ) ;
317
- Console . WriteLine ( "save undo " + currentUndoIndex ) ;
318
358
}
319
359
320
360
0 commit comments