-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from Geodan/use_rgba_hexcodes
use rgba hexcodes not argb
- Loading branch information
Showing
6 changed files
with
71 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
using System.Drawing; | ||
using NUnit.Framework; | ||
using NUnit.Framework; | ||
|
||
namespace Wkb2Gltf.Tests; | ||
|
||
public class ColorTests | ||
{ | ||
|
||
[Test] | ||
public void HexColorToRgbTest() | ||
{ | ||
var hexcolor = "#ff5555"; | ||
var color = ColorTranslator.FromHtml(hexcolor); | ||
Assert.That(color.R == 255 && color.G == 85 && color.B == 85, Is.True); | ||
var color = RgbaColor.FromHex(hexcolor); | ||
Assert.That(color.R == 255 && color.G == 85 && color.B == 85); | ||
} | ||
|
||
[Test] | ||
public void HexColorWithAlphaToRgbaTest() | ||
{ | ||
var hexcolor = "#55ff5657"; | ||
var color = ColorTranslator.FromHtml(hexcolor); | ||
Assert.That(color.A == 85 && color.R == 255 && color.G == 86 && color.B == 87, Is.True); | ||
var hexcolor = "#FF000055"; | ||
var color = RgbaColor.FromHex(hexcolor); | ||
Assert.That(color.A == 85 && color.R == 255 && color.G == 0 && color.B == 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Drawing; | ||
using System.Globalization; | ||
|
||
namespace Wkb2Gltf; | ||
|
||
public static class RgbaColor | ||
{ | ||
public static string ToHex(Color color) | ||
{ | ||
return $"#{color.R:X2}{color.G:X2}{color.B:X2}{color.A:X2}"; | ||
} | ||
public static Color FromHex(string hex) | ||
{ | ||
var hexSpan = hex.AsSpan(); | ||
|
||
if (hexSpan.StartsWith("#")) { | ||
hexSpan = hexSpan.Slice(1); | ||
} | ||
|
||
var rgb = GetRgb(hexSpan); | ||
|
||
if (hexSpan.Length == 8) { | ||
var alpha = byte.Parse(hexSpan.Slice(6, 2), NumberStyles.HexNumber); | ||
return Color.FromArgb(alpha, rgb.red, rgb.green, rgb.blue); | ||
} | ||
else if (hexSpan.Length == 6) { | ||
return Color.FromArgb(255, rgb.red, rgb.green, rgb.blue); | ||
} | ||
else { | ||
throw new ArgumentException("Hex-color code must be 6 or 8 characters."); | ||
} | ||
} | ||
|
||
private static (int red, int green, int blue) GetRgb(ReadOnlySpan<char> hexSpan) | ||
{ | ||
var red = byte.Parse(hexSpan.Slice(0, 2), NumberStyles.HexNumber); | ||
var green = byte.Parse(hexSpan.Slice(2, 2), NumberStyles.HexNumber); | ||
var blue = byte.Parse(hexSpan.Slice(4, 2), NumberStyles.HexNumber); | ||
return (red, green, blue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters