public static List <ColorPlus> LoadImagePalette(Color32[] colors) { Color32EqualityComparer comp = new Color32EqualityComparer(); HashSet <Color32> unique_hash = new HashSet <Color32>(colors, comp); List <ColorPlus> transparent = new List <ColorPlus>(); List <ColorPlus> final_list = new List <ColorPlus>(); // cycle through colors and sort out transparent foreach (Color32 hash_val in unique_hash) { if (hash_val.a < ImageUtilities.transparent_threshhold) { transparent.Add(new ColorPlus(hash_val)); } else { final_list.Add(new ColorPlus(hash_val)); } } // shift transparent colors to end of list final_list.AddRange(transparent); return(final_list); }
public static void SetOutputImage() { // ensure we received a valid colors array if (input_palette.Count == 0) { // no image loaded return; } if (output_palette.Count == 0) { // no palette, copy input and end function output_image.texture = Instantiate(input_image.texture) as Texture2D; return; } // get pixel data directly from input texture Color32[] pixels = (input_image.mainTexture as Texture2D).GetPixels32(0); var comp = new Color32EqualityComparer(); Dictionary <Color32, Color32> dict = new Dictionary <Color32, Color32>(comp); foreach (ColorPlus c in input_palette) { dict.Add(c.color32, c.match); } // color replacement using dictionary lookup for (int i = 0; i != pixels.Length; i++) { // perform color match and swap dict.TryGetValue(pixels[i], out pixels[i]); } // apply changes directly to output texture (output_image.mainTexture as Texture2D).SetPixels32(pixels); (output_image.mainTexture as Texture2D).Apply(false); }