Guest User

ColorPaletteFetcherThing

a guest
Oct 15th, 2014
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. // For List
  4. using System.Collections.Generic;
  5.  
  6. //
  7. // To use, import a texture with the following settings:
  8. // Texture Type = Advanced
  9. // Read/Write Enabled = true
  10. // Filter Mode = Point
  11. // Format = ARGB 32 bit
  12. // Don't forget to press Apply :)
  13. //
  14. // When creating the texture, I tested using a 32 bit png saved from Paint.NET
  15. //
  16. public class ColorFetcherScript : MonoBehaviour {
  17.  
  18.     // The texture we're going to read from
  19.     public Texture2D textureToRead;
  20.     // whether or not we want to prepend Alpha of 1.0 (for paint.net solid colors, yes we do)
  21.     public bool prefixFF = true;
  22.  
  23.     // List of format <hex value (string)>
  24.     private List<string> colorList;
  25.  
  26.     // Use this for initialization
  27.     void Start(){
  28.  
  29.         if(!textureToRead){
  30.             Debug.LogError("Cannot read texture!");
  31.         }
  32.  
  33.         colorList = new List<string>();
  34.  
  35.         getColors();
  36.  
  37.     }
  38.  
  39.     private void getColors(){
  40.  
  41.         Color[] texColors = textureToRead.GetPixels();
  42.         string tempHex = "";
  43.  
  44.         Debug.Log("Reading Colors...");
  45.  
  46.         for(int i = 0; i < texColors.Length; i++){
  47.  
  48.             tempHex = ((Color32)texColors[i]).r.ToString("X2") + ((Color32)texColors[i]).g.ToString("X2") + ((Color32)texColors[i]).b.ToString("X2");
  49.  
  50.             if(!colorList.Contains(tempHex)){
  51.                 //Debug.Log("Found new color: " + tempHex);
  52.                 if(prefixFF){
  53.                     colorList.Add("FF" + tempHex);
  54.                 }else{
  55.                     colorList.Add(tempHex);
  56.                 }
  57.             }
  58.  
  59.         }
  60.  
  61.         Debug.Log("Done! Found " +colorList.Count+ " unique colors.");
  62.         Debug.Log("=======PRINTING LIST========");
  63.  
  64.         string printedList = "";
  65.         foreach(string s in colorList){
  66.             printedList += s + "\n";
  67.         }
  68.         Debug.Log(printedList);
  69.  
  70.     }
  71.  
  72. }
RAW Paste Data