1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Added a helper method to convert colors to their HEX-representation

This commit is contained in:
Darth Affe 2018-03-03 10:23:50 +01:00
parent f323f6206d
commit 54f7ec97d1
4 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,34 @@
namespace RGB.NET.Core
{
/// <summary>
/// Contains helper methods for converting things.
/// </summary>
public static class ConversionHelper
{
#region Methods
// Source: https://web.archive.org/web/20180224104425/https://stackoverflow.com/questions/623104/byte-to-hex-string/3974535
/// <summary>
/// Converts an array of bytes to a HEX-representation.
/// </summary>
/// <param name="bytes">The array of bytes.</param>
/// <returns>The HEX-representation of the provided bytes.</returns>
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
}
}

View File

@ -327,6 +327,18 @@ namespace RGB.NET.Core
}
}
/// <summary>
/// Gets the current color as a RGB-HEX-string.
/// </summary>
/// <returns>The RGB-HEX-string.</returns>
public string AsRGBHexString() => ConversionHelper.ToHex(R, G, B);
/// <summary>
/// Gets the current color as a ARGB-HEX-string.
/// </summary>
/// <returns>The ARGB-HEX-string.</returns>
public string AsARGBHexString() => ConversionHelper.ToHex(A, R, G, B);
#region Deconstruction
/// <summary>

View File

@ -64,6 +64,7 @@
<Compile Include="Devices\Layout\LedImage.cs" />
<Compile Include="Devices\Layout\LedImageLayout.cs" />
<Compile Include="Devices\RGBDeviceLighting.cs" />
<Compile Include="Helper\ConversionHelper.cs" />
<Compile Include="Helper\CultureHelper.cs" />
<Compile Include="Helper\PathHelper.cs" />
<Compile Include="Leds\LedId.cs" />

View File

@ -10,7 +10,7 @@ namespace RGB.NET.Devices.Debug
#region Properties & Fields
/// <inheritdoc />
public bool RequiresInitialization => false;
public bool RequiresInitialization => true;
#endregion