using BigFloatNumerics; 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("{1}", colorStr, targetStr); return _stringBuilder.ToString(); } public static bool IsLittleEndian() { uint tmp = 0x000000ff; byte[] bytes = 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; } public static double PermutaitionPowAdd(double v, double goalPow) { if(v == 1) return goalPow; return (v - Math.Pow(v, goalPow + 1)) / (1 - v); } public static float GetAngle2D(UnityEngine.Vector2 v) { return Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg; } public static UnityEngine.Vector2 GetDirection2D(float angle) { float rad = angle * Mathf.Deg2Rad; return new UnityEngine.Vector2(Mathf.Cos(rad), Mathf.Sin(rad)); } public static BigInteger Clamp(BigInteger value, BigInteger min, BigInteger max) { if(value < min) return min; else if(value > max) return max; else return value; } public static BigInteger Max(BigInteger a, BigInteger b) { return a > b ? a : b; } public static BigInteger Min(BigInteger a, BigInteger b) { return a < b ? a : b; } } [Serializable] public struct EditableKeyValue { public TKey key; public TValue value; } public static class Formula { /// /// (이전 값 * 계수) + 가산 = 현재 값 을 계산하는 수식 /// /// 기본값 /// 계수 /// 가산 /// 현재 레벨 public static BigFloat Calculate(BigFloat baseV, BigFloat multiplier, BigFloat adder, uint level) { if (level <= 1) return baseV; //NOTE : baseV * (multiplier ^ (level - 1)) + adder * (total add value of multiplier pow value to (level - 2)) BigFloat a = baseV * Math.Pow(multiplier, level - 1); BigFloat b = adder * (1 + Utility.PermutaitionPowAdd(multiplier, level - 2)); return a + b; } }