public int GetIndex(UInt32 input) { int i = 0; int minDelta = int.MaxValue; int index = 0; foreach (UInt32 color in palette) { int deltaR = Math.Abs(Color32Util.GetR(color) - Color32Util.GetR(input)); int deltaG = Math.Abs(Color32Util.GetG(color) - Color32Util.GetG(input)); int deltaB = Math.Abs(Color32Util.GetB(color) - Color32Util.GetB(input)); int delta = deltaR + deltaG + deltaB; if (delta < minDelta) { minDelta = delta; index = i; } i++; } return(index); }
public void RecalcCentroid() { centroid = Vector3.zero; foreach (UInt32 color in colors) { centroid.x += Color32Util.GetR(color); centroid.y += Color32Util.GetG(color); centroid.z += Color32Util.GetB(color); } centroid.x = centroid.x / (float)colors.Count; centroid.y = centroid.y / (float)colors.Count; centroid.z = centroid.z / (float)colors.Count; }
public static float ColorDistance(UInt32 a, UInt32 b) { Vector3 e1 = new Vector3(Color32Util.GetR(a), Color32Util.GetG(a), Color32Util.GetB(a)); Vector3 e2 = new Vector3(Color32Util.GetR(b), Color32Util.GetG(b), Color32Util.GetB(b)); Vector3 rel = e2 - e1; Vector3 d1 = new Vector3(2, 3, 4) - e1; Vector3 d2 = new Vector3(2, 3, 4) - e2; d1.Normalize(); d2.Normalize(); double ang = 2.0 - Math.Abs(Vector3.Dot(d1, d2)); double length = rel.magnitude; return((float)(length * ang)); }
public int MiddleDelta(UInt32 color) { int count = _colors.Count; if (count == 0) { return(int.MaxValue); } UInt32 middleColor = _colors[count / 2]; int deltaR = Math.Abs(Color32Util.GetR(color) - Color32Util.GetR(middleColor)); int deltaG = Math.Abs(Color32Util.GetG(color) - Color32Util.GetG(middleColor)); int deltaB = Math.Abs(Color32Util.GetB(color) - Color32Util.GetB(middleColor)); return(deltaR + deltaG + deltaB); }
public void SortColors(Color32 range) { byte channel = BiggestChannel(range); if (range.r == channel) { _colors.Sort((a, b) => Color32Util.GetR(a).CompareTo(Color32Util.GetR(b))); } else if (range.g == channel) { _colors.Sort((a, b) => Color32Util.GetG(a).CompareTo(Color32Util.GetG(b))); } else { _colors.Sort((a, b) => Color32Util.GetB(a).CompareTo(Color32Util.GetB(b))); } }
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); }