Skip to content

Commit

Permalink
Added the possibility to export all colors to a CSV File (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Feb 21, 2024
1 parent 95e66da commit 8abd273
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 4 deletions.
87 changes: 87 additions & 0 deletions ColorPicker/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,93 @@
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="RadioButton.Static.Background" Color="#FFFFFFFF" />
<SolidColorBrush x:Key="RadioButton.Static.Border" Color="#FF707070" />
<SolidColorBrush x:Key="RadioButton.Static.Glyph" Color="#FF212121" />
<SolidColorBrush x:Key="RadioButton.MouseOver.Background" Color="Transparent" />
<SolidColorBrush x:Key="RadioButton.MouseOver.Border" Color="{DynamicResource Accent}" />
<SolidColorBrush x:Key="RadioButton.MouseOver.Glyph" Color="{DynamicResource Accent}" />
<SolidColorBrush x:Key="RadioButton.Pressed.Background" Color="Transparent" />
<SolidColorBrush x:Key="RadioButton.Pressed.Border" Color="{DynamicResource Accent}" />
<SolidColorBrush x:Key="RadioButton.Pressed.Glyph" Color="{DynamicResource Accent}" />
<SolidColorBrush x:Key="RadioButton.Disabled.Background" Color="#FFE6E6E6" />
<SolidColorBrush x:Key="RadioButton.Disabled.Border" Color="#FFBCBCBC" />
<SolidColorBrush x:Key="RadioButton.Disabled.Glyph" Color="#FF707070" />
<Style x:Key="RadioButtonStyle1" TargetType="{x:Type RadioButton}">
<Setter Property="Background" Value="{StaticResource RadioButton.Static.Background}" />
<Setter Property="BorderBrush" Value="{StaticResource RadioButton.Static.Border}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid
x:Name="templateRoot"
Background="Transparent"
SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border
x:Name="radioButtonBorder"
Margin="1 1 2 1"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="100">
<Grid x:Name="markGrid" Margin="2">
<Ellipse
x:Name="optionMark"
MinWidth="6"
MinHeight="6"
Fill="{DynamicResource AccentColor}"
Opacity="0" />
</Grid>
</Border>
<ContentPresenter
x:Name="contentPresenter"
Grid.Column="1"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Focusable="False"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="true">
<Setter Property="Padding" Value="4 -1 0 0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="radioButtonBorder" Property="Background" Value="{StaticResource RadioButton.MouseOver.Background}" />
<Setter TargetName="radioButtonBorder" Property="BorderBrush" Value="{StaticResource RadioButton.MouseOver.Border}" />
<Setter TargetName="optionMark" Property="Fill" Value="{StaticResource RadioButton.MouseOver.Glyph}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="radioButtonBorder" Property="Background" Value="{StaticResource RadioButton.Disabled.Background}" />
<Setter TargetName="radioButtonBorder" Property="BorderBrush" Value="{StaticResource RadioButton.Disabled.Border}" />
<Setter TargetName="optionMark" Property="Fill" Value="{StaticResource RadioButton.Disabled.Glyph}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="radioButtonBorder" Property="Background" Value="{StaticResource RadioButton.Pressed.Background}" />
<Setter TargetName="radioButtonBorder" Property="BorderBrush" Value="{StaticResource RadioButton.Pressed.Border}" />
<Setter TargetName="optionMark" Property="Fill" Value="{StaticResource RadioButton.Pressed.Glyph}" />
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="optionMark" Property="Opacity" Value="1" />
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="optionMark" Property="Opacity" Value="0.56" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style x:Key="ScrollBarPageButton" TargetType="{x:Type RepeatButton}">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Background" Value="Transparent" />
Expand Down
91 changes: 88 additions & 3 deletions ColorPicker/Pages/ImageExtractorPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
Foreground="{DynamicResource Foreground1}"
mc:Ignorable="d">


<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Margin="10" Orientation="Horizontal">
Expand Down Expand Up @@ -109,8 +111,9 @@
FontWeight="Bold"
Foreground="{DynamicResource WindowButtonsHoverForeground1}"
Style="{DynamicResource PrimaryButton}" />

<Border
Grid.Row="4"
Grid.Row="5"
Margin="0 5 0 0"
Padding="5"
Background="{DynamicResource Background1}"
Expand All @@ -123,17 +126,34 @@
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>

<StackPanel
Grid.Row="0"
Grid.ColumnSpan="3"
Orientation="Horizontal">
<Button
x:Name="ExportBtn"
Padding="5 2"
Background="{DynamicResource LightAccentColor}"
BorderThickness="0"
Click="ExportBtn_Click"
Content="{x:Static lang:Resources.ExportToCSV}"
FontWeight="Bold"
Foreground="{DynamicResource AccentColor}"
Style="{DynamicResource PrimaryButton}" />
</StackPanel>
<TextBlock
Grid.Row="1"
Grid.Column="1"
Margin="15 5"
VerticalAlignment="Center"
FontWeight="Bold"
Text="{x:Static lang:Resources.Color}" />
<TextBlock
Grid.Row="1"
Grid.Column="2"
Margin="5"
VerticalAlignment="Center"
Expand All @@ -142,7 +162,7 @@

<StackPanel
x:Name="ColorDisplayer"
Grid.Row="1"
Grid.Row="2"
Grid.ColumnSpan="3" />
</Grid>
</Border>
Expand Down Expand Up @@ -200,5 +220,70 @@
</StackPanel>
</Border>
</Popup>
<Popup
x:Name="ExportCSVPopup"
AllowsTransparency="True"
Placement="Bottom"
PlacementTarget="{Binding ElementName=ExportBtn}"
PopupAnimation="Fade"
StaysOpen="False">
<Border
Margin="10"
Padding="10"
Background="{DynamicResource Background1}"
CornerRadius="5">
<Border.Effect>
<DropShadowEffect
BlurRadius="10"
Opacity="0.4"
RenderingBias="Performance"
ShadowDepth="0"
Color="Black" />
</Border.Effect>

<StackPanel>
<TextBlock
Margin="5 0"
FontWeight="Bold"
Text="{x:Static lang:Resources.SelectFormatItems}" />
<RadioButton
x:Name="CommaRadioBtn"
Margin="10 0"
BorderBrush="{DynamicResource AccentColor}"
Content="{x:Static lang:Resources.CommaSeparated}"
GroupName="Format"
IsChecked="True"
Style="{DynamicResource RadioButtonStyle1}" />
<RadioButton
x:Name="SemiColonRadioBtn"
Margin="10 0"
BorderBrush="{DynamicResource AccentColor}"
Content="{x:Static lang:Resources.SemiColonSeparated}"
GroupName="Format"
Style="{DynamicResource RadioButtonStyle1}" />
<CheckBox
x:Name="IncludeFrequenceChk"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
BorderThickness="2"
Content="{x:Static lang:Resources.IncludeFrequence}"
FontWeight="Bold"
Style="{DynamicResource CheckBoxStyle1}" />
<Button
x:Name="ExportCSVBtn"
Grid.Row="3"
Margin="5"
Padding="5 2"
HorizontalAlignment="Center"
Background="{DynamicResource AccentColor}"
BorderThickness="0"
Click="ExportCSVBtn_Click"
Content="{x:Static lang:Resources.ExportShort}"
FontWeight="Bold"
Foreground="{DynamicResource WindowButtonsHoverForeground1}"
Style="{DynamicResource PrimaryButton}" />
</StackPanel>
</Border>
</Popup>
</Grid>
</Page>
39 changes: 39 additions & 0 deletions ColorPicker/Pages/ImageExtractorPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/

using ColorPicker.Classes;
using ColorPicker.Enums;
using ColorPicker.UserControls;
using Microsoft.Win32;
using Synethia;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
Expand All @@ -38,10 +40,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Windows.Forms.LinkLabel;

namespace ColorPicker.Pages;
/// <summary>
Expand All @@ -51,6 +55,7 @@ public partial class ImageExtractorPage : Page
{
bool code = Global.Settings.UseSynethia ? false : true; // checks if the code as already been implemented
readonly List<string> filePaths = new();
private Dictionary<RGB, int> Colors = new();
public ImageExtractorPage()
{
InitializeComponent();
Expand Down Expand Up @@ -94,6 +99,7 @@ private async void ExtractBtn_Click(object sender, RoutedEventArgs e)
bool precisionValid = int.TryParse(PrecisionTxt.Text, out var precision);

var colors = await GetImageColorFrequenciesAsync(filePaths[0], precisionValid ? precision : 10);
Colors = colors;

int c = 0;
foreach (var color in colors)
Expand Down Expand Up @@ -169,4 +175,37 @@ private void ClearBtn_Click(object sender, RoutedEventArgs e)
LoadImageUI();
ColorDisplayer.Children.Clear();
}

private void ExportBtn_Click(object sender, RoutedEventArgs e)
{
ExportCSVPopup.IsOpen = true;
}

private void ExportCSVBtn_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new()
{
Filter = "CSV Files|*.csv|All Files|*.*"
};
if (saveFileDialog.ShowDialog() == true)
{
ExportToCSV(Colors, saveFileDialog.FileName, (CommaRadioBtn.IsChecked ?? true) ? "," : ";", IncludeFrequenceChk.IsChecked ?? false);
}
}

private void ExportToCSV(Dictionary<RGB, int> colors, string fileName, string separator, bool includeFreq)
{
string text = "";
foreach (var color in colors)
{
var hex = ColorHelper.ColorConverter.RgbToHex(new(color.Key.R, color.Key.G, color.Key.B));
text += $"#{hex}{separator}{(includeFreq ? color.Value.ToString() + separator : "")}\n";
}
try
{
using StreamWriter writer = new(fileName, false, Encoding.UTF8);
writer.WriteLine(text);
}
catch { }
}
}
Loading

0 comments on commit 8abd273

Please sign in to comment.