3
3
using System . Collections . Generic ;
4
4
using System . ComponentModel ;
5
5
using System . IO ;
6
+ using System . Linq ;
6
7
using System . Runtime . CompilerServices ;
7
8
using System . Windows ;
8
9
using System . Windows . Controls ;
@@ -73,6 +74,9 @@ public partial class MainWindow : Window, INotifyPropertyChanged
73
74
byte [ ] emptyPixels ;
74
75
int emptyStride ;
75
76
77
+ // settings
78
+ double wheelSpeed = 0.05 ;
79
+
76
80
private ToolMode _currentTool = ToolMode . Draw ;
77
81
public ToolMode CurrentTool
78
82
{
@@ -173,6 +177,7 @@ void Start()
173
177
currentColorIndex = 5 ;
174
178
currentColor = palette [ currentColorIndex ] ;
175
179
SetRectangleFillColor ( rectCurrentColor , currentColor ) ;
180
+ UpdateCurrentHue ( currentColor ) ;
176
181
}
177
182
178
183
@@ -312,8 +317,74 @@ void PickPalette(MouseEventArgs e)
312
317
if ( y < 0 || y > paletteResolutionY - 1 ) return ;
313
318
currentColorIndex = y * paletteResolutionX + x + 1 ; // +1 for fix index magic number..
314
319
currentColor = palette [ currentColorIndex ] ;
320
+
321
+ UpdateCurrentHue ( currentColor ) ;
315
322
}
316
323
324
+ LinearGradientBrush myBrush ;
325
+ void UpdateCurrentHue ( PixelColor c )
326
+ {
327
+ hueLocation = 0.5 ;
328
+
329
+ myBrush = new LinearGradientBrush ( ) ;
330
+ var c1 = new Color ( ) ;
331
+ c1 . R = 0 ;
332
+ c1 . G = 0 ;
333
+ c1 . B = 0 ;
334
+ c1 . A = 255 ;
335
+ var c2 = new Color ( ) ;
336
+ c2 . R = c . Red ;
337
+ c2 . G = c . Green ;
338
+ c2 . B = c . Blue ;
339
+ c2 . A = 255 ;
340
+ var c3 = new Color ( ) ;
341
+ c3 . R = 255 ;
342
+ c3 . G = 255 ;
343
+ c3 . B = 255 ;
344
+ c3 . A = 255 ;
345
+
346
+ myBrush . StartPoint = new Point ( 0 , 0 ) ;
347
+ myBrush . EndPoint = new Point ( 1 , 0 ) ;
348
+
349
+ var g1 = new GradientStop ( c1 , 0.0 ) ;
350
+ myBrush . GradientStops . Add ( g1 ) ;
351
+
352
+ var g2 = new GradientStop ( c2 , 0.5 ) ;
353
+ myBrush . GradientStops . Add ( g2 ) ;
354
+
355
+ var g3 = new GradientStop ( c3 , 1 ) ;
356
+ myBrush . GradientStops . Add ( g3 ) ;
357
+
358
+ rectCurrentHue . Fill = myBrush ;
359
+
360
+ //myBrush.GradientStops
361
+
362
+ }
363
+
364
+ // https://stackoverflow.com/a/39450207/5452781
365
+ private static Color GetColorByOffset ( GradientStopCollection collection , double offset )
366
+ {
367
+ GradientStop [ ] stops = collection . OrderBy ( x => x . Offset ) . ToArray ( ) ;
368
+ if ( offset <= 0 ) return stops [ 0 ] . Color ;
369
+ if ( offset >= 1 ) return stops [ stops . Length - 1 ] . Color ;
370
+ GradientStop left = stops [ 0 ] , right = null ;
371
+ foreach ( GradientStop stop in stops )
372
+ {
373
+ if ( stop . Offset >= offset )
374
+ {
375
+ right = stop ;
376
+ break ;
377
+ }
378
+ left = stop ;
379
+ }
380
+ //Debug.Assert(right != null);
381
+ offset = Math . Round ( ( offset - left . Offset ) / ( right . Offset - left . Offset ) , 2 ) ;
382
+ byte a = ( byte ) ( ( right . Color . A - left . Color . A ) * offset + left . Color . A ) ;
383
+ byte r = ( byte ) ( ( right . Color . R - left . Color . R ) * offset + left . Color . R ) ;
384
+ byte g = ( byte ) ( ( right . Color . G - left . Color . G ) * offset + left . Color . G ) ;
385
+ byte b = ( byte ) ( ( right . Color . B - left . Color . B ) * offset + left . Color . B ) ;
386
+ return Color . FromArgb ( a , r , g , b ) ;
387
+ }
317
388
318
389
// return canvas pixel color from x,y
319
390
unsafe PixelColor GetPixel ( int x , int y )
@@ -370,6 +441,7 @@ void DrawingMiddleButtonDown(object sender, MouseButtonEventArgs e)
370
441
371
442
currentColor = GetPixel ( x , y ) ;
372
443
SetRectangleFillColor ( rectCurrentColor , currentColor ) ;
444
+ UpdateCurrentHue ( currentColor ) ;
373
445
}
374
446
}
375
447
@@ -454,6 +526,7 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
454
526
else if ( e . MiddleButton == MouseButtonState . Pressed )
455
527
{
456
528
currentColor = GetPixel ( x , y ) ;
529
+ UpdateCurrentHue ( currentColor ) ;
457
530
}
458
531
459
532
ShowMousePos ( x , y ) ;
@@ -478,6 +551,7 @@ void ShowMousePixelColor(int x, int y)
478
551
lblPixelColor . Content = col . Red + "," + col . Green + "," + col . Blue + "," + col . Alpha ;
479
552
}
480
553
554
+ double hueLocation = 0.5 ;
481
555
void DrawingMouseWheel ( object sender , MouseWheelEventArgs e )
482
556
{
483
557
/*
@@ -500,12 +574,19 @@ void DrawingMouseWheel(object sender, MouseWheelEventArgs e)
500
574
}
501
575
i.RenderTransform = new MatrixTransform(m);
502
576
*/
503
- //Console.WriteLine(e.Delta);
504
- int amount = e . Delta < 0 ? - 1 : 1 ;
505
- //var c = ColorToHSV(currentColor);
506
- //ColorToHSV(currentColor);
507
- currentColor = AdjustColorLightness ( currentColor , amount ) ;
508
- //currentColor =
577
+
578
+ hueLocation += e . Delta < 0 ? - wheelSpeed : wheelSpeed ;
579
+ if ( hueLocation < 0 ) hueLocation = 0 ;
580
+ if ( hueLocation > 1 ) hueLocation = 1 ;
581
+
582
+ var c = GetColorByOffset ( myBrush . GradientStops , hueLocation ) ;
583
+ var cc = new PixelColor ( ) ;
584
+ cc . Red = c . R ;
585
+ cc . Green = c . G ;
586
+ cc . Blue = c . B ;
587
+ cc . Alpha = 255 ;
588
+ currentColor = cc ;
589
+ SetRectangleFillColor ( rectCurrentColor , currentColor ) ;
509
590
}
510
591
511
592
private void OnClearButton ( object sender , RoutedEventArgs e )
@@ -918,6 +999,7 @@ private void OnLevelSaturationMouseDown(object sender, MouseButtonEventArgs e)
918
999
c2 . Blue = c1 . B ;
919
1000
currentColor = c2 ;
920
1001
rectCurrentColor . Fill = new SolidColorBrush ( Color . FromArgb ( c2 . Alpha , c2 . Red , c2 . Green , c2 . Blue ) ) ;
1002
+ UpdateCurrentHue ( currentColor ) ;
921
1003
}
922
1004
} // class
923
1005
0 commit comments