private static boolean isSameRegion(int width, int height, int[] inputColors, int x0, int y0, int x, int y, float hueTolerance, float brightnessTolerance) {
        int i0 = x0 + y0 * width;
        int i = x + y * width;
        if (i >= 0 && i < width * height) {
            int inputColor0 = inputColors[i0];
            float red0 = (inputColor0 >> 16) & 0xff;
            float green0 = (inputColor0 >> 8) & 0xff;
            float blue0 = (inputColor0 >> 0) & 0xff;
            float average0 = (red0 + green0 + blue0) / 3;
            float brightness0 = Math.max(Math.max(red0, green0), blue0);
            float redHue0 = red0 / brightness0;
            float greenHue0 = green0 / brightness0;
            float blueHue0 = blue0 / brightness0;

            int inputColor = inputColors[i];
            float red = (inputColor >> 16) & 0xff;
            float green = (inputColor >> 8) & 0xff;
            float blue = (inputColor >> 0) & 0xff;
            float average = (red + green + blue) / 3;
            float brightness = Math.max(Math.max(red, green), blue);
            float redHue = red / brightness;
            float greenHue = green / brightness;
            float blueHue = blue / brightness;

            float hueDelta = (Math.abs(redHue - redHue0) + Math.abs(greenHue - greenHue0) + Math.abs(blueHue - blueHue0)) / 3;
            float brightnessDelta = Math.abs(brightness - brightness0) / 255f;

            if (brightness0 > 10 && brightness > 10) {
                if (hueDelta < hueTolerance && brightnessDelta < brightnessTolerance) {
                    return true;
                } else {
                    return false;
                }
            } else if (brightness0 <= 10 && brightness <= 10) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }