Skip to content

Commit

Permalink
Added the possibility to convert HSL colors (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed May 15, 2021
1 parent 74ce152 commit 629fbe0
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions ColorPicker/Classes/Global.cs
Expand Up @@ -120,6 +120,7 @@ public static string ColorTypesToString(ColorTypes colorTypes)
ColorTypes.HEX => Properties.Resources.HEX,
ColorTypes.RGB => Properties.Resources.RGB,
ColorTypes.HSV => Properties.Resources.HSV,
ColorTypes.HSL => Properties.Resources.HSL,
_ => Properties.Resources.RGB
}; // Return value
}
Expand Down
3 changes: 2 additions & 1 deletion ColorPicker/Enums/ColorTypes.cs
Expand Up @@ -36,6 +36,7 @@ public enum ColorTypes
{
RGB,
HEX,
HSV
HSV,
HSL
}
}
17 changes: 17 additions & 0 deletions ColorPicker/Pages/ConverterPage.xaml
Expand Up @@ -49,6 +49,22 @@
</StackPanel>
</StackPanel>
</Grid>
<Grid x:Name="HSLGrid" Visibility="Collapsed" Margin="10,0,0,0" VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.H}" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBox x:Name="HTxt" MinHeight="24" TextChanged="HTxt_TextChanged" Width="70" Style="{DynamicResource TextBoxStyle1}" BorderBrush="{Binding Source={StaticResource AccentColor}}" CaretBrush="{Binding Source={StaticResource Foreground1}}" BorderThickness="2" Margin="10,0,0,0" VerticalAlignment="Center" Background="{Binding Source={StaticResource Background1}}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.S}" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBox x:Name="STxt" MinHeight="24" TextChanged="STxt_TextChanged" Width="70" Style="{DynamicResource TextBoxStyle1}" BorderBrush="{Binding Source={StaticResource AccentColor}}" CaretBrush="{Binding Source={StaticResource Foreground1}}" BorderThickness="2" Margin="10,0,0,0" VerticalAlignment="Center" Background="{Binding Source={StaticResource Background1}}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.L}" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBox x:Name="LTxt" MinHeight="24" TextChanged="LTxt_TextChanged" Width="70" Style="{DynamicResource TextBoxStyle1}" BorderBrush="{Binding Source={StaticResource AccentColor}}" CaretBrush="{Binding Source={StaticResource Foreground1}}" BorderThickness="2" Margin="10,0,0,0" VerticalAlignment="Center" Background="{Binding Source={StaticResource Background1}}"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>
</StackPanel>

Expand All @@ -63,6 +79,7 @@
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.RGB}" x:Name="RGBTxt"/>
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.HEX}" x:Name="HEXTxt"/>
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.HSV}" x:Name="HSVTxt"/>
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.HSL}" x:Name="HSLTxt"/>
</StackPanel>
</StackPanel>

Expand Down
62 changes: 61 additions & 1 deletion ColorPicker/Pages/ConverterPage.xaml.cs
Expand Up @@ -59,6 +59,7 @@ private void InitUI()
ColorTypeComboBox.Items.Add(Global.ColorTypesToString(ColorTypes.RGB)); // Add
ColorTypeComboBox.Items.Add(Global.ColorTypesToString(ColorTypes.HEX)); // Add
ColorTypeComboBox.Items.Add(Global.ColorTypesToString(ColorTypes.HSV)); // Add
ColorTypeComboBox.Items.Add(Global.ColorTypesToString(ColorTypes.HSL)); // Add
ColorTypeComboBox.SelectedIndex = 0; // Set index

// Generate random color
Expand All @@ -79,10 +80,12 @@ private void ColorTxt_TextChanged(object sender, TextChangedEventArgs e)
{
string[] rgb = ColorTxt.Text.Split(new string[] { ";" }, StringSplitOptions.None); // Split
var hsv = ColorsConverter.RGBtoHSV(int.Parse(rgb[0]), int.Parse(rgb[1]), int.Parse(rgb[2])); // Convert
var hsl = ColorHelper.ColorConverter.RgbToHsl(new((byte)int.Parse(rgb[0]), (byte)int.Parse(rgb[1]), (byte)int.Parse(rgb[2])));

RGBTxt.Text = $"{Properties.Resources.RGB} {rgb[0]};{rgb[1]};{rgb[2]}"; // Set text
HEXTxt.Text = $"{Properties.Resources.HEX} #{ColorsConverter.RGBtoHEX(int.Parse(rgb[0]), int.Parse(rgb[1]), int.Parse(rgb[2])).Value}"; // Set text
HSVTxt.Text = $"{Properties.Resources.HSV} ({hsv.Hue},{hsv.Saturation},{hsv.Value})"; // Set text
HSLTxt.Text = $"{Properties.Resources.HSL} ({hsl.H},{hsl.S},{hsl.L})"; // Set text

rgbColor = $"{rgb[0]};{rgb[1]};{rgb[2]}"; // Set text
hexColor = $"#{ColorsConverter.RGBtoHEX(int.Parse(rgb[0]), int.Parse(rgb[1]), int.Parse(rgb[2])).Value}"; // Set text
Expand All @@ -93,10 +96,12 @@ private void ColorTxt_TextChanged(object sender, TextChangedEventArgs e)
var rgb = ColorsConverter.HEXtoRGB(new() { Value = ColorTxt.Text }); // Convert
string hex = ColorTxt.Text.StartsWith("#") ? ColorTxt.Text : "#" + ColorTxt.Text; // Set
var hsv = ColorsConverter.RGBtoHSV(rgb); // Convert
var hsl = ColorHelper.ColorConverter.HexToHsl(new(hex)); // Convert

RGBTxt.Text = $"{Properties.Resources.RGB} {rgb.R};{rgb.G};{rgb.B}"; // Set text
HEXTxt.Text = $"{Properties.Resources.HEX} {hex}"; // Set text
HSVTxt.Text = $"{Properties.Resources.HSV} ({hsv.Hue},{hsv.Saturation},{hsv.Value})"; // Set text
HSLTxt.Text = $"{Properties.Resources.HSL} ({hsl.H},{hsl.S},{hsl.L})"; // Set text

rgbColor = $"{rgb.R};{rgb.G};{rgb.B}"; // Set text
hexColor = $"{hex}"; // Set text
Expand Down Expand Up @@ -147,10 +152,12 @@ private void ConvertHSV()
int v = int.Parse(ValTxt.Text); // Parse
var rgb = ColorHelper.ColorConverter.HsvToRgb(new(h, (byte)s, (byte)v)); // Convert
var hex = ColorsConverter.RGBtoHEX(rgb.R, rgb.G, rgb.B); // Convert
var hsl = ColorHelper.ColorConverter.RgbToHsl(rgb); // Convert

RGBTxt.Text = $"{Properties.Resources.RGB} {rgb.R};{rgb.G};{rgb.B}"; // Set text
HEXTxt.Text = $"{Properties.Resources.HEX} #{hex.Value}"; // Set text
HSVTxt.Text = $"{Properties.Resources.HSV} ({h},{s},{v})"; // Set text
HSLTxt.Text = $"{Properties.Resources.HSL} ({hsl.H},{hsl.S},{hsl.L})"; // Set text

hsvColor = $"({h},{s},{v})"; // Set

Expand All @@ -168,11 +175,56 @@ private void ConvertHSV()
}
}

private void ConvertHSL()
{
try
{
int h = int.Parse(HTxt.Text); // Parse
int s = int.Parse(STxt.Text); // Parse
int l = int.Parse(LTxt.Text); // Parse
var rgb = ColorHelper.ColorConverter.HslToRgb(new(h, (byte)s, (byte)l)); // Convert
var hex = ColorsConverter.RGBtoHEX(rgb.R, rgb.G, rgb.B); // Convert
var hsv = ColorHelper.ColorConverter.RgbToHsv(rgb); // Convert

RGBTxt.Text = $"{Properties.Resources.RGB} {rgb.R};{rgb.G};{rgb.B}"; // Set text
HEXTxt.Text = $"{Properties.Resources.HEX} #{hex.Value}"; // Set text
HSVTxt.Text = $"{Properties.Resources.HSV} ({hsv.H},{hsv.S},{hsv.V})"; // Set text
HSLTxt.Text = $"{Properties.Resources.HSL} ({h},{s},{l})"; // Set text

ColorDisplayer.Background = new SolidColorBrush { Color = Color.FromRgb(rgb.R, rgb.G, rgb.B) }; // Set color

IconValidMsgTxt.Foreground = new SolidColorBrush { Color = Color.FromRgb(0, 223, 57) }; // Set foreground color
IconValidMsgTxt.Text = "\uF299"; // Set icon
ValidMsgTxt.Text = Properties.Resources.ColorValid; // Set text
}
catch
{
IconValidMsgTxt.Foreground = new SolidColorBrush { Color = Color.FromRgb(255, 69, 0) }; // Set foreground color
IconValidMsgTxt.Text = "\uF36E"; // Set icon
ValidMsgTxt.Text = Properties.Resources.InvalidColor; // Set text
}
}

private void CopyHSVBtn_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(hsvColor); // Copy
}

private void HTxt_TextChanged(object sender, TextChangedEventArgs e)
{
ConvertHSL();
}

private void STxt_TextChanged(object sender, TextChangedEventArgs e)
{
ConvertHSL();
}

private void LTxt_TextChanged(object sender, TextChangedEventArgs e)
{
ConvertHSL();
}

private void CopyHEXBtn_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(hexColor); // Copy
Expand All @@ -197,14 +249,22 @@ private void ColorTypeComboBox_SelectionChanged(object sender, SelectionChangedE
case 0: // RGB
ColorTxt.Visibility = Visibility.Visible; // Show
HSVGrid.Visibility = Visibility.Collapsed; // Hide
HSLGrid.Visibility = Visibility.Collapsed; // Hide
break;
case 1: // HEX
ColorTxt.Visibility = Visibility.Visible; // Show
HSVGrid.Visibility = Visibility.Collapsed; // Hide
HSLGrid.Visibility = Visibility.Collapsed; // Hide
break;
case 2:
case 2: // HSV
ColorTxt.Visibility = Visibility.Collapsed; // Hide
HSVGrid.Visibility = Visibility.Visible; // Show
HSLGrid.Visibility = Visibility.Collapsed; // Hide
break;
case 3: // HSL
ColorTxt.Visibility = Visibility.Collapsed; // Hide
HSVGrid.Visibility = Visibility.Collapsed; // Hide
HSLGrid.Visibility = Visibility.Visible; // Show
break;
}
}
Expand Down
18 changes: 18 additions & 0 deletions ColorPicker/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ColorPicker/Properties/Resources.en-US.resx
Expand Up @@ -297,4 +297,10 @@
<data name="CopyHSV" xml:space="preserve">
<value>Copy HSV</value>
</data>
<data name="HSL" xml:space="preserve">
<value>HSL</value>
</data>
<data name="L" xml:space="preserve">
<value>L</value>
</data>
</root>
6 changes: 6 additions & 0 deletions ColorPicker/Properties/Resources.fr-FR.resx
Expand Up @@ -297,4 +297,10 @@
<data name="CopyHSV" xml:space="preserve">
<value>Copier HSV</value>
</data>
<data name="HSL" xml:space="preserve">
<value>HSL</value>
</data>
<data name="L" xml:space="preserve">
<value>L</value>
</data>
</root>
6 changes: 6 additions & 0 deletions ColorPicker/Properties/Resources.resx
Expand Up @@ -277,4 +277,10 @@
<data name="CopyHSV" xml:space="preserve">
<value>Copy HSV</value>
</data>
<data name="HSL" xml:space="preserve">
<value>HSL</value>
</data>
<data name="L" xml:space="preserve">
<value>L</value>
</data>
</root>

0 comments on commit 629fbe0

Please sign in to comment.