一些工具备用日后整理
using System; using System.IO; using UnityEngine; namespace Tool.Tools { class NameTools { /// <summary> /// 生成和时间相关的字符串 /// </summary> /// <returns>string "YYYY-MM-DD-hh-mm-ss-mmm"</returns> public static string NowTime() { string year = DateTime.Now.Year.ToString("0000"); string month = DateTime.Now.Month.ToString("00"); string day = DateTime.Now.Day.ToString("00"); string hour = DateTime.Now.Hour.ToString("00"); string minute = DateTime.Now.Minute.ToString("00"); string secend = DateTime.Now.Second.ToString("00"); string millisecond = DateTime.Now.Millisecond.ToString("000"); return year + "_" + month + "_" + day + "_" + hour + "_" + minute + "_" + secend + "_" + millisecond; } } class Encryption { public static string Encode(string str) { string htext = ""; for (int i = 0; i < str.Length; i++) { htext = htext + (char)(str[i] + 10 - 1 * 2); } return htext; } public static string Decode(string str) { string dtext = ""; for (int i = 0; i < str.Length; i++) { dtext = dtext + (char)(str[i] - 10 + 1 * 2); } return dtext; } } } namespace Tool.ImageTools { class ImageConversion { /// <summary> /// /// </summary> /// <param name="texture2D"></param> /// <returns></returns> public static Sprite Texture2DToSprite(Texture2D texture2D) { var sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero); return sprite; } /// <summary> /// /// </summary> /// <param name="sprite"></param> /// <returns></returns> public static Texture2D Sprite2Texture2D(Sprite sprite) { var texture2D = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height); var pixels = sprite.texture.GetPixels( (int)sprite.textureRect.x, (int)sprite.textureRect.y, (int)sprite.textureRect.width, (int)sprite.textureRect.height); texture2D.SetPixels(pixels); texture2D.Apply(); return texture2D; } /// <summary> /// /// </summary> /// <param name="path"></param> /// <returns></returns> public static string Image2Base64String(string path) { try { FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (int)fs.Length); string base64String = Convert.ToBase64String(buffer); //Debug.Log("获取当前图片base64为---" + base64String); return base64String; } catch (Exception e) { return "ImgToBase64String 转换失败:" + e.Message; } } /// <summary> /// /// </summary> /// <param name="base64String"></param> /// <param name="contentPath"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static string Base64String2Image(string base64String, string contentPath, int width, int height) { Texture2D texture2D = new Texture2D(width, height); byte[] data = Convert.FromBase64String(base64String); texture2D.LoadImage(data); byte[] bytes = texture2D.EncodeToPNG(); string FileFullPath = contentPath + "/" + Tools.NameTools.NowTime() + ".png"; File.WriteAllBytes(FileFullPath, bytes); return FileFullPath; } public static Texture2D Base64String2Texture2D(string base64String, int width, int height) { Texture2D texture2D = new Texture2D(width, height); byte[] data = Convert.FromBase64String(base64String); texture2D.LoadImage(data); return texture2D; } } class ImageSave { public static void SaveTexture2D(Texture2D texture2D, string contentPath) { if (!Directory.Exists(contentPath)) Directory.CreateDirectory(contentPath); var png = texture2D.EncodeToPNG(); FileStream file = File.Open(contentPath + "/" + Tools.NameTools.NowTime() + ".png", FileMode.Create); BinaryWriter writer = new BinaryWriter(file); writer.Write(png); file.Close(); } public static void SaveSprite(Sprite sprite, string contentPath) { SaveTexture2D(ImageConversion.Sprite2Texture2D(sprite), contentPath); } } } namespace Tool.ColorTools { public class ColorChange { static float Gamma(float x) { return x > 0.04045f ? Mathf.Pow((x + 0.055f) / 1.055f, 2.4f) : x / 12.92f; } /// <summary> /// RGB转Lab /// </summary> /// <param name="var_R">Color.r</param> /// <param name="var_G">Color.g</param> /// <param name="var_B">Color.b</param> /// <returns>float[] Lab</returns> public static float[] Rgb2Lab(float var_R, float var_G, float var_B) { float[] Lab = new float[3]; float B = Gamma(var_B); float G = Gamma(var_G); float R = Gamma(var_R); float X = 0.412453f * R + 0.357580f * G + 0.180423f * B; float Y = 0.212671f * R + 0.715160f * G + 0.072169f * B; float Z = 0.019334f * R + 0.119193f * G + 0.950227f * B; X /= 0.95047f; Y /= 1.0f; Z /= 1.08883f; float FX = X > 0.008856f ? Mathf.Pow(X, 1.0f / 3.0f) : (7.787f * X + 0.137931f); float FY = Y > 0.008856f ? Mathf.Pow(Y, 1.0f / 3.0f) : (7.787f * Y + 0.137931f); float FZ = Z > 0.008856f ? Mathf.Pow(Z, 1.0f / 3.0f) : (7.787f * Z + 0.137931f); Lab[0] = Y > 0.008856f ? (116.0f * FY - 16.0f) : (903.3f * Y); Lab[1] = 500f * (FX - FY); Lab[2] = 200f * (FY - FZ); return Lab; } } public class ColorSimilar { /// <summary> /// 比较颜色是否相似 /// </summary> /// <param name="sample">样本</param> /// <param name="standardSample">标样</param> /// <param name="labThreshold">E2000计算阈值</param> /// <param name="rgbThreshold">RGB加权计算阈值</param> /// <returns></returns> public static bool ColorGap(Color sample, Color standardSample, float labThreshold, float rgbThreshold) { float[] lab1 = ColorChange.Rgb2Lab(sample.r, sample.g, sample.b); float[] lab2 = ColorChange.Rgb2Lab(standardSample.r, standardSample.g, standardSample.b); if (ColorSimilar.Delta_E00(lab1[0], lab1[1], lab1[2], lab2[0], lab2[1], lab2[2]) < labThreshold || Dalte_Rgb(sample, standardSample) < rgbThreshold) { return true; } return false; } #region 色差公式 /// <summary> /// RGB加权公式 /// </summary> /// <param name="sample"></param> /// <param name="standardSample"></param> /// <returns></returns> static double Dalte_Rgb(Color sample, Color standardSample) { long rAverage = (long)((sample.r + standardSample.r) * 255 / 2); long rDelta = (long)((sample.r - standardSample.r) * 255); long gDelta = (long)((sample.g - standardSample.g) * 255); long bDelta = (long)((sample.b - standardSample.b) * 255); double delta = Mathf.Sqrt((2 + rAverage / 256) * rDelta * rDelta + 4 * gDelta * gDelta + (2 + (255 - rAverage) / 256) * bDelta * bDelta); return delta; } /// <summary> /// CIEDE2000色差公式 /// </summary> /// <param name="L1"></param> /// <param name="a1"></param> /// <param name="b1"></param> /// <param name="L2"></param> /// <param name="a2"></param> /// <param name="b2"></param> /// <returns></returns> static double Delta_E00(double L1, double a1, double b1, double L2, double a2, double b2) { double LL1, LL2, aa1, aa2, bb1, bb2; double delta_LL, delta_CC, delta_hh, delta_HH; double kL, kC, kH; double SL, SC, SH, T; kL = 1; kC = 1; kH = 1; double mean_Cab = (Chromaticity(a1, b1) + Chromaticity(a2, b2)) / 2; double mean_Cab_pow7 = Math.Pow(mean_Cab, 7); double G = 0.5 * (1 - Math.Pow(mean_Cab_pow7 / (mean_Cab_pow7 + Math.Pow(25, 7)), 0.5)); LL1 = L1; aa1 = a1 * (1 + G); bb1 = b1; LL2 = L2; aa2 = a2 * (1 + G); bb2 = b2; double CC1, CC2; CC1 = Chromaticity(aa1, bb1); CC2 = Chromaticity(aa2, bb2); double hh1, hh2; hh1 = HueAngle(aa1, bb1); hh2 = HueAngle(aa2, bb2); delta_LL = LL1 - LL2; delta_CC = CC1 - CC2; delta_hh = HueAngle(aa1, bb1) - HueAngle(aa2, bb2); delta_HH = 2 * Math.Sin(Math.PI * delta_hh / 360) * Math.Pow(CC1 * CC2, 0.5); double mean_LL = (LL1 + LL2) / 2; double mean_CC = (CC1 + CC2) / 2; double mean_hh = (hh1 + hh2) / 2; SL = 1 + 0.015 * Math.Pow(mean_LL - 50, 2) / Math.Pow(20 + Math.Pow(mean_LL - 50, 2), 0.5); SC = 1 + 0.045 * mean_CC; T = 1 - 0.17 * Math.Cos((mean_hh - 30) * Math.PI / 180) + 0.24 * Math.Cos((2 * mean_hh) * Math.PI / 180) + 0.32 * Math.Cos((3 * mean_hh + 6) * Math.PI / 180) - 0.2 * Math.Cos((4 * mean_hh - 63) * Math.PI / 180); SH = 1 + 0.015 * mean_CC * T; double mean_CC_pow7 = Math.Pow(mean_CC, 7); double RC = 2 * Math.Pow(mean_CC_pow7 / (mean_CC_pow7 + Math.Pow(25, 7)), 0.5); double delta_xita = 30 * Math.Exp(-Math.Pow((mean_hh - 275) / 25, 2)); double RT = -Math.Sin(2 * delta_xita * Math.PI / 180) * RC; double L_item, C_item, H_item; L_item = delta_LL / (kL * SL); C_item = delta_CC / (kC * SC); H_item = delta_HH / (kH * SH); double E00 = Math.Pow(L_item * L_item + C_item * C_item + H_item * H_item + RT * C_item * H_item, 0.5); return E00; } static double Chromaticity(double a, double b) { double Cab = Math.Pow(a * a + b * b, 0.5); return Cab; } static double HueAngle(double a, double b) { double h = 180 / Math.PI * Math.Atan(b / a); double hab; if (a > 0 && b > 0) { hab = h; } else if (a < 0 && b > 0) { hab = 180 + h; } else if (a < 0 && b < 0) { hab = 180 + h; } else { hab = 360 + h; } return hab; } #endregion } }