public void SyncWithTexture(Texture2D sourceTexture) { Color[] sourcePixels = sourceTexture.GetPixels(); // Unlock the PaletteGroup so that we can edit it. bool wasLocked = Locked; Locked = false; bool wasPaletteLocked = BasePalette.Locked; BasePalette.Locked = false; // Add new colors from the texture into the Palette List <Color> seenColors = new List <Color>(); for (int i = 0; i < sourcePixels.Length; i++) { Color colorAtSource = RBPalette.ClearRGBIfNoAlpha(sourcePixels [i]); int index = BasePalette.IndexOf(colorAtSource); bool colorNotFound = index < 0; if (colorNotFound) { AddColor(); BasePalette [BasePalette.Count - 1] = colorAtSource; // Note this assumes color is added to the end... } else { // Add unique seen colors to list of seen colors if (!seenColors.Contains(colorAtSource)) { seenColors.Add(colorAtSource); } } } // Remove unused colors, back to front to avoid shifting indeces for (int i = BasePalette.Count - 1; i >= 0; i--) { bool colorWasSeen = seenColors.Contains(BasePalette[i]); if (!colorWasSeen) { RemoveColorAtIndex(i); } } // Relock the palette group Locked = wasLocked; BasePalette.Locked = wasPaletteLocked; }
public void ApplyDiff(RBPaletteDiff diff) { // Unlock the PaletteGroup so that we can edit it. bool wasLocked = Locked; Locked = false; // Add new colors to the palette for (int i = 0; i < diff.Insertions.Count; i++) { AddColor(diff.Insertions [i]); } // Remove unused colors for (int i = 0; i < diff.Deletions.Count; i++) { int unusedColorIndex = BasePalette.IndexOf(diff.Deletions [i]); RemoveColorAtIndex(unusedColorIndex); } // Relock the palette group Locked = wasLocked; }