|
|
@ -1,4 +1,5 @@ |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
using System.Globalization; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine; |
|
|
|
|
|
|
|
|
|
|
|
namespace Tools |
|
|
|
namespace Tools |
|
|
@ -158,7 +159,7 @@ namespace Tools |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ColorDictionary == null) |
|
|
|
if (ColorDictionary == null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
InitializeDictionary(); |
|
|
|
initializeDictionary(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (index < ColorDictionary.Count) |
|
|
|
if (index < ColorDictionary.Count) |
|
|
@ -169,122 +170,11 @@ namespace Tools |
|
|
|
{ |
|
|
|
{ |
|
|
|
return Color.white; |
|
|
|
return Color.white; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Color RandomColor() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int random = Random.Range(0, 140); |
|
|
|
|
|
|
|
return GetColorAt(random); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 返回在指定的最小值和最大值之间的随机颜色 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="color"></param> |
|
|
|
|
|
|
|
/// <param name="min"></param> |
|
|
|
|
|
|
|
/// <param name="max"></param> |
|
|
|
|
|
|
|
/// <returns></returns> |
|
|
|
|
|
|
|
public static Color RandomColor(this Color color, Color min, Color max) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Color c = new Color() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
r = UnityEngine.Random.Range(min.r, max.r), |
|
|
|
|
|
|
|
g = UnityEngine.Random.Range(min.g, max.g), |
|
|
|
|
|
|
|
b = UnityEngine.Random.Range(min.b, max.b), |
|
|
|
|
|
|
|
a = UnityEngine.Random.Range(min.a, max.a) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return c; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 返回一个由指定颜色和 alpha 组成的均匀“平面”渐变 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="color">用于渐变两端的颜色</param> |
|
|
|
|
|
|
|
/// <param name="alpha">用于渐变两端的 alpha</param> |
|
|
|
|
|
|
|
/// <returns></returns> |
|
|
|
|
|
|
|
public static Gradient FlatGradient(Color32 color, float alpha = 1f) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new Gradient() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
colorKeys = new GradientColorKey[2] |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
new GradientColorKey(color, 0), new GradientColorKey(color, 1f) |
|
|
|
|
|
|
|
}, alphaKeys = new GradientAlphaKey[2] |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
new GradientAlphaKey(alpha, 0), new GradientAlphaKey(alpha, 1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 返回由两个指定颜色和 alpha 组成的简单渐变 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="startColor">用于渐变左侧的颜色</param> |
|
|
|
|
|
|
|
/// <param name="endColor">用于渐变右侧的颜色</param> |
|
|
|
|
|
|
|
/// <param name="startAlpha">用于渐变左侧的 alpha</param> |
|
|
|
|
|
|
|
/// <param name="endAlpha">用于渐变右侧的 alpha</param> |
|
|
|
|
|
|
|
/// <returns></returns> |
|
|
|
|
|
|
|
public static Gradient SimpleGradient(Color32 startColor, Color32 endColor, float startAlpha = 1f, float endAlpha = 1f) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new Gradient() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
colorKeys = new GradientColorKey[2] |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
new GradientColorKey(startColor, 0), new GradientColorKey(endColor, 1f) |
|
|
|
|
|
|
|
}, alphaKeys = new GradientAlphaKey[2] |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
new GradientAlphaKey(startAlpha, 0), new GradientAlphaKey(endAlpha, 1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// Tint:使用 HSV 颜色转换,保留原始值,乘以 alpha |
|
|
|
|
|
|
|
/// Multiply:整个颜色,包括 alpha,乘以原始颜色 |
|
|
|
|
|
|
|
/// Replace:完全替换原始颜色/// ReplaceKeepAlpha:颜色被替换,但原始的 alpha 通道被忽略 |
|
|
|
|
|
|
|
/// Add:目标颜色被添加(包括其 alpha 通道) |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
public enum ColoringMode { Tint, Multiply, Replace, ReplaceKeepAlpha, Add } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Color Colorize(this Color originalColor, Color targetColor, ColoringMode coloringMode, float lerpAmount = 1.0f) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Color resultColor = Color.white; |
|
|
|
|
|
|
|
switch (coloringMode) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
case ColoringMode.Tint: |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
float s_h, s_s, s_v, t_h, t_s, t_v; |
|
|
|
|
|
|
|
Color.RGBToHSV(originalColor, out s_h, out s_s, out s_v); |
|
|
|
|
|
|
|
Color.RGBToHSV(targetColor, out t_h, out t_s, out t_v); |
|
|
|
|
|
|
|
resultColor = Color.HSVToRGB(t_h, t_s, s_v * t_v); |
|
|
|
|
|
|
|
resultColor.a = originalColor.a * targetColor.a; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ColoringMode.Multiply: |
|
|
|
|
|
|
|
resultColor = originalColor * targetColor; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ColoringMode.Replace: |
|
|
|
|
|
|
|
resultColor = targetColor; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ColoringMode.ReplaceKeepAlpha: |
|
|
|
|
|
|
|
resultColor = targetColor; |
|
|
|
|
|
|
|
resultColor.a = originalColor.a; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ColoringMode.Add: |
|
|
|
|
|
|
|
resultColor = originalColor + targetColor; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return Color.Lerp(originalColor, resultColor, lerpAmount); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region InitialiezDictionary |
|
|
|
#region InitialiezDictionary |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void InitializeDictionary() |
|
|
|
static void initializeDictionary() |
|
|
|
{ |
|
|
|
{ |
|
|
|
ColorDictionary = new Dictionary<int, Color> |
|
|
|
ColorDictionary = new Dictionary<int, Color> |
|
|
|
{ |
|
|
|
{ |
|
|
@ -435,4 +325,164 @@ namespace Tools |
|
|
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
#endregion |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Color RandomColor() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int random = Random.Range(0, 140); |
|
|
|
|
|
|
|
return GetColorAt(random); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 返回在指定的最小值和最大值之间的随机颜色 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="color"></param> |
|
|
|
|
|
|
|
/// <param name="min"></param> |
|
|
|
|
|
|
|
/// <param name="max"></param> |
|
|
|
|
|
|
|
/// <returns></returns> |
|
|
|
|
|
|
|
public static Color RandomColor(this Color color, Color min, Color max) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Color c = new Color() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
r = UnityEngine.Random.Range(min.r, max.r), |
|
|
|
|
|
|
|
g = UnityEngine.Random.Range(min.g, max.g), |
|
|
|
|
|
|
|
b = UnityEngine.Random.Range(min.b, max.b), |
|
|
|
|
|
|
|
a = UnityEngine.Random.Range(min.a, max.a) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return c; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 返回一个由指定颜色和 alpha 组成的均匀“平面”渐变 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="color">用于渐变两端的颜色</param> |
|
|
|
|
|
|
|
/// <param name="alpha">用于渐变两端的 alpha</param> |
|
|
|
|
|
|
|
/// <returns></returns> |
|
|
|
|
|
|
|
public static Gradient FlatGradient(Color32 color, float alpha = 1f) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new Gradient() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
colorKeys = new GradientColorKey[2] |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
new GradientColorKey(color, 0), new GradientColorKey(color, 1f) |
|
|
|
|
|
|
|
}, alphaKeys = new GradientAlphaKey[2] |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
new GradientAlphaKey(alpha, 0), new GradientAlphaKey(alpha, 1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 返回由两个指定颜色和 alpha 组成的简单渐变 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="startColor">用于渐变左侧的颜色</param> |
|
|
|
|
|
|
|
/// <param name="endColor">用于渐变右侧的颜色</param> |
|
|
|
|
|
|
|
/// <param name="startAlpha">用于渐变左侧的 alpha</param> |
|
|
|
|
|
|
|
/// <param name="endAlpha">用于渐变右侧的 alpha</param> |
|
|
|
|
|
|
|
/// <returns></returns> |
|
|
|
|
|
|
|
public static Gradient SimpleGradient(Color32 startColor, Color32 endColor, float startAlpha = 1f, float endAlpha = 1f) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new Gradient() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
colorKeys = new GradientColorKey[2] |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
new GradientColorKey(startColor, 0), new GradientColorKey(endColor, 1f) |
|
|
|
|
|
|
|
}, alphaKeys = new GradientAlphaKey[2] |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
new GradientAlphaKey(startAlpha, 0), new GradientAlphaKey(endAlpha, 1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// Tint:使用 HSV 颜色转换,保留原始值,乘以 alpha |
|
|
|
|
|
|
|
/// Multiply:整个颜色,包括 alpha,乘以原始颜色 |
|
|
|
|
|
|
|
/// Replace:完全替换原始颜色/// ReplaceKeepAlpha:颜色被替换,但原始的 alpha 通道被忽略 |
|
|
|
|
|
|
|
/// Add:目标颜色被添加(包括其 alpha 通道) |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
public enum ColoringMode { Tint, Multiply, Replace, ReplaceKeepAlpha, Add } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Color Colorize(this Color originalColor, Color targetColor, ColoringMode coloringMode, float lerpAmount = 1.0f) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Color resultColor = Color.white; |
|
|
|
|
|
|
|
switch (coloringMode) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
case ColoringMode.Tint: |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
float s_h, s_s, s_v, t_h, t_s, t_v; |
|
|
|
|
|
|
|
Color.RGBToHSV(originalColor, out s_h, out s_s, out s_v); |
|
|
|
|
|
|
|
Color.RGBToHSV(targetColor, out t_h, out t_s, out t_v); |
|
|
|
|
|
|
|
resultColor = Color.HSVToRGB(t_h, t_s, s_v * t_v); |
|
|
|
|
|
|
|
resultColor.a = originalColor.a * targetColor.a; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ColoringMode.Multiply: |
|
|
|
|
|
|
|
resultColor = originalColor * targetColor; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ColoringMode.Replace: |
|
|
|
|
|
|
|
resultColor = targetColor; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ColoringMode.ReplaceKeepAlpha: |
|
|
|
|
|
|
|
resultColor = targetColor; |
|
|
|
|
|
|
|
resultColor.a = originalColor.a; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ColoringMode.Add: |
|
|
|
|
|
|
|
resultColor = originalColor + targetColor; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return Color.Lerp(originalColor, resultColor, lerpAmount); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 解析十六进制颜色字符串并返回对应的 Color 对象 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="hex">十六进制颜色字符串(格式为 #RRGGBB)</param> |
|
|
|
|
|
|
|
/// <returns>解析后的 Color 对象,如果格式错误则返回 Color.clear</returns> |
|
|
|
|
|
|
|
public static Color ParseHexColor(string hex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (hex.Length != 7 || !hex.StartsWith("#")) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Debug.LogError("颜色格式解析错误"); |
|
|
|
|
|
|
|
return Color.clear; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var red = int.Parse(hex[1..3], NumberStyles.HexNumber); |
|
|
|
|
|
|
|
var green = int.Parse(hex[3..5], NumberStyles.HexNumber); |
|
|
|
|
|
|
|
var blue = int.Parse(hex[5..7], NumberStyles.HexNumber); |
|
|
|
|
|
|
|
return new Color(red / 255f, green / 255f, blue / 255f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 将颜色设置为完全透明 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="self">要设置透明的 Color 对象</param> |
|
|
|
|
|
|
|
/// <returns>设置为完全透明的 Color 对象</returns> |
|
|
|
|
|
|
|
public static Color SetTransparent(this Color self) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return self.SetAlpha(0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 将颜色的 alpha 通道值重置为完全不透明 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="self">要重置 alpha 的 Color 对象</param> |
|
|
|
|
|
|
|
/// <returns>设置为完全不透明的 Color 对象</returns> |
|
|
|
|
|
|
|
public static Color ResetTransparent(this Color self) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return self.SetAlpha(1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 设置颜色的 alpha 通道值 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="self">要设置 alpha 的 Color 对象</param> |
|
|
|
|
|
|
|
/// <param name="alpha">新的 alpha 值</param> |
|
|
|
|
|
|
|
/// <returns>设置了新的 alpha 值的 Color 对象</returns> |
|
|
|
|
|
|
|
public static Color SetAlpha(this Color self, float alpha) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new Color(self.r, self.g, self.b, alpha); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |