I have 384 RGB values, all already calculated, and I need a function that takes in any given R, G, and B values and returns true if it is one of the known 384 RGB colors, and false otherwise.
Since I couldn't seem to find how to create dictionaries (if they exist) or multidimensional arrays, I eventually managed to get it to work by creating instantiating 3 different arrays, one for R values, G values, and B values, and then looping through all 384 values every time the function was called. It looks like this:
int cR[384] = int[384](254, 254, 165, 254, ... , 233);
int cG[384] = int[384](254, 254, 74, 224, ... , 62);
int cB[384] = int[384](232, 194, 12, 102, ... , 12);
bool cExists(int r, int g, int b)
{
for (int i = 0; i < 384; i++)
{
if (r == cR[i] && g == cG[i] && b == cB[i])
{
return true;
}
}
return false;
}
This works as intended, but is far too slow and is causing problems elsewhere as a result. To try to speed it up, I tried to make a giant array using bit shifting as a pairing function to create unique keys but got stuck here when I found out that I don't even know the syntax to assign a value to an array (the second line errors and I can't for the life of me figure out how to change the values in an array):
bool cRGBHashed[16777216];
cRGBHashed[0] = true;
And here was the hash function I was going to use if you're curious:
int rgbHash(int r, int g, int b)
{
return (r << 16) | (g << 8) | b;
}
I'm certain there's room for significant improvement here somehow, but the combination of my lack of knowledge about the language's bells and whistles combined with the lack of easy to follow documentation (since I honestly don't even know what version of GLSL this program is even running) is making this a real headache.
I would perform linear search on the red channel and if an element was found, I would check the other channels for it is the actual color. Because you might have multiple identical red values you have to still check the "neighbor-numbers" of your red-value, since the identical would be all next to each other, so do some kind of little check-loop in the left and right direction for if one of them would be it.