Skip to content

Commit d6644ee

Browse files
committed
Add hexadecimal color convert
1 parent 54dd621 commit d6644ee

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using WebUntisAPI.Client.Extensions;
8+
9+
namespace API.Test.ExtensionsTests;
10+
11+
[TestFixture]
12+
internal class ColorExtensionsTests
13+
{
14+
[Test]
15+
public void ToHexColorFormat()
16+
{
17+
// Example 1
18+
Color c1 = Color.FromArgb(200, 150, 255);
19+
Assert.That(c1.ToHexColorFormat(), Is.EqualTo("C896FF"));
20+
21+
// Example 2
22+
Color c2 = Color.FromArgb(10, 16, 0);
23+
Assert.That(c2.ToHexColorFormat(), Is.EqualTo("0A1000"));
24+
}
25+
26+
[Test]
27+
public void FromHexColorFormat()
28+
{
29+
// Example 1
30+
Color c1 = Color.FromArgb(200, 150, 255);
31+
Assert.That(new Color().FromHexColorFormat("C896FF"), Is.EqualTo(c1));
32+
33+
// Example 2
34+
Color c2 = Color.FromArgb(10, 16, 0);
35+
Assert.That(new Color().FromHexColorFormat("0A1000"), Is.EqualTo(c2));
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Drawing;
7+
using System.Text.RegularExpressions;
8+
9+
namespace WebUntisAPI.Client.Extensions
10+
{
11+
/// <summary>
12+
/// Extensions for the <see cref="Color"/> struct
13+
/// </summary>
14+
public static class ColorExtensions
15+
{
16+
/// <summary>
17+
/// Convert a color instance to the Hexadeciaml format: RRGGBB
18+
/// </summary>
19+
/// <param name="color"></param>
20+
/// <returns>The color string</returns>
21+
public static string ToHexColorFormat(this Color color)
22+
{
23+
return color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
24+
}
25+
26+
/// <summary>
27+
/// Convert a color string to the Hexadecimal format: RRGGBB
28+
/// </summary>
29+
/// <param name="color"></param>
30+
/// <returns>The created color</returns>
31+
/// <exception cref="ArgumentException">Thrown when the <paramref name="colorString"/> isn't in the correct format</exception>
32+
public static Color FromHexColorFormat(this Color color, string colorString)
33+
{
34+
if (!Regex.IsMatch(colorString, @"[\da-fA-F]{6}"))
35+
throw new ArgumentException("The color string isn't in a correct format", nameof(colorString));
36+
37+
// Convert the hex string to the single color channels
38+
int red = Convert.ToByte(colorString.Substring(0, 2), 16);
39+
int green = Convert.ToByte(colorString.Substring(2, 2), 16);
40+
int blue = Convert.ToByte(colorString.Substring(4, 2), 16);
41+
42+
return Color.FromArgb(red, green, blue);
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)