1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Gradient editor - Added stops creation

Core - ColorGradient can now return a color at a certain position
This commit is contained in:
Robert 2020-03-11 20:11:42 +01:00
parent f95aaf2443
commit 0ccc1fed3d
5 changed files with 75 additions and 40 deletions

View File

@ -44,6 +44,31 @@ namespace Artemis.Core.Models.Profile
{
OnPropertyChanged(nameof(Colors));
}
public SKColor GetColor(float position)
{
var point = Colors.SingleOrDefault(f => f.Position == position);
if (point != null) return point.Color;
var before = Colors.First(w => w.Position == Colors.Min(m => m.Position));
var after = Colors.First(w => w.Position == Colors.Max(m => m.Position));
foreach (var gs in Colors)
{
if (gs.Position < position && gs.Position > before.Position)
before = gs;
if (gs.Position >= position && gs.Position < after.Position)
after = gs;
}
return new SKColor(
(byte) ((position - before.Position) * (after.Color.Red - before.Color.Red) / (after.Position - before.Position) + before.Color.Red),
(byte) ((position - before.Position) * (after.Color.Green - before.Color.Green) / (after.Position - before.Position) + before.Color.Green),
(byte) ((position - before.Position) * (after.Color.Blue - before.Color.Blue) / (after.Position - before.Position) + before.Color.Blue),
(byte) ((position - before.Position) * (after.Color.Alpha - before.Color.Alpha) / (after.Position - before.Position) + before.Color.Alpha)
);
}
}
public class ColorGradientColor : INotifyPropertyChanged

View File

@ -19,12 +19,14 @@ namespace Artemis.UI.Shared.Screens.Dialogs
public void Confirm()
{
Session.Close(true);
if (!Session.IsEnded)
Session.Close(true);
}
public void Cancel()
{
Session.Close(false);
if (!Session.IsEnded)
Session.Close(false);
}
}
}

View File

@ -41,43 +41,41 @@
<TextBlock Margin="15 15 0 0">Gradient</TextBlock>
<Separator Margin="15 5" />
<ItemsControl ItemsSource="{Binding ColorGradient.Colors}" ClipToBounds="False" Margin="15 0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Height="60" Width="437" x:Name="PreviewCanvas">
<Canvas.Background>
<VisualBrush Stretch="None" AlignmentY="Top">
<VisualBrush.Visual>
<materialDesign:Card materialDesign:ShadowAssist.ShadowDepth="Depth1" Margin="15">
<Rectangle x:Name="Preview"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Width="{Binding ActualWidth, ElementName=PreviewCanvas}"
Height="35">
<Rectangle.Fill>
<LinearGradientBrush GradientStops="{Binding ColorGradient.Colors, Converter={StaticResource ColorGradientToGradientStopsConverter}}" />
</Rectangle.Fill>
</Rectangle>
</materialDesign:Card>
</VisualBrush.Visual>
</VisualBrush>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Top" Value="40" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:GradientEditorColor
ColorGradient="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:MaterialWindow}, Path=ColorGradient}"
GradientColor="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Grid>
<ItemsControl ItemsSource="{Binding ColorGradient.Colors}" ClipToBounds="False" Margin="15 0" MouseLeftButtonUp="Rectangle_MouseLeftButtonUp" Cursor="Cross">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Height="60" Width="437" x:Name="PreviewCanvas">
<Canvas.Background>
<VisualBrush Stretch="None" AlignmentY="Top">
<VisualBrush.Visual>
<materialDesign:Card materialDesign:ShadowAssist.ShadowDepth="Depth1" Margin="15">
<Rectangle x:Name="Preview" Width="{Binding ActualWidth, ElementName=PreviewCanvas}">
<Rectangle.Fill>
<LinearGradientBrush GradientStops="{Binding ColorGradient.Colors, Converter={StaticResource ColorGradientToGradientStopsConverter}}" />
</Rectangle.Fill>
</Rectangle>
</materialDesign:Card>
</VisualBrush.Visual>
</VisualBrush>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Top" Value="40" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:GradientEditorColor
ColorGradient="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:MaterialWindow}, Path=ColorGradient}"
GradientColor="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
<TextBlock Margin="15 15 0 0">Stops</TextBlock>
<Separator Margin="15 5" />

View File

@ -32,7 +32,7 @@ namespace Artemis.UI.Shared.Screens.GradientEditor
InitializeComponent();
ColorGradient = colorGradient;
}
public ColorGradient ColorGradient
{
get => (ColorGradient) GetValue(ColorGradientProperty);
@ -57,5 +57,14 @@ namespace Artemis.UI.Shared.Screens.GradientEditor
editor.OnPropertyChanged(nameof(ColorGradient));
editor._inCallback = false;
}
private void Rectangle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var position = e.GetPosition((IInputElement) sender);
// Position ranges from 0.0 to 1.0
var newPosition = (float) Math.Min(1, Math.Max(0, Math.Round((position.X) / 435.0, 3, MidpointRounding.AwayFromZero)));
ColorGradient.Colors.Add(new ColorGradientColor(ColorGradient.GetColor(newPosition), newPosition));
}
}
}

View File

@ -99,6 +99,7 @@ namespace Artemis.UI.Shared.Screens.GradientEditor
}
((IInputElement) sender).ReleaseMouseCapture();
e.Handled = true;
}
private void ColorStop_MouseMove(object sender, MouseEventArgs e)