// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Drawing; using System.Globalization; using Microsoft.PowerToys.Settings.UI.Library.Enumerations; namespace ColorPicker.Helpers { /// /// Helper class to easier work with color representation /// internal static class ColorRepresentationHelper { /// /// Return a representation of a given /// /// The for the presentation /// The type of the representation /// A representation of a color internal static string GetStringRepresentationFromMediaColor(System.Windows.Media.Color color, ColorRepresentationType colorRepresentationType) { var drawingcolor = Color.FromArgb(color.A, color.R, color.G, color.B); return GetStringRepresentation(drawingcolor, colorRepresentationType); } /// /// Return a representation of a given /// /// The for the presentation /// The type of the representation /// A representation of a color internal static string GetStringRepresentation(Color color, ColorRepresentationType colorRepresentationType) => colorRepresentationType switch { ColorRepresentationType.CMYK => ColorToCMYK(color), ColorRepresentationType.HEX => ColorToHex(color), ColorRepresentationType.HSB => ColorToHSB(color), ColorRepresentationType.HSI => ColorToHSI(color), ColorRepresentationType.HSL => ColorToHSL(color), ColorRepresentationType.HSV => ColorToHSV(color), ColorRepresentationType.HWB => ColorToHWB(color), ColorRepresentationType.NCol => ColorToNCol(color), ColorRepresentationType.RGB => ColorToRGB(color), ColorRepresentationType.CIELAB => ColorToCIELAB(color), ColorRepresentationType.CIEXYZ => ColorToCIEXYZ(color), ColorRepresentationType.VEC4 => ColorToFloat(color), ColorRepresentationType.DecimalValue => ColorToDecimal(color), // Fall-back value, when "_userSettings.CopiedColorRepresentation.Value" is incorrect _ => ColorToHex(color), }; /// /// Return a representation of a CMYK color /// /// The for the CMYK color presentation /// A representation of a CMYK color private static string ColorToCMYK(Color color) { var (cyan, magenta, yellow, blackKey) = ColorHelper.ConvertToCMYKColor(color); cyan = Math.Round(cyan * 100); magenta = Math.Round(magenta * 100); yellow = Math.Round(yellow * 100); blackKey = Math.Round(blackKey * 100); return $"cmyk({cyan.ToString(CultureInfo.InvariantCulture)}%" + $", {magenta.ToString(CultureInfo.InvariantCulture)}%" + $", {yellow.ToString(CultureInfo.InvariantCulture)}%" + $", {blackKey.ToString(CultureInfo.InvariantCulture)}%)"; } /// /// Return a hexadecimal representation of a RGB color /// /// The see cref="Color"/> for the hexadecimal presentation /// A hexadecimal representation of a RGB color private static string ColorToHex(Color color) => $"{color.R.ToString("X2", CultureInfo.InvariantCulture)}" + $"{color.G.ToString("X2", CultureInfo.InvariantCulture)}" + $"{color.B.ToString("X2", CultureInfo.InvariantCulture)}"; /// /// Return a representation of a HSB color /// /// The for the HSB color presentation /// A representation of a HSB color private static string ColorToHSB(Color color) { var (hue, saturation, brightness) = ColorHelper.ConvertToHSBColor(color); hue = Math.Round(hue); saturation = Math.Round(saturation * 100); brightness = Math.Round(brightness * 100); return $"hsb({hue.ToString(CultureInfo.InvariantCulture)}" + $", {saturation.ToString(CultureInfo.InvariantCulture)}%" + $", {brightness.ToString(CultureInfo.InvariantCulture)}%)"; } /// /// Return a representation float color styling(0.1f, 0.1f, 0.1f) /// /// The to convert /// a string value (0.1f, 0.1f, 0.1f) private static string ColorToFloat(Color color) { var (red, green, blue) = ColorHelper.ConvertToDouble(color); var precision = 2; return $"({Math.Round(red, precision).ToString("0.##", CultureInfo.InvariantCulture)}f, {Math.Round(green, precision).ToString("0.##", CultureInfo.InvariantCulture)}f, {Math.Round(blue, precision).ToString("0.##", CultureInfo.InvariantCulture)}f, 1f)"; } /// /// Return a representation decimal color value /// /// The to convert /// a string value number private static string ColorToDecimal(Color color) { return $"{color.R + (color.G * 256) + (color.B * 65536)}"; } /// /// Return a representation of a HSI color /// /// The for the HSI color presentation /// A representation of a HSI color private static string ColorToHSI(Color color) { var (hue, saturation, intensity) = ColorHelper.ConvertToHSIColor(color); hue = Math.Round(hue); saturation = Math.Round(saturation * 100); intensity = Math.Round(intensity * 100); return $"hsi({hue.ToString(CultureInfo.InvariantCulture)}" + $", {saturation.ToString(CultureInfo.InvariantCulture)}%" + $", {intensity.ToString(CultureInfo.InvariantCulture)}%)"; } /// /// Return a representation of a HSL color /// /// The for the HSL color presentation /// A representation of a HSL color private static string ColorToHSL(Color color) { var (hue, saturation, lightness) = ColorHelper.ConvertToHSLColor(color); hue = Math.Round(hue); saturation = Math.Round(saturation * 100); lightness = Math.Round(lightness * 100); // Using InvariantCulture since this is used for color representation return $"hsl({hue.ToString(CultureInfo.InvariantCulture)}" + $", {saturation.ToString(CultureInfo.InvariantCulture)}%" + $", {lightness.ToString(CultureInfo.InvariantCulture)}%)"; } /// /// Return a representation of a HSV color /// /// The for the HSV color presentation /// A representation of a HSV color private static string ColorToHSV(Color color) { var (hue, saturation, value) = ColorHelper.ConvertToHSVColor(color); hue = Math.Round(hue); saturation = Math.Round(saturation * 100); value = Math.Round(value * 100); // Using InvariantCulture since this is used for color representation return $"hsv({hue.ToString(CultureInfo.InvariantCulture)}" + $", {saturation.ToString(CultureInfo.InvariantCulture)}%" + $", {value.ToString(CultureInfo.InvariantCulture)}%)"; } /// /// Return a representation of a HWB color /// /// The for the HWB color presentation /// A representation of a HWB color private static string ColorToHWB(Color color) { var (hue, whiteness, blackness) = ColorHelper.ConvertToHWBColor(color); hue = Math.Round(hue); whiteness = Math.Round(whiteness * 100); blackness = Math.Round(blackness * 100); return $"hwb({hue.ToString(CultureInfo.InvariantCulture)}" + $", {whiteness.ToString(CultureInfo.InvariantCulture)}%" + $", {blackness.ToString(CultureInfo.InvariantCulture)}%)"; } /// /// Return a representation of a natural color /// /// The for the natural color presentation /// A representation of a natural color private static string ColorToNCol(Color color) { var (hue, whiteness, blackness) = ColorHelper.ConvertToNaturalColor(color); whiteness = Math.Round(whiteness * 100); blackness = Math.Round(blackness * 100); return $"{hue}" + $", {whiteness.ToString(CultureInfo.InvariantCulture)}%" + $", {blackness.ToString(CultureInfo.InvariantCulture)}%"; } /// /// Return a representation of a RGB color /// /// The for the RGB color presentation /// A representation of a RGB color private static string ColorToRGB(Color color) => $"rgb({color.R.ToString(CultureInfo.InvariantCulture)}" + $", {color.G.ToString(CultureInfo.InvariantCulture)}" + $", {color.B.ToString(CultureInfo.InvariantCulture)})"; /// /// Returns a representation of a CIE LAB color /// /// The for the CIE LAB color presentation /// A representation of a CIE LAB color private static string ColorToCIELAB(Color color) { var (lightness, chromaticityA, chromaticityB) = ColorHelper.ConvertToCIELABColor(color); lightness = Math.Round(lightness, 2); chromaticityA = Math.Round(chromaticityA, 2); chromaticityB = Math.Round(chromaticityB, 2); return $"CIELab({lightness.ToString(CultureInfo.InvariantCulture)}" + $", {chromaticityA.ToString(CultureInfo.InvariantCulture)}" + $", {chromaticityB.ToString(CultureInfo.InvariantCulture)})"; } /// /// Returns a representation of a CIE XYZ color /// /// The for the CIE XYZ color presentation /// A representation of a CIE XYZ color private static string ColorToCIEXYZ(Color color) { var (x, y, z) = ColorHelper.ConvertToCIEXYZColor(color); x = Math.Round(x * 100, 4); y = Math.Round(y * 100, 4); z = Math.Round(z * 100, 4); return $"xyz({x.ToString(CultureInfo.InvariantCulture)}" + $", {y.ToString(CultureInfo.InvariantCulture)}" + $", {z.ToString(CultureInfo.InvariantCulture)})"; } } }