static int closestMatch(int depth, byte red, byte green, byte blue,
			int redMask, int greenMask, int blueMask, byte[] reds,
			byte[] greens, byte[] blues) {
		if (depth > 8) {
			int rshift = 32 - getMSBOffset(redMask);
			int gshift = 32 - getMSBOffset(greenMask);
			int bshift = 32 - getMSBOffset(blueMask);
			return (((red << 24) >>> rshift) & redMask)
					| (((green << 24) >>> gshift) & greenMask)
					| (((blue << 24) >>> bshift) & blueMask);
		}
		int r, g, b;
		int minDistance = 0x7fffffff;
		int nearestPixel = 0;
		int n = reds.length;
		for (int j = 0; j < n; j++) {
			r = (reds[j] & 0xFF) - (red & 0xFF);
			g = (greens[j] & 0xFF) - (green & 0xFF);
			b = (blues[j] & 0xFF) - (blue & 0xFF);
			int distance = r * r + g * g + b * b;
			if (distance < minDistance) {
				nearestPixel = j;
				if (distance == 0)
					break;
				minDistance = distance;
			}
		}
		return nearestPixel;
	}