public PaletteMap(Texture2D sourceTexture, RBPalette basePalette) { this.width = sourceTexture.width; this.height = sourceTexture.height; Color[] sourcePixels = sourceTexture.GetPixels(); pixels = new Color[sourcePixels.Length]; // Remap original colors to point to indeces in the palette for (int i = 0; i < sourcePixels.Length; i++) { // Get the alpha value by looking it up in the paletteKey int paletteIndex = basePalette.IndexOf(sourcePixels [i]); if (paletteIndex < 0) { Vector2 coordinateFromBottomLeft = new Vector2(i % width, i / height); Vector2 coordinateFromTopLeft = new Vector2(coordinateFromBottomLeft.x, height - coordinateFromBottomLeft.y); throw new System.ArgumentException("Encountered color in source PaletteMap image that is not in the base palette." + " Color in PaletteMap: " + (Color32)sourcePixels [i] + " At coordinate: " + coordinateFromTopLeft); } float alpha; if (basePalette.Count == 1) { alpha = 0.0f; } else { alpha = paletteIndex / (float)(basePalette.Count - 1); // For some reason, 1.0f wraps around in the shader. Maybe it's epsilon issues. alpha = Mathf.Clamp(alpha, 0.0f, 0.99f); } pixels [i] = new Color(0.0f, 0.0f, 0.0f, alpha); } }
public RBPaletteDiff DiffWithTexture(Texture2D textureToDiff) { RBPalette paletteFromTexture = RBPalette.CreatePaletteFromTexture(textureToDiff); RBPaletteDiff diff = RBPaletteDiff.Diff(BasePalette, paletteFromTexture); return(diff); }
public void AddPalette() { RBPalette newPalette = new RBPalette(BasePalette); newPalette.PaletteName = "Unnamed"; AddPalette(newPalette); }
public static bool CreatePaletteMapAndKey(string outputPath, Texture2D sourceTexture, RBPaletteGroup suppliedPaletteGroup, bool overwriteExistingFiles, string paletteKeyFileName, string paletteMapFilename) { // If no palette key texture is provided, create a new one from the source image RBPalette basePalette = null; if (suppliedPaletteGroup == null) { basePalette = RBPalette.CreatePaletteFromTexture(sourceTexture); } else { RBPaletteDiff diff = suppliedPaletteGroup.DiffWithTexture(sourceTexture); if (diff.NumChanges > 0) { var result = EditorUtility.DisplayDialogComplex("Palettes Differ", $"Current BasePalette is different from the one that would generate from this texture. {diff.Insertions.Count} colors added and {diff.Deletions.Count} removed." + "Are you sure you want to sync this PaletteGroup to this texture?", "Yes", "No", "Cancel"); if (result == 0) { suppliedPaletteGroup.ApplyDiff(diff); } else if (result == 2) { return(false); } //suppliedPaletteGroup.ApplyDiff (diff); } basePalette = suppliedPaletteGroup.BasePalette; } // Create the palette map from the palette key PaletteMap palettemap; palettemap = new PaletteMap(sourceTexture, basePalette); if (suppliedPaletteGroup == null) { // Write PaletteGroup to disk try { string paletteGroupFilename = paletteKeyFileName + ".asset"; RBPaletteGroup createdGroup = RBPaletteCreator.CreatePaletteGroup(outputPath, paletteGroupFilename, overwriteExistingFiles); createdGroup.GroupName = paletteKeyFileName; createdGroup.SetBasePalette(basePalette); createdGroup.Locked = true; } catch (IOException) { throw new System.IO.IOException("Failed to write PaletteMap. A PaletteGroup already exists by the name: " + paletteKeyFileName); } } // Write PaletteMap to disk string paletteMapFilenameWithExtension = paletteMapFilename + ".png"; string fullPathToPaletteMapFile = outputPath + paletteMapFilenameWithExtension; palettemap.WriteToFile(fullPathToPaletteMapFile, overwriteExistingFiles); return(true); }
public void Initialize(string groupPaletteName = DefaultGroupName, RBPalette basePalette = null) { palettes = new List <RBPalette> (); GroupName = groupPaletteName; if (basePalette == null) { basePalette = new RBPalette("Base Palette"); } palettes.Add(basePalette); }
public static RBPaletteGroup CreateInstanceFromTexture(Texture2D sourceTexture, string groupPaletteName = DefaultGroupName) { // Create a base palette from the Texture RBPalette paletteFromTexture = RBPalette.CreatePaletteFromTexture(sourceTexture); paletteFromTexture.PaletteName = "Base Palette"; // Create the paletteGroup with the base Palette RBPaletteGroup paletteGroup = RBPaletteGroup.CreateInstance(groupPaletteName, paletteFromTexture); return(paletteGroup); }
public static RBPaletteGroup CreatePaletteGroup(string path, string filename, Texture2D sourceTexture, bool overwriteExisting) { ValidateSaveLocation(path + filename, overwriteExisting); // Create a base palette from the Texture RBPalette paletteFromTexture = RBPalette.CreatePaletteFromTexture(sourceTexture); paletteFromTexture.PaletteName = "Base Palette"; // Create the paletteGroup with the base Palette RBPaletteGroup paletteGroup = RBPaletteGroup.CreateInstance(sourceTexture.name + suffix, paletteFromTexture); return((RBPaletteGroup)AssetDatabaseUtility.SaveObject(paletteGroup, path, filename)); }
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 static void CreatePaletteMapAndKey(string outputPath, Texture2D sourceTexture, RBPaletteGroup suppliedPaletteGroup, bool sortPaletteKey, bool overwriteExistingFiles, string paletteKeyFileName, string paletteMapFilename) { // If no palette key texture is provided, create a new one from the source image RBPalette basePalette = null; if (suppliedPaletteGroup == null) { basePalette = RBPalette.CreatePaletteFromTexture(sourceTexture); if (sortPaletteKey) { basePalette.SortByGrayscale(); } } else { // Sync the palette group up with the texture suppliedPaletteGroup.SyncWithTexture(sourceTexture); basePalette = suppliedPaletteGroup.BasePalette; } // Create the palette map from the palette key PaletteMap palettemap; palettemap = new PaletteMap(sourceTexture, basePalette); if (suppliedPaletteGroup == null) { // Write PaletteGroup to disk try { string paletteGroupFilename = paletteKeyFileName + ".asset"; RBPaletteGroup createdGroup = RBPaletteCreator.CreatePaletteGroup(outputPath, paletteGroupFilename, overwriteExistingFiles); createdGroup.GroupName = paletteKeyFileName; createdGroup.SetBasePalette(basePalette); createdGroup.BasePalette.Locked = true; createdGroup.Locked = true; } catch (IOException) { throw new System.IO.IOException("Failed to write PaletteMap. A PaletteGroup already exists by the name: " + paletteKeyFileName); } } // Write PaletteMap to disk string paletteMapFilenameWithExtension = paletteMapFilename + ".png"; string fullPathToPaletteMapFile = outputPath + paletteMapFilenameWithExtension; palettemap.WriteToFile(fullPathToPaletteMapFile, overwriteExistingFiles); }
public static RBPalette CreatePaletteFromTexture(Texture2D sourceTexture) { Color[] sourcePixels = sourceTexture.GetPixels(); RBPalette palette = new RBPalette(); palette.PaletteName = sourceTexture.name; // Get all unique colors for (int i = 0; i < sourcePixels.Length; i++) { Color colorAtSource = ClearRGBIfNoAlpha(sourcePixels [i]); if (!palette.ContainsColor(colorAtSource)) { palette.AddColor(colorAtSource); } } return(palette); }
static RBPalette[] ExtractRBPalettesFromPaletteTexture(Texture2D paletteTexture) { Color[] sourcePixels = paletteTexture.GetPixels(); int numPalettes = paletteTexture.height; int numColors = paletteTexture.width; RBPalette[] palettes = new RBPalette[numPalettes]; for (int i = 0; i < palettes.Length; i++) { palettes [i] = new RBPalette("Palette" + i); // Add all colors to the palette for (int color = 0; color < numColors; color++) { int colorIndex = i * paletteTexture.width + color; palettes [i].AddColor(sourcePixels [colorIndex]); } } return(palettes); }
public static RBPaletteDiff Diff(RBPalette paletteA, RBPalette paletteB) { RBPaletteDiff diffResults = new RBPaletteDiff(); // Find deletions (colors in A that aren't in B) for (int i = 0; i < paletteA.Count; i++) { if (!paletteB.ContainsColor(paletteA[i])) { diffResults.Deletions.Add(paletteA[i]); } } // Find insertions (colors in B that aren't in A) for (int i = 0; i < paletteB.Count; i++) { if (!paletteA.ContainsColor(paletteB[i])) { diffResults.Insertions.Add(paletteB[i]); } } return(diffResults); }
public RBPalette(RBPalette paletteToCopy) { this.PaletteName = paletteToCopy.PaletteName; this.ColorsInPalette = new List <Color> (); this.ColorsInPalette.AddRange(paletteToCopy.ColorsInPalette); }
public void SetBasePalette(RBPalette basePalette) { this.BasePalette = basePalette; this.BasePalette.PaletteName = "Base Palette"; // TODO: Extend or truncate existing palettes }
public static RBPaletteGroup CreateInstance(string groupPaletteName = DefaultGroupName, RBPalette basePalette = null) { RBPaletteGroup paletteGroup = ScriptableObject.CreateInstance <RBPaletteGroup> (); paletteGroup.Initialize(groupPaletteName, basePalette); return(paletteGroup); }
public void AddPalette(RBPalette paletteToAdd) { palettes.Add(paletteToAdd); }