mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Fix color parsing and serialization
This commit is contained in:
22
Source/ConformalDecals/Util/ColorUtil.cs
Normal file
22
Source/ConformalDecals/Util/ColorUtil.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user