using System; using UnityEngine; class ColorHelper { //reference: http://en.wikipedia.org/wiki/HSL_and_HSV public static Color ColorFromHSV(double _hue, double _saturation, double _value) { int hi = Convert.ToInt32(Math.Floor(_hue / 60)) % 6; double f = _hue / 60 - Math.Floor(_hue / 60); _value = _value * 255; int v = Convert.ToInt32(_value); int p = Convert.ToInt32(_value * (1 - _saturation)); int q = Convert.ToInt32(_value * (1 - f * _saturation)); int t = Convert.ToInt32(_value * (1 - (1 - f) * _saturation)); if (hi == 0) return new Color((float)v / 255.0f, (float)t / 255.0f, (float)p / 255.0f, 1.0f); else if (hi == 1) return new Color((float)q / 255.0f, (float)v / 255.0f, (float)p / 255.0f, 1.0f); else if (hi == 2) return new Color((float)p / 255.0f, (float)v / 255.0f, (float)t / 255.0f, 1.0f); else if (hi == 3) return new Color((float)p / 255.0f, (float)q / 255.0f, (float)v / 255.0f, 1.0f); else if (hi == 4) return new Color((float)t / 255.0f, (float)p / 255.0f, (float)v / 255.0f, 1.0f); else return new Color((float)v / 255.0f, (float)p / 255.0f, (float)q / 255.0f, 1.0f); } }
public Color GetTypeColor() { int hue = 0; if ( TypeHelper .registerTypeHueMap.TryGetValue(paramType, out hue) ) { return ColorHelper .ColorFromHSV(hue, 1.0f, 1.0f); } else { int hashcode = paramType.FullName.GetHashCode(); return ColorHelper .ColorFromHSV(hashcode, 1.0f, 1.0f); } }
using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using NewAge; using System.Reflection; public class DrawColor : MonoBehaviour { bool bShowAllHueColors = true; float top = 0.0f; public static Dictionary<Type, int> registerTypeHueMap = new Dictionary<Type, int>(); float width = 100.0f; void Start() { Debug.Log("Draw Sth."); registerTypeHueMap.Add(typeof(bool), 0); registerTypeHueMap.Add(typeof(int), 120); registerTypeHueMap.Add(typeof(float), 30); registerTypeHueMap.Add(typeof(string), 240); registerTypeHueMap.Add(typeof(Vector2), 50); registerTypeHueMap.Add(typeof(Vector3), 60); registerTypeHueMap.Add(typeof(Vector4), 85); registerTypeHueMap.Add(typeof(Transform), 180); registerTypeHueMap.Add(typeof(Quaternion), 210); registerTypeHueMap.Add(typeof(TransformInfo), 255); registerTypeHueMap.Add(typeof(Shader), 280); registerTypeHueMap.Add(typeof(object), 160); registerTypeHueMap.Add(typeof(Matrix4x4), 10); registerTypeHueMap.Add(typeof(Material), 300); registerTypeHueMap.Add(typeof(GameObject), 190); registerTypeHueMap.Add(typeof(Component), 330); registerTypeHueMap.Add(typeof(AudioClip), 260); registerTypeHueMap.Add(typeof(AnimationClip), 230); } void DrawTypeColor(Type _type) { Color colorToUse = ColorHelper.ColorFromHSV(registerTypeHueMap[_type], 1.0f, 1.0f); GUI.contentColor = colorToUse; Color debugColor = new Color(colorToUse.r * 255.0f, colorToUse.g * 255.0f, colorToUse.b * 255.0f, colorToUse.a * 255.0f); top += 30.0f; GUI.Label(new Rect(20.0f, top, 600.0f, 25.0f), _type.ToString() + " Hue:" + registerTypeHueMap[_type] + " " + debugColor.ToString()); //"show me the color" } void DrawAllTypeColors() { top = 0.0f; DrawTypeColor(typeof(int)); DrawTypeColor(typeof(float)); DrawTypeColor(typeof(bool)); DrawTypeColor(typeof(string)); DrawTypeColor(typeof(Vector2)); DrawTypeColor(typeof(Vector3)); DrawTypeColor(typeof(Vector4)); DrawTypeColor(typeof(Transform)); DrawTypeColor(typeof(Quaternion)); DrawTypeColor(typeof(TransformInfo)); DrawTypeColor(typeof(Shader)); DrawTypeColor(typeof(object)); DrawTypeColor(typeof(Matrix4x4)); DrawTypeColor(typeof(Material)); DrawTypeColor(typeof(GameObject)); DrawTypeColor(typeof(Component)); DrawTypeColor(typeof(AudioClip)); DrawTypeColor(typeof(AnimationClip)); } void DrawHueColor(int _hue, float left, float top) { Color colorToUse = ColorHelper.ColorFromHSV(_hue, 1.0f, 1.0f); GUI.contentColor = colorToUse; Color debugColor = new Color(colorToUse.r * 255.0f, colorToUse.g * 255.0f, colorToUse.b * 255.0f, colorToUse.a * 255.0f); // GUI.Label(new Rect(left, top, 400.0f, 25.0f), "Color " + " Hue:" + _hue + " " + debugColor.ToString()); GUI.Label(new Rect(left, top, width, 25.0f), "Color " + " Hue:" + _hue); } void DrawAllHueColors() { float top = 0.0f; float left = 20.0f; for (int i = 0; i < 360; ++i) { if (top + 25.0f > 730.0f) { top = 0.0f; left += width; } DrawHueColor(i, left, top); top += 25.0f; } } void OnGUI() { if (bShowAllHueColors) { DrawAllHueColors(); } else { DrawAllTypeColors(); } } }
原文地址:http://blog.csdn.net/u010153703/article/details/39228075