Skip to content

Commit

Permalink
Added the possibility to copy a color as any color type (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed Apr 16, 2022
1 parent 8057a95 commit 051fcba
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
19 changes: 19 additions & 0 deletions ColorPicker/Classes/Global.cs
Expand Up @@ -136,6 +136,19 @@ public static string ColorTypesToString(ColorTypes colorTypes)
}; // Return value
}

public static string ColorTypesToCopyString(ColorTypes colorTypes)
{
return colorTypes switch
{
ColorTypes.HEX => Properties.Resources.CopyHEX,
ColorTypes.RGB => Properties.Resources.CopyRGB,
ColorTypes.HSV => Properties.Resources.CopyHSV,
ColorTypes.HSL => Properties.Resources.CopyHSL,
ColorTypes.CMYK => Properties.Resources.CopyCMYK,
_ => Properties.Resources.CopyRGB
}; // Return value
}

/// <summary>
/// Changes the application's theme.
/// </summary>
Expand Down Expand Up @@ -206,5 +219,11 @@ public static void ChangeLanguage()
break;
}
}

internal static string GetHsvString(ColorHelper.HSV hsv) => $"({hsv.H},{hsv.S},{hsv.V})";

internal static string GetHslString(ColorHelper.HSL hsl) => $"({hsl.H},{hsl.S},{hsl.L})";

internal static string GetCmykString(ColorHelper.CMYK cmyk) => $"{cmyk.C},{cmyk.M},{cmyk.Y},{cmyk.K}";
}
}
26 changes: 24 additions & 2 deletions ColorPicker/Pages/PickerPage.xaml.cs
Expand Up @@ -21,7 +21,9 @@ MIT License
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using ColorHelper;
using ColorPicker.Classes;
using ColorPicker.Enums;
using ColorPicker.UserControls;
using ColorPicker.Windows;
using Gma.System.MouseKeyHook;
Expand Down Expand Up @@ -96,6 +98,12 @@ public PickerPage()

private void InitUI()
{
// Set copy button text
if (Global.Settings.FavoriteColorType.Value != ColorTypes.HEX) // There is already a "Copy HEX" button
{
CopyBtn.Content = Global.ColorTypesToCopyString(Global.Settings.FavoriteColorType.Value);
}

// Generate random color
Random random = new();
int r = random.Next(0, 255); int g = random.Next(0, 255); int b = random.Next(0, 255); // Generate random values
Expand Down Expand Up @@ -250,8 +258,22 @@ private void SelectColorBtn_Click(object sender, RoutedEventArgs e)

private void CopyBtn_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText($"{RedSlider.Value}{sep}{GreenSlider.Value}{sep}{BlueSlider.Value}"); // Copy
RecentColorsDisplayer.Children.Add(new RecentColorItem((int)RedSlider.Value, (int)GreenSlider.Value, (int)BlueSlider.Value));
int r = (int)RedSlider.Value; // Red
int g = (int)GreenSlider.Value; // Green
int b = (int)BlueSlider.Value; // Blue

Clipboard.SetText(Global.Settings.FavoriteColorType switch
{
ColorTypes.RGB => $"{r}{sep}{g}{sep}{b}",
ColorTypes.HEX => "#" + (u ? ColorsConverter.RGBtoHEX(r, g, b).Value.ToUpper() : ColorsConverter.RGBtoHEX((int)RedSlider.Value, (int)GreenSlider.Value, (int)BlueSlider.Value).Value.ToLower()),
ColorTypes.HSV => Global.GetHsvString(ColorHelper.ColorConverter.RgbToHsv(new((byte)r, (byte)g, (byte)b))),
ColorTypes.HSL => Global.GetHslString(ColorHelper.ColorConverter.RgbToHsl(new((byte)r, (byte)g, (byte)b))),
ColorTypes.CMYK => Global.GetCmykString(ColorHelper.ColorConverter.RgbToCmyk(new((byte)r, (byte)g, (byte)b))),
_ => $"{r}{sep}{g}{sep}{b}"
}); // Copy


RecentColorsDisplayer.Children.Add(new RecentColorItem(r, g, b));
HistoryBtn.Visibility = Visibility.Visible;
}

Expand Down

0 comments on commit 051fcba

Please sign in to comment.