Пример #1
0
        public static List <Color32> ExtractPalette(string validImagePath, int numberOfColors)
        {
            byte[]    fileData = System.IO.File.ReadAllBytes(validImagePath);
            Texture2D tex      = new Texture2D(2, 2);

            tex.LoadImage(fileData);

            int scaledSize = 128;

            RenderTexture scaled = RenderTexture.GetTemporary(scaledSize, scaledSize);

            Graphics.Blit(tex, scaled);
            Texture2D smalltex = new Texture2D(scaledSize, scaledSize);

            smalltex.ReadPixels(new Rect(0, 0, scaledSize, scaledSize), 0, 0);
            RenderTexture.ReleaseTemporary(scaled);

            PaletteExtractor extractor        = new PaletteExtractor(smalltex);
            List <Color32>   extractedPalette = extractor.GetColors(numberOfColors);

            return(extractedPalette);
        }
Пример #2
0
        void DrawExtractPaletteMedianCut()
        {
            if (GUILayout.Button("Extract Palette From Image", GUILayout.Width(200), GUILayout.Height(28)))
            {
                if (paletteImagePath == null)
                {
                    paletteImagePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
                }

                string imagePath = EditorUtility.OpenFilePanelWithFilters("Select Image File", paletteImagePath, new string[]
                {
                    "JPG Image", "jpg",
                    "PNG Image", "png",
                });

                if (imagePath.Length > 0)
                {
                    paletteImagePath = imagePath;
                    List <Color32> extractedPalette = PaletteExtractor.ExtractPalette(paletteImagePath, _target.numberOfColors);
                    _target.SetColors(extractedPalette);
                    dirty.forceDirty = true;
                }
            }
        }