2021-12-19 04:10:43 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Starpelly
|
|
|
|
{
|
2022-01-14 02:33:51 +00:00
|
|
|
public static class Colors
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
2022-02-08 01:07:03 +00:00
|
|
|
public static string Color2Hex(this Color color)
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
2022-02-08 01:07:03 +00:00
|
|
|
Color32 col = (Color32)color;
|
|
|
|
string hex = col.r.ToString("X2") + col.g.ToString("X2") + col.b.ToString("X2");
|
2021-12-19 04:10:43 +00:00
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Converts a Hexadecimal Color to an RGB Color.
|
|
|
|
/// </summary>
|
2022-01-14 02:33:51 +00:00
|
|
|
public static Color Hex2RGB(this string hex)
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
2023-10-29 19:44:47 +00:00
|
|
|
if (hex is null or "") return Color.black;
|
2021-12-19 04:10:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
hex = hex.Replace("0x", "");//in case the string is formatted 0xFFFFFF
|
|
|
|
hex = hex.Replace("#", "");//in case the string is formatted #FFFFFF
|
|
|
|
byte a = 255;//assume fully visible unless specified in hex
|
|
|
|
byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
|
|
|
|
byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
|
|
|
|
byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
|
|
|
|
//Only use alpha if the string has enough characters
|
2023-10-29 19:44:47 +00:00
|
|
|
if (hex.Length >= 8)
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
|
|
|
a = byte.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
|
|
|
|
}
|
|
|
|
return new Color32(r, g, b, a);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return Color.black;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|