Skip to content

Commit 28367a0

Browse files
committed
add current color brightness bar below canvas (use mousescroll to adjust current brightness)
1 parent d61c228 commit 28367a0

File tree

3 files changed

+281
-14
lines changed

3 files changed

+281
-14
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@
197197
<Button x:Name="btnScrollDown" Click="OnScrollButtonDownClicked" Content="\/" HorizontalAlignment="Left" Margin="388,274,0,0" VerticalAlignment="Top" Width="24"/>
198198
<Button x:Name="btnScrollLeft" Click="OnScrollButtonLeftClicked" Content="&lt;" HorizontalAlignment="Left" Margin="361,263,0,0" VerticalAlignment="Top" Width="24"/>
199199
<Button x:Name="btnScrollRight" Click="OnScrollButtonRightClicked" Content="&gt;" HorizontalAlignment="Left" Margin="415,262,0,0" VerticalAlignment="Top" Width="24"/>
200-
<Button x:Name="btnFlipX" Click="OnFlipXButtonDown" Content="FlipX" HorizontalAlignment="Left" Margin="353,316,0,0" VerticalAlignment="Top" Width="32"/>
201-
<Button x:Name="btnFlipY" Click="OnFlipYButtonDown" Content="FlipY" HorizontalAlignment="Left" Margin="390,316,0,0" VerticalAlignment="Top" Width="32"/>
200+
<Button x:Name="btnFlipX" Click="OnFlipXButtonDown" Content="FlipX" HorizontalAlignment="Left" Margin="365,316,0,0" VerticalAlignment="Top" Width="32"/>
201+
<Button x:Name="btnFlipY" Click="OnFlipYButtonDown" Content="FlipY" HorizontalAlignment="Left" Margin="402,316,0,0" VerticalAlignment="Top" Width="32"/>
202202
<Button x:Name="btnLoadPalette" Click="OnLoadPaletteButton" Content="Load Palette" HorizontalAlignment="Left" Margin="10,321,0,0" VerticalAlignment="Top" Width="71"/>
203203
</Grid>
204204
<CheckBox x:Name="chkOutline" Content="Outline" HorizontalAlignment="Left" Margin="638,50,0,0" VerticalAlignment="Top" Width="64" Click="chkOutline_Click"/>
@@ -210,6 +210,14 @@
210210
<Rectangle x:Name="tempRect" Width="200" Height="200" Margin="459,130,0,0" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" UseLayoutRounding="False" Fill="Black" />
211211
<Rectangle x:Name="rectSaturation" Fill="{StaticResource LevelSaturationBrush}" Width="200" Height="200" Margin="459,130,0,0" StrokeThickness="1" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" MouseDown="OnLevelSaturationMouseDown" />
212212
<Rectangle x:Name="rectHueBar" Fill="{StaticResource HueBrush}" Width="20" Height="200" Margin="664,130,0,0" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" MouseDown="rectHueBar_MouseDown" HorizontalAlignment="Left" VerticalAlignment="Top" />
213+
<Rectangle x:Name="rectCurrentHue" Width="253" Height="14" Margin="91,316,0,0" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" >
214+
<Rectangle.Fill>
215+
<LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
216+
<GradientStop Color="Black" Offset="0"/>
217+
<GradientStop Color="White" Offset="1"/>
218+
</LinearGradientBrush>
219+
</Rectangle.Fill>
220+
</Rectangle>
213221

214222
</Grid>
215223
</Window>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.ComponentModel;
55
using System.IO;
6+
using System.Linq;
67
using System.Runtime.CompilerServices;
78
using System.Windows;
89
using 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

Comments
 (0)