Home > Mobile >  How to find all the colors in a Texture2D, and saving them to an array
How to find all the colors in a Texture2D, and saving them to an array

Time:04-24

Sorry about the title, I wasn't entirely sure how to phrase it, but basically I am trying to increase an index based on how many colors there are in a given Texture2D. To do this I have written

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class ProvinceMapGenerator : MonoBehaviour
{

    public Texture2D provinceMap;

    Color previousColor;
    Color[] provinceColors = { new Color(0, 0, 0) };

    int provinceIndex;

    private void Start()
    {
        provinceColors = new Color[100];

        GenerateProvinces();
    }

    public void GenerateProvinces()
    {
        // Check all pixels for different colors, and save each color
        // Potential Bug, saying 54 when there is at least 59 colors...
        for (int x = 0; x < provinceMap.width; x  )
        {
            for (int y = 0; y < provinceMap.height; y  )
            {
                // If the current pixel isn't the previous pixel, and the pixels aren't the same, increase the index
                if (provinceMap.GetPixel(x, y) != previousColor && !provinceColors.Contains(provinceMap.GetPixel(x, y)))
                {
                    // Save the color so that its not picked again
                    provinceColors[provinceIndex] = provinceMap.GetPixel(x, y);

                    provinceIndex  ;
                    previousColor = provinceMap.GetPixel(x, y);

                    Debug.Log("Different Colors/Province Index: "   provinceIndex);
                }
            }
        }
    }

}

and this...somewhat works. It will count up to 54 which originally I thought was the answer, but in this case its actually 59. I'm not sure why its not.

In the if I try to see if the pixel is different, but not the same as other pixels, and if that is true, then save the new color to the provinceColor array, increase the index and set the previous color equal to the color that was just picked, and log the index. Again this prints 54, when the answer is 49. As for the Texture2D I am using, the Texture2D Texture2D settings

CodePudding user response:

Instead of using that whole logic about storing colors and what not, simply switch the type of the "provinceColors" to a HashSet and add all the pixels to the hashset. You can do so by getting all pixels at once and passing it to the constructor of the hashset.

A HashSet will compare any new element you add to the existing ones and if it already exists it won't be added again so every element is unique.

Code:

public class ProvinceMapGenerator : MonoBehaviour
{
    public Texture2D provinceMap;
    private HashSet<Color> _provinceColors;

    private void Start()
    {
        GenerateProvinces();
    }

    private void GenerateProvinces()
    {
        Color[] pixels = provinceMap.GetPixels();
        _provinceColors = new HashSet<Color>(pixels);
        Debug.Log($"Color count: {_provinceColors.Count}");
    }
}

CodePudding user response:

Sven Viking solved this, and its not because of any error with the code, rather me just being dumb and apparently not knowing math. Anyway, thanks for the three of yall for trying to help me!

Edit: Ill set this to the answer when i can if I rememeber

  • Related