I have a BufferedImage using an IndexColorModel to display some graphics data which uses a palette. I then draw to the buffer using createGraphics(), and I want to turn this data back into an array of indicies. However, all methods I can find get me RGB values, not the actual index values. This seems a bit silly, since while I could just iterate through my palette and find the proper color, it’d be much simpler to just grab the value directly.
Is this even possible without finding it manually?
If you can’t modify/override
createGraphics()method (because belonging to an external JAR library with no sources), you effectively have to find your object manually.Otherwise, if you have access to
createGraphics()source code, two choices:If some method clients need indexes as returned object whereas other clients need RGB values as returned, you could build a
HashMap<Integer, RGB>as return. Integer would be the index and RGB either an Object itself or a primitive value like a String.If you effectively have found a way to access method’s source code and confirms that index is the essential required data for your process, simply returns an Integer => the index, so that you can grab your targeted objects at O(1).