Skip to content

Commit

Permalink
Added the possibility to convert CMYK colors (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed May 15, 2021
1 parent 469365f commit 0ffda74
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 2 deletions.
1 change: 1 addition & 0 deletions ColorPicker/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static string ColorTypesToString(ColorTypes colorTypes)
ColorTypes.RGB => Properties.Resources.RGB,
ColorTypes.HSV => Properties.Resources.HSV,
ColorTypes.HSL => Properties.Resources.HSL,
ColorTypes.CMYK => Properties.Resources.CMYK,
_ => Properties.Resources.RGB
}; // Return value
}
Expand Down
3 changes: 2 additions & 1 deletion ColorPicker/Enums/ColorTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum ColorTypes
RGB,
HEX,
HSV,
HSL
HSL,
CMYK
}
}
21 changes: 21 additions & 0 deletions ColorPicker/Pages/ConverterPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@
</StackPanel>
</StackPanel>
</Grid>
<Grid x:Name="CMYKGrid" 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.C}" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBox x:Name="CTxt" MinHeight="24" TextChanged="CTxt_TextChanged" Width="50" 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}}" Foreground="{Binding Source={StaticResource Foreground1}}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.M}" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBox x:Name="MTxt" MinHeight="24" TextChanged="CTxt_TextChanged" Width="50" 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}}" Foreground="{Binding Source={StaticResource Foreground1}}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.Y}" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBox x:Name="YTxt" MinHeight="24" TextChanged="CTxt_TextChanged" Width="50" 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}}" Foreground="{Binding Source={StaticResource Foreground1}}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.K}" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBox x:Name="KTxt" MinHeight="24" TextChanged="CTxt_TextChanged" Width="50" 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}}" Foreground="{Binding Source={StaticResource Foreground1}}"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>
</StackPanel>

Expand All @@ -80,6 +100,7 @@
<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"/>
<TextBlock FontWeight="Bold" FontSize="14" Text="{x:Static lang:Resources.CMYK}" x:Name="CMYKTxt"/>
</StackPanel>
</StackPanel>

Expand Down
78 changes: 77 additions & 1 deletion ColorPicker/Pages/ConverterPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace ColorPicker.Pages
/// </summary>
public partial class ConverterPage : Page
{
string rgbColor, hexColor, hsvColor, hslColor = "";
string rgbColor, hexColor, hsvColor, hslColor, cmykColor = "";
public ConverterPage()
{
InitializeComponent();
Expand All @@ -60,6 +60,7 @@ private void InitUI()
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.Items.Add(Global.ColorTypesToString(ColorTypes.CMYK)); // Add
ColorTypeComboBox.SelectedIndex = 0; // Set index

// Generate random color
Expand All @@ -81,33 +82,39 @@ 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])));
var cmyk = ColorHelper.ColorConverter.RgbToCmyk(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
CMYKTxt.Text = $"{Properties.Resources.CMYK} {cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}";

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
hsvColor = $"({hsv.Hue},{hsv.Saturation},{hsv.Value})"; // Set
hslColor = $"({hsl.H},{hsl.S},{hsl.L})"; // Set
cmykColor = $"{cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}"; // Set
}
else if (ColorTypeComboBox.Text == Properties.Resources.HEX)
{
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
var cmyk = ColorHelper.ColorConverter.HexToCmyk(new(hex));

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
CMYKTxt.Text = $"{Properties.Resources.CMYK} {cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}";

rgbColor = $"{rgb.R};{rgb.G};{rgb.B}"; // Set text
hexColor = $"{hex}"; // Set text
hsvColor = $"({hsv.Hue},{hsv.Saturation},{hsv.Value})"; // Set
hslColor = $"({hsl.H},{hsl.S},{hsl.L})"; // Set
cmykColor = $"{cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}"; // Set
}

string[] rC = rgbColor.Split(new string[] { ";" }, StringSplitOptions.None); // Split
Expand Down Expand Up @@ -156,16 +163,19 @@ private void ConvertHSV()
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
var cmyk = ColorHelper.ColorConverter.HsvToCmyk(new(h, (byte)s, (byte)v));

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
CMYKTxt.Text = $"{Properties.Resources.CMYK} {cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}";

rgbColor = $"{rgb.R};{rgb.G};{rgb.B}"; // Set text
hexColor = $"{hex}"; // Set text
hsvColor = $"({h},{s},{v})"; // Set
hslColor = $"({hsl.H},{hsl.S},{hsl.L})"; // Set
cmykColor = $"{cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}"; // Set

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

Expand All @@ -191,16 +201,58 @@ private void ConvertHSL()
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
var cmyk = ColorHelper.ColorConverter.HslToCmyk(new(h, (byte)s, (byte)l));

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
CMYKTxt.Text = $"{Properties.Resources.CMYK} {cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}";

hslColor = $"({h},{s},{l})"; // Set
hsvColor = $"({hsv.H},{hsv.S},{hsv.V})"; // Set
rgbColor = $"{rgb.R};{rgb.G};{rgb.B}"; // Set
hexColor = $"#{hex.Value}"; // Set
cmykColor = $"{cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}"; // Set

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 ConvertCMYK()
{
try
{
int c = int.Parse(CTxt.Text);
int m = int.Parse(MTxt.Text);
int y = int.Parse(YTxt.Text);
int k = int.Parse(KTxt.Text);
var rgb = ColorHelper.ColorConverter.CmykToRgb(new((byte)c, (byte)m, (byte)y, (byte)k)); // Convert color
var hex = ColorHelper.ColorConverter.CmykToHex(new((byte)c, (byte)m, (byte)y, (byte)k)); // Convert color
var hsl = ColorHelper.ColorConverter.CmykToHsl(new((byte)c, (byte)m, (byte)y, (byte)k)); // Convert color
var hsv = ColorHelper.ColorConverter.CmykToHsv(new((byte)c, (byte)m, (byte)y, (byte)k)); // Convert color

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} ({hsl.H},{hsl.S},{hsl.L})"; // Set text
CMYKTxt.Text = $"{Properties.Resources.CMYK} {c},{m},{y},{k}"; // Set text

hslColor = $"({hsl.H},{hsl.S},{hsl.L})"; // Set
hsvColor = $"({hsv.H},{hsv.S},{hsv.V})"; // Set
rgbColor = $"{rgb.R};{rgb.G};{rgb.B}"; // Set
hexColor = $"#{hex.Value}"; // Set
cmykColor = $"{c},{m},{y},{k}"; // Set

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

Expand Down Expand Up @@ -236,6 +288,11 @@ private void LTxt_TextChanged(object sender, TextChangedEventArgs e)
ConvertHSL();
}

private void CTxt_TextChanged(object sender, TextChangedEventArgs e)
{
ConvertCMYK();
}

private void CopyHSLBtn_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(hslColor);
Expand All @@ -260,27 +317,46 @@ private void ColorTypeComboBox_SelectionChanged(object sender, SelectionChangedE
SatTxt.Text = ""; // Clear
ValTxt.Text = ""; // Clear

HTxt.Text = ""; // Clear
STxt.Text = ""; // Clear
LTxt.Text = ""; // Clear

CTxt.Text = ""; // Clear
MTxt.Text = ""; // Clear
YTxt.Text = ""; // Clear
KTxt.Text = ""; // Clear

switch (ColorTypeComboBox.SelectedIndex)
{
case 0: // RGB
ColorTxt.Visibility = Visibility.Visible; // Show
HSVGrid.Visibility = Visibility.Collapsed; // Hide
HSLGrid.Visibility = Visibility.Collapsed; // Hide
CMYKGrid.Visibility = Visibility.Collapsed; // Hide
break;
case 1: // HEX
ColorTxt.Visibility = Visibility.Visible; // Show
HSVGrid.Visibility = Visibility.Collapsed; // Hide
HSLGrid.Visibility = Visibility.Collapsed; // Hide
CMYKGrid.Visibility = Visibility.Collapsed; // Hide
break;
case 2: // HSV
ColorTxt.Visibility = Visibility.Collapsed; // Hide
HSVGrid.Visibility = Visibility.Visible; // Show
HSLGrid.Visibility = Visibility.Collapsed; // Hide
CMYKGrid.Visibility = Visibility.Collapsed; // Hide
break;
case 3: // HSL
ColorTxt.Visibility = Visibility.Collapsed; // Hide
HSVGrid.Visibility = Visibility.Collapsed; // Hide
HSLGrid.Visibility = Visibility.Visible; // Show
CMYKGrid.Visibility = Visibility.Collapsed; // Hide
break;
case 4: // CMYK
ColorTxt.Visibility = Visibility.Collapsed; // Hide
HSVGrid.Visibility = Visibility.Collapsed; // Hide
HSLGrid.Visibility = Visibility.Collapsed; // Hide
CMYKGrid.Visibility = Visibility.Visible; // Show
break;
}
}
Expand Down
54 changes: 54 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.

18 changes: 18 additions & 0 deletions ColorPicker/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,22 @@
<data name="CopyHSL" xml:space="preserve">
<value>Copy HSL</value>
</data>
<data name="CMYK" xml:space="preserve">
<value>CMYK</value>
</data>
<data name="CopyCMYK" xml:space="preserve">
<value>Copy CMYK</value>
</data>
<data name="C" xml:space="preserve">
<value>C</value>
</data>
<data name="M" xml:space="preserve">
<value>M</value>
</data>
<data name="Y" xml:space="preserve">
<value>Y</value>
</data>
<data name="K" xml:space="preserve">
<value>K</value>
</data>
</root>
18 changes: 18 additions & 0 deletions ColorPicker/Properties/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,22 @@
<data name="CopyHSL" xml:space="preserve">
<value>Copier HSL</value>
</data>
<data name="CMYK" xml:space="preserve">
<value>CMJN</value>
</data>
<data name="CopyCMYK" xml:space="preserve">
<value>Copier CMJN</value>
</data>
<data name="C" xml:space="preserve">
<value>C</value>
</data>
<data name="M" xml:space="preserve">
<value>M</value>
</data>
<data name="Y" xml:space="preserve">
<value>J</value>
</data>
<data name="K" xml:space="preserve">
<value>N</value>
</data>
</root>
Loading

0 comments on commit 0ffda74

Please sign in to comment.