From ea4b914f9da2c01d6db0960338e5699d91e89419 Mon Sep 17 00:00:00 2001 From: Joelius300 Date: Mon, 5 Oct 2020 22:06:10 +0200 Subject: [PATCH] Respect System.Drawing.Colors Alpha when appropriate Plus minor refactor of ColorUtil --- src/ChartJs.Blazor/Util/ColorUtil.cs | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/ChartJs.Blazor/Util/ColorUtil.cs b/src/ChartJs.Blazor/Util/ColorUtil.cs index f7dba516..9d8bcbd4 100644 --- a/src/ChartJs.Blazor/Util/ColorUtil.cs +++ b/src/ChartJs.Blazor/Util/ColorUtil.cs @@ -11,46 +11,40 @@ public static class ColorUtil private static readonly Random _rand = new Random(); /// - /// Produces a string of the form 'rgba(r, g, b, 1)' with the provided rgb values where the alpha is fixed at 1 + /// Produces a string in the form '#rrggbb' with the provided rgb values. /// /// /// /// - /// - public static string ColorString(byte r, byte g, byte b) + public static string ColorHexString(byte r, byte g, byte b) { - return $"rgba({r}, {g}, {b}, 1)"; + return $"#{r:X2}{g:X2}{b:X2}"; } /// - /// Produces a string of the form '#aabbc' with the provided rgb values + /// Produces a string in the form 'rgba(r, g, b, 1)' with the provided rgb values where the alpha is fixed at 1. /// /// /// /// /// - public static string ColorHexString(byte r, byte g, byte b) - { - return $"#{r:X2}{g:X2}{b:X2}"; - } + public static string ColorString(byte r, byte g, byte b) => ColorString(r, g, b, 1); /// - /// Produces a string of the form 'rgba(r, g, b, alpha)' with the provided rgb and alpha values + /// Produces a string in the form 'rgba(r, g, b, alpha)' with the provided rgb and alpha values. /// /// /// /// /// - /// public static string ColorString(byte r, byte g, byte b, double alpha) { return $"rgba({r}, {g}, {b}, {alpha.ToString(CultureInfo.InvariantCulture)})"; } /// - /// Produces a string of the form 'rgba(r, g, b, alpha)' with random values for rgb and alpha + /// Produces a string of the form 'rgba(r, g, b, alpha)' with random values for rgb and alpha. /// - /// public static string RandomColorString() { byte[] rgb = new byte[3]; @@ -66,12 +60,19 @@ public static string RandomColorString() } /// - /// Generates the corresponding string representation (as hex) of a object. + /// Generates the corresponding string representation of a object. + /// Depending on the value, it's returned as hex string or as rgba string. /// - /// The string representation as a hex color string public static string FromDrawingColor(System.Drawing.Color color) { - return ColorHexString(color.R, color.G, color.B); + if (color.A != byte.MaxValue) + { + return ColorString(color.R, color.G, color.B, (double)color.A / byte.MaxValue); + } + else + { + return ColorHexString(color.R, color.G, color.B); + } } } }