private Texture2D CreatePaletteTexture(List <UInt32> colors, int shades) { Texture2D palette = new Texture2D(colors.Count, shades); palette.filterMode = FilterMode.Point; palette.wrapMode = TextureWrapMode.Clamp; Color32[] data = palette.GetPixels32(); for (int x = 0; x < palette.width; x++) { for (int y = 0; y < palette.height; y++) { data[y * palette.width + x] = Color32Util.FromRGBAUint32(colors[x]); } } palette.SetPixels32(data); palette.Apply(); return(palette); }
public void CalcRange(out Color32 range, out Color32 min, out Color32 max) { min = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); max = new Color32(byte.MinValue, byte.MinValue, byte.MinValue, byte.MinValue); foreach (UInt32 c in colors) { Color32 color = Color32Util.FromRGBAUint32(c); min.r = Math.Min(color.r, min.r); min.g = Math.Min(color.g, min.g); min.b = Math.Min(color.b, min.b); max.r = Math.Max(color.r, max.r); max.g = Math.Max(color.g, max.g); max.b = Math.Max(color.b, max.b); } range = new Color32((byte)(max.r - min.r), (byte)(max.g - min.g), (byte)(max.b - min.b), 0); }
private Dictionary <UInt32, UInt32> GetHistogram(Color32[] data) { Dictionary <UInt32, UInt32> histogram = new Dictionary <UInt32, UInt32>(); int i; UInt32 color; for (i = 0; i < data.Length; i++) { color = data[i].ToRGBAUInt32(); if (histogram.ContainsKey(color)) { histogram[color]++; } else { histogram[color] = 0; } if (i % ProgressUpdateFreq == 0) { if (EditorUtility.DisplayCancelableProgressBar("Converting", "Calculating unique colors", (float)i / (float)data.Length)) { EditorUtility.ClearProgressBar(); return(null); } } } int count = histogram.Count; UInt32[] colors = new UInt32[count]; int[] hues = new int[count]; int[] sats = new int[count]; int[] vals = new int[count]; i = 0; foreach (KeyValuePair <UInt32, UInt32> kv in histogram) { float h, s, v; Color.RGBToHSV(Color32Util.FromRGBAUint32(kv.Key), out h, out s, out v); colors[i] = kv.Key; hues[i] = (int)(h * 255.0f); sats[i] = (int)(s * 255.0f); vals[i] = (int)(v * 255.0f); i++; } for (i = 0; i < count; i++) { int hueDelta = 0; int satDelta = 0; int valDelta = 0; color = colors[i]; int hue = hues[i]; int sat = sats[i]; int val = vals[i]; for (int j = 0; j < count; j++) { hueDelta += Math.Abs(hues[j] - hue); satDelta += Math.Abs(sats[j] - sat); valDelta += Math.Abs(vals[j] - val); } histogram[color] = (UInt32)(hueDelta + valDelta + satDelta); } return(histogram); }