33using System . Collections . Generic ;
44using System . ComponentModel ;
55using System . IO ;
6+ using System . Linq ;
67using System . Runtime . CompilerServices ;
78using System . Windows ;
89using System . Windows . Controls ;
@@ -73,6 +74,9 @@ public partial class MainWindow : Window, INotifyPropertyChanged
7374 byte [ ] emptyPixels ;
7475 int emptyStride ;
7576
77+ // settings
78+ double wheelSpeed = 0.05 ;
79+
7680 private ToolMode _currentTool = ToolMode . Draw ;
7781 public ToolMode CurrentTool
7882 {
@@ -173,6 +177,7 @@ void Start()
173177 currentColorIndex = 5 ;
174178 currentColor = palette [ currentColorIndex ] ;
175179 SetRectangleFillColor ( rectCurrentColor , currentColor ) ;
180+ UpdateCurrentHue ( currentColor ) ;
176181 }
177182
178183
@@ -312,8 +317,74 @@ void PickPalette(MouseEventArgs e)
312317 if ( y < 0 || y > paletteResolutionY - 1 ) return ;
313318 currentColorIndex = y * paletteResolutionX + x + 1 ; // +1 for fix index magic number..
314319 currentColor = palette [ currentColorIndex ] ;
320+
321+ UpdateCurrentHue ( currentColor ) ;
315322 }
316323
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+ }
317388
318389 // return canvas pixel color from x,y
319390 unsafe PixelColor GetPixel ( int x , int y )
@@ -370,6 +441,7 @@ void DrawingMiddleButtonDown(object sender, MouseButtonEventArgs e)
370441
371442 currentColor = GetPixel ( x , y ) ;
372443 SetRectangleFillColor ( rectCurrentColor , currentColor ) ;
444+ UpdateCurrentHue ( currentColor ) ;
373445 }
374446 }
375447
@@ -454,6 +526,7 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
454526 else if ( e . MiddleButton == MouseButtonState . Pressed )
455527 {
456528 currentColor = GetPixel ( x , y ) ;
529+ UpdateCurrentHue ( currentColor ) ;
457530 }
458531
459532 ShowMousePos ( x , y ) ;
@@ -478,6 +551,7 @@ void ShowMousePixelColor(int x, int y)
478551 lblPixelColor . Content = col . Red + "," + col . Green + "," + col . Blue + "," + col . Alpha ;
479552 }
480553
554+ double hueLocation = 0.5 ;
481555 void DrawingMouseWheel ( object sender , MouseWheelEventArgs e )
482556 {
483557 /*
@@ -500,12 +574,19 @@ void DrawingMouseWheel(object sender, MouseWheelEventArgs e)
500574 }
501575 i.RenderTransform = new MatrixTransform(m);
502576 */
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 ) ;
509590 }
510591
511592 private void OnClearButton ( object sender , RoutedEventArgs e )
@@ -918,6 +999,7 @@ private void OnLevelSaturationMouseDown(object sender, MouseButtonEventArgs e)
918999 c2 . Blue = c1 . B ;
9191000 currentColor = c2 ;
9201001 rectCurrentColor . Fill = new SolidColorBrush ( Color . FromArgb ( c2 . Alpha , c2 . Red , c2 . Green , c2 . Blue ) ) ;
1002+ UpdateCurrentHue ( currentColor ) ;
9211003 }
9221004 } // class
9231005
0 commit comments