From 54f7ec97d183fce9023770ba6d796aedb6f3c3fd Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 3 Mar 2018 10:23:50 +0100 Subject: [PATCH] Added a helper method to convert colors to their HEX-representation --- RGB.NET.Core/Helper/ConversionHelper.cs | 34 +++++++++++++++++++ RGB.NET.Core/Leds/Color.cs | 12 +++++++ RGB.NET.Core/RGB.NET.Core.csproj | 1 + .../DebugDeviceProviderLoader.cs | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 RGB.NET.Core/Helper/ConversionHelper.cs diff --git a/RGB.NET.Core/Helper/ConversionHelper.cs b/RGB.NET.Core/Helper/ConversionHelper.cs new file mode 100644 index 0000000..dca7dab --- /dev/null +++ b/RGB.NET.Core/Helper/ConversionHelper.cs @@ -0,0 +1,34 @@ +namespace RGB.NET.Core +{ + /// + /// Contains helper methods for converting things. + /// + public static class ConversionHelper + { + #region Methods + + // Source: https://web.archive.org/web/20180224104425/https://stackoverflow.com/questions/623104/byte-to-hex-string/3974535 + /// + /// Converts an array of bytes to a HEX-representation. + /// + /// The array of bytes. + /// The HEX-representation of the provided bytes. + public static string ToHex(params byte[] bytes) + { + char[] c = new char[bytes.Length * 2]; + + for (int bx = 0, cx = 0; bx < bytes.Length; ++bx, ++cx) + { + byte b = ((byte)(bytes[bx] >> 4)); + c[cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30); + + b = ((byte)(bytes[bx] & 0x0F)); + c[++cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30); + } + + return new string(c); + } + + #endregion + } +} diff --git a/RGB.NET.Core/Leds/Color.cs b/RGB.NET.Core/Leds/Color.cs index c638e00..76317e3 100644 --- a/RGB.NET.Core/Leds/Color.cs +++ b/RGB.NET.Core/Leds/Color.cs @@ -327,6 +327,18 @@ namespace RGB.NET.Core } } + /// + /// Gets the current color as a RGB-HEX-string. + /// + /// The RGB-HEX-string. + public string AsRGBHexString() => ConversionHelper.ToHex(R, G, B); + + /// + /// Gets the current color as a ARGB-HEX-string. + /// + /// The ARGB-HEX-string. + public string AsARGBHexString() => ConversionHelper.ToHex(A, R, G, B); + #region Deconstruction /// diff --git a/RGB.NET.Core/RGB.NET.Core.csproj b/RGB.NET.Core/RGB.NET.Core.csproj index b1162a5..f2eca93 100644 --- a/RGB.NET.Core/RGB.NET.Core.csproj +++ b/RGB.NET.Core/RGB.NET.Core.csproj @@ -64,6 +64,7 @@ + diff --git a/RGB.NET.Devices.Debug/DebugDeviceProviderLoader.cs b/RGB.NET.Devices.Debug/DebugDeviceProviderLoader.cs index e7c9b52..a21ce4b 100644 --- a/RGB.NET.Devices.Debug/DebugDeviceProviderLoader.cs +++ b/RGB.NET.Devices.Debug/DebugDeviceProviderLoader.cs @@ -10,7 +10,7 @@ namespace RGB.NET.Devices.Debug #region Properties & Fields /// - public bool RequiresInitialization => false; + public bool RequiresInitialization => true; #endregion