Skip to content

Commit

Permalink
Improve HexToRGB24Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasweimann committed May 19, 2024
1 parent 5c6507d commit 2e15665
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/RxTelegram.Bot/Utils/HexToRGB24Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@

namespace RxTelegram.Bot.Utils;

public class HexToRgb24
public static class HexToRgb24
{
public int Convert(string hexColor) => int.Parse(FormatHexString(hexColor), NumberStyles.HexNumber);
public static int Convert(string hexColor)
{
var formatHexString = FormatHexString(hexColor);
if (formatHexString.Length != 6)
{
throw new ArgumentException("Invalid hex color", nameof(hexColor));
}
return int.Parse(formatHexString, NumberStyles.HexNumber);
}

public bool TryConvert(string hexColor, out int result) => int.TryParse(FormatHexString(hexColor), NumberStyles.HexNumber, null, out result);
public static bool TryConvert(string hexColor, out int result)
{
var formatHexString = FormatHexString(hexColor);
if (formatHexString.Length == 6)
{
return int.TryParse(FormatHexString(hexColor), NumberStyles.HexNumber, null, out result);
}

result = -1;
return false;
}

private static string FormatHexString(string hexColor)
{
Expand All @@ -16,10 +34,6 @@ private static string FormatHexString(string hexColor)
{
hexColor = string.Concat(hexColor[0], hexColor[0], hexColor[1], hexColor[1], hexColor[2], hexColor[2]);
}
else if (hexColor.Length != 6)
{
throw new ArgumentException("Hex color must be 3 or 6 characters long.");
}

return hexColor;
}
Expand Down
62 changes: 62 additions & 0 deletions src/UnitTests/HexToRgb24Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using NUnit.Framework;
using RxTelegram.Bot.Utils;

namespace RxTelegram.Bot.UnitTests;

[TestFixture]
public class HexToRgb24Tests
{
[TestCase("#FF0000", ExpectedResult = 16711680)] // Red
[TestCase("#00FF00", ExpectedResult = 65280)] // Green
[TestCase("#0000FF", ExpectedResult = 255)] // Blue
[TestCase("#000000", ExpectedResult = 0)] // Black
[TestCase("#FFFFFF", ExpectedResult = 16777215)] // White
[TestCase("#ABCDEF", ExpectedResult = 11259375)] // Random color
public int Convert_ValidHexColor_ReturnsRgbValue(string hexColor) => HexToRgb24.Convert(hexColor);

[TestCase("#0000000")]
[TestCase("#00000000")]
public void Convert_InvalidHexLength_ThrowsArgumentException(string hexColor) =>
Assert.Throws<ArgumentException>(() => HexToRgb24.Convert(hexColor));

[TestCase("#G00000")]
[TestCase("#00000G")]
public void Convert_InvalidHexColor_ThrowsFormatException(string hexColor) =>
Assert.Throws<FormatException>(() => HexToRgb24.Convert(hexColor));

[TestCase("#0000000")]
[TestCase("#00000000")]
public void TryConvert_InvalidHexLength_ThrowsArgumentException(string hexColor) =>
Assert.That(HexToRgb24.TryConvert(hexColor, out _), Is.False);

[TestCase("#G00000")]
[TestCase("#00000G")]
public void TryConvert_InvalidHexColor_ThrowsFormatException(string hexColor) =>
Assert.That(HexToRgb24.TryConvert(hexColor, out _), Is.False);

[TestCase("#FF0000", ExpectedResult = true)] // Red
[TestCase("#00FF00", ExpectedResult = true)] // Green
[TestCase("#0000FF", ExpectedResult = true)] // Blue
[TestCase("#000000", ExpectedResult = true)] // Black
[TestCase("#FFFFFF", ExpectedResult = true)] // White
[TestCase("#ABCDEF", ExpectedResult = true)] // Random color
[TestCase("#G00000", ExpectedResult = false)]
[TestCase("#00000G", ExpectedResult = false)]
[TestCase("#0000000", ExpectedResult = false)]
[TestCase("#00000000", ExpectedResult = false)]
public bool TryConvert_ValidHexColor_ReturnsTrue(string hexColor) => HexToRgb24.TryConvert(hexColor, out _);

[TestCase("#FF0000", 16711680)] // Red
[TestCase("#00FF00", 65280)] // Green
[TestCase("#0000FF", 255)] // Blue
[TestCase("#000000", 0)] // Black
[TestCase("#FFFFFF", 16777215)] // White
[TestCase("#ABCDEF", 11259375)] // Random color
public void TryConvert_ValidHexColor_ReturnsRgbValue(string hexColor, int expected)
{
HexToRgb24.TryConvert(hexColor, out var result);
Assert.That(result, Is.EqualTo(expected));
}

}

0 comments on commit 2e15665

Please sign in to comment.