Fix color parsing and serialization

This commit is contained in:
2020-09-29 19:46:14 -07:00
parent bf7d5dd933
commit d1029ca0c1
5 changed files with 58 additions and 13 deletions

View File

@ -0,0 +1,22 @@
using UnityEngine;
namespace ConformalDecals.Util {
public static class ColorUtil {
/// Returns an RGBA 32-bit hex string
public static string ToHexString(this Color32 color) {
return $"#{color.r:x2}{color.g:x2}{color.b:x2}{color.a:x2}";
}
// Returns an RGBA 32-bit unsigned integer representation of the color
public static uint ToUint(this Color32 color) {
uint rgba = color.r;
rgba <<= 8;
rgba |= color.g;
rgba <<= 8;
rgba |= color.b;
rgba <<= 8;
rgba |= color.a;
return rgba;
}
}
}

View File

@ -142,7 +142,7 @@ namespace ConformalDecals.Util {
public static bool TryParseHexColor(string valueString, out Color32 value) {
value = new Color32(0, 0, 0, byte.MaxValue);
if (!int.TryParse(valueString, System.Globalization.NumberStyles.HexNumber, null, out var hexColor)) return false;
if (!uint.TryParse(valueString, System.Globalization.NumberStyles.HexNumber, null, out var hexColor)) return false;
switch (valueString.Length) {
case 8: // RRGGBBAA
@ -216,7 +216,14 @@ namespace ConformalDecals.Util {
value.g = (byte) (green * 0xFF);
value.b = (byte) (blue * 0xFF);
return true;
case 1: // try again for hex color
if (TryParseHexColor(split[0], out var hexcolor)) {
value = hexcolor;
return true;
}
else {
return false;
}
default:
return false;
}