public void createInverseMap(int no_of_colors, int[] colorPalette) {   
    		int red, green, blue, r, g, b;
            int rdist, gdist, bdist, dist;
            int rinc, ginc, binc;

    		int x = (1 << bitsDiscarded);// Step size for each color
    		int xsqr = (1 << (bitsDiscarded + bitsDiscarded));
            int txsqr = xsqr + xsqr;
    		int buf_index;

            int[] dist_buf = new int[invMapLen];
    		
    		// Initialize the distance buffer array with the largest integer value
    		for (int i = invMapLen; --i >= 0;)
    			dist_buf[i] = 0x7FFFFFFF;
            // Now loop through all the colors in the color map
    		for (int i = 0; i < no_of_colors; i++)
    		{
    			red   = ((colorPalette[i]>>16)&0xff);
    			green = ((colorPalette[i]>>8)&0xff);
    			blue  = (colorPalette[i]&0xff);
    			/**
    			 * We start from the origin (0,0,0) of the quantized colors, calculate
    			 * the distance between the cell center of the quantized colors and
    			 * the current color map entry as follows:
    			 * (rcenter * x + x/2) - red, where rcenter is the center of the 
    			 * Quantized red color map entry which is 0 since we start from 0.
    			 */
    	        rdist = (x>>1) - red;// Red distance
    	        gdist = (x>>1) - green;// Green distance
    	        bdist = (x>>1) - blue;// Blue distance
    	        dist = rdist*rdist + gdist*gdist + bdist*bdist;//The modular
                // The distance increment with each step value x
    	        rinc = txsqr - (red   << (bitsDiscarded + 1));
    	        ginc = txsqr - (green << (bitsDiscarded + 1));
                binc = txsqr - (blue  << (bitsDiscarded + 1));

    			buf_index = 0;
    			// Loop through quantized RGB space
    			for (r = 0, rdist = dist; r < maxColorVal; rdist += rinc, rinc += txsqr, r++ )
    			{
    				for (g = 0, gdist = rdist; g < maxColorVal; gdist += ginc, ginc += txsqr, g++)
    				{
    					for (b = 0, bdist = gdist; b < maxColorVal; bdist += binc, binc += txsqr, buf_index++, b++)
    					{
    						if (bdist < dist_buf[buf_index])
    						{
    							dist_buf[buf_index] = bdist;
    							invColorMap[buf_index] = (byte)i;
    						}
    					}
    				}
    			}
    		}
    	}
Create an inverse color map using the input forward RGB map.
subs/gif-revert/src/main/java/com/engineer/gif/revert/lib/AnimatedGIFWriter.java