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))); } }