You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

102 lines
2.7 KiB

using System;
using System.Numerics;
using System.Text;
using UnityEngine;
public static class Utility
{
static StringBuilder _stringBuilder = new StringBuilder();
public static StringBuilder StringBuilder => _stringBuilder;
public static string Color2StringWithoutAlpha(Color color)
{
string red = ((int)(color.r * 255)).ToString("X2");
string green = ((int)(color.g * 255)).ToString("X2");
string blue = ((int)(color.b * 255)).ToString("X2");
_stringBuilder.Clear();
_stringBuilder.Append(red);
_stringBuilder.Append(green);
_stringBuilder.Append(blue);
return _stringBuilder.ToString();
}
public static Color String2Color(string colorString)
{
Color result = Color.white;
colorString = colorString.ToUpper();
const int stride = 2;
int[] rgba = new int[] { 0, 0, 0, 0 };
int offset = 0;
for (int i = 0; i < colorString.Length; i++)
{
char c = colorString[i];
if (c >= 'A' && c <= 'F')
{
rgba[offset / stride] += (int)Mathf.Pow(16, (stride - (offset % stride) - 1)) * (10 + (c - 'A'));
offset++;
}
else if (c >= '0' && c <= '9')
{
rgba[offset / stride] += (int)Mathf.Pow(16, (stride - (offset % stride) - 1)) * (c - '0');
offset++;
}
}
result.r = rgba[0] / 255f;
result.g = rgba[1] / 255f;
result.b = rgba[2] / 255f;
result.a = rgba[3] / 255f;
return result;
}
public static string GetMergeStringWithColor(Color color, string targetStr)
{
string colorStr = Color2StringWithoutAlpha(color);
_stringBuilder.Clear();
_stringBuilder.AppendFormat("<color=#{0}>{1}</color>", colorStr, targetStr);
return _stringBuilder.ToString();
}
public static bool IsLittleEndian()
{
uint tmp = 0x000000ff;
byte[] bytes = System.BitConverter.GetBytes(tmp);
return bytes[0] == 0xff;
}
public static uint PermutationAdd(uint goal, uint start = 1)
{
if(start != 1)
return PermutationAdd(goal) - PermutationAdd(start - 1);
else
return goal * (goal + 1) / 2;
}
public static BigInteger PermutationAdd(BigInteger goal, BigInteger start)
{
if(goal < 0 || start < 0)
throw new ArgumentException("goal and start must be positive number.");
if(start != 1)
return PermutationAdd(goal, 1) - PermutationAdd(start - 1, 1);
else
return goal * (goal + 1) / 2;
}
}
[Serializable]
public struct EditableKeyValue<TKey, TValue>
{
public TKey key;
public TValue value;
}