public int getDeviceColor(int color) {

		if(isColor && colorCount > 65536 && java.awt.Color.white.equals(ApplicationManager.manager.bgColor))
			return color;

		int r = (color >> 16) & 255;
		int g = (color >> 8) & 255;
		int b = (color & 255);

		if (isColor){
			int scnt = 2;
			int cnt = 8;
			while(cnt < colorCount){
				scnt *= 2;
				cnt *= 8;
			}

			r = discretize(r, scnt);
			g = discretize(g, scnt);
			b = discretize(b, scnt);
		}
		else {
	        double grayscale = (r+g+b) / (3.0 * 255.0); 

   	        grayscale = (int) ((colorCount - 1) * grayscale + 0.5);
       	    grayscale /= (colorCount - 1);

	        java.awt.Color bg = ApplicationManager.manager.bgColor;

	        r = (int) grayscale * bg.getRed();
        	g = (int) grayscale * bg.getGreen();
        	b = (int) grayscale * bg.getBlue();
		}
        return (color & 0x0ff000000) | (r << 16) | (g << 8) | b;
    }