compat/src/main/java/androidx/core/graphics/ColorUtils.java
public static int calculateMinimumAlpha(@ColorInt int foreground, @ColorInt int background,
            float minContrastRatio) {
        if (Color.alpha(background) != 255) {
            throw new IllegalArgumentException("background can not be translucent: #"
                    + Integer.toHexString(background));
        }

        // First lets check that a fully opaque foreground has sufficient contrast
        int testForeground = setAlphaComponent(foreground, 255);
        double testRatio = calculateContrast(testForeground, background);
        if (testRatio < minContrastRatio) {
            // Fully opaque foreground does not have sufficient contrast, return error
            return -1;
        }

        // Binary search to find a value with the minimum value which provides sufficient contrast
        int numIterations = 0;
        int minAlpha = 0;
        int maxAlpha = 255;

        while (numIterations <= MIN_ALPHA_SEARCH_MAX_ITERATIONS &&
                (maxAlpha - minAlpha) > MIN_ALPHA_SEARCH_PRECISION) {
            final int testAlpha = (minAlpha + maxAlpha) / 2;

            testForeground = setAlphaComponent(foreground, testAlpha);
            testRatio = calculateContrast(testForeground, background);

            if (testRatio < minContrastRatio) {
                minAlpha = testAlpha;
            } else {
                maxAlpha = testAlpha;
            }

            numIterations++;
        }

        // Conservatively return the max of the range of possible alphas, which is known to pass.
        return maxAlpha;
    }
/**
     * Calculates the minimum alpha value which can be applied to {@code foreground} so that would
     * have a contrast value of at least {@code minContrastRatio} when compared to
     * {@code background}.
     *
     * @param foreground       the foreground color
     * @param background       the opaque background color
     * @param minContrastRatio the minimum contrast ratio
     * @return the alpha value in the range 0-255, or -1 if no value could be calculated
     */
Calculates the minimum alpha value which can be applied to  foreground so that would
have a contrast value of at least  minContrastRatio when compared to
background.

@param foreground       the foreground color
@param background       the opaque background color
@param minContrastRatio the minimum contrast ratio
@return the alpha value in the range 0-255, or -1 if no value could be calculated
Calculates the minimum alpha value which can be applied to foreground so that would have a contrast value of at least minContrastRatio when compared to background . @param foreground the foreground color @param background the opaque background color @param minContrastRatio the minimum contrast ratio @return the alpha value in the range 0 - 255 or - 1 if no value could be calculated
public static int calculateMinimumAlpha(@ColorInt int foreground, @ColorInt int background,
            float minContrastRatio) {
        if (Color.alpha(background) != 255) {
            throw new IllegalArgumentException("background can not be translucent: #"
                    + Integer.toHexString(background));
        }
        int testForeground = setAlphaComponent(foreground, 255);
        double testRatio = calculateContrast(testForeground, background);
        if (testRatio < minContrastRatio) {
            return -1;
        }
        int numIterations = 0;
        int minAlpha = 0;
        int maxAlpha = 255;
        while (numIterations <= MIN_ALPHA_SEARCH_MAX_ITERATIONS &&
                (maxAlpha - minAlpha) > MIN_ALPHA_SEARCH_PRECISION) {
            final int testAlpha = (minAlpha + maxAlpha) / 2;
            testForeground = setAlphaComponent(foreground, testAlpha);
            testRatio = calculateContrast(testForeground, background);
            if (testRatio < minContrastRatio) {
                minAlpha = testAlpha;
            } else {
                maxAlpha = testAlpha;
            }
            numIterations++;
        }
        return maxAlpha;
    }
public static int calculateMinimumAlpha ( @ ColorInt int foreground , @ ColorInt int background , float minContrastRatio ) { if ( Color . alpha ( background ) != 255 ) { throw new IllegalArgumentException ( "background can not be translucent: #" + Integer . toHexString ( background ) ) ; } int testForeground = setAlphaComponent ( foreground , 255 ) ; double testRatio = calculateContrast ( testForeground , background ) ; if ( testRatio < minContrastRatio ) { return - 1 ; } int numIterations = 0 ; int minAlpha = 0 ; int maxAlpha = 255 ; while ( numIterations <= MIN_ALPHA_SEARCH_MAX_ITERATIONS && ( maxAlpha - minAlpha ) > MIN_ALPHA_SEARCH_PRECISION ) { final int testAlpha = ( minAlpha + maxAlpha ) / 2 ; testForeground = setAlphaComponent ( foreground , testAlpha ) ; testRatio = calculateContrast ( testForeground , background ) ; if ( testRatio < minContrastRatio ) { minAlpha = testAlpha ; } else { maxAlpha = testAlpha ; } numIterations ++ ; } return maxAlpha ; }
Calculates the minimum alpha value which can be applied to {@code foreground} so that would
 have a contrast value of at least {@code minContrastRatio} when compared to
 {@code background}.
Calculates the minimum alpha value which can be applied to { @code foreground } so that would have a contrast value of at least { @code minContrastRatio } when compared to { @code background } .
// First lets check that a fully opaque foreground has sufficient contrast // Fully opaque foreground does not have sufficient contrast, return error // Binary search to find a value with the minimum value which provides sufficient contrast // Conservatively return the max of the range of possible alphas, which is known to pass.