public List <Color32> GetColors(int numColors) { while (buckets.Count < numColors) { buckets.Sort((ColorBucket x, ColorBucket y) => (int)Mathf.Sign(y.BiggestVariance - x.BiggestVariance)); ColorBucket bucket = buckets[0]; buckets.RemoveAt(0); List <ColorBucket> splitResult = bucket.MedianCut(); buckets.AddRange(splitResult); if (splitResult.Count < 2) { break; } } List <Color32> colors = new List <Color32>(); foreach (ColorBucket bucket in buckets) { colors.Add(bucket.averageColor); } // Sort by approx luminance (grayscale value) colors.Sort((Color32 x, Color32 y) => (int)Mathf.Sign(((Color)x).grayscale - ((Color)y).grayscale)); return(colors); }
public PaletteTree(ColorBucket bucket, int level, int maxLevels) { this.bucket = bucket; this.level = level; if (level < maxLevels) { children = new List <PaletteTree>(); List <ColorBucket> childBuckets = bucket.MedianCut(); foreach (ColorBucket child in childBuckets) { children.Add(new PaletteTree(child, level + 1, maxLevels)); } } }
public PaletteExtractor(Texture2D texture) { ColorBucket bucket = new ColorBucket(texture.GetPixels32()); buckets.Add(bucket); }
public PaletteExtractor(Texture2D texture) { ColorBucket bucket = new ColorBucket(texture.GetPixels32()); paletteTree = new PaletteTree(bucket, 0, 8); }