compat/src/main/java/androidx/core/graphics/ColorUtils.java
public static double calculateContrast(@ColorInt int foreground, @ColorInt int background) {
        if (Color.alpha(background) != 255) {
            throw new IllegalArgumentException("background can not be translucent: #"
                    + Integer.toHexString(background));
        }
        if (Color.alpha(foreground) < 255) {
            // If the foreground is translucent, composite the foreground over the background
            foreground = compositeColors(foreground, background);
        }

        final double luminance1 = calculateLuminance(foreground) + 0.05;
        final double luminance2 = calculateLuminance(background) + 0.05;

        // Now return the lighter luminance divided by the darker luminance
        return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
    }
/**
     * Returns the contrast ratio between {@code foreground} and {@code background}.
     * {@code background} must be opaque.
     * <p>
     * Formula defined
     * <a href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>.
     */
Returns the contrast ratio between  foreground and  background.
background must be opaque.

Formula defined
here.
Returns the contrast ratio between foreground and background . background must be opaque . Formula defined here .
public static double calculateContrast(@ColorInt int foreground, @ColorInt int background) {
        if (Color.alpha(background) != 255) {
            throw new IllegalArgumentException("background can not be translucent: #"
                    + Integer.toHexString(background));
        }
        if (Color.alpha(foreground) < 255) {
            foreground = compositeColors(foreground, background);
        }
        final double luminance1 = calculateLuminance(foreground) + 0.05;
        final double luminance2 = calculateLuminance(background) + 0.05;
        return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
    }
public static double calculateContrast ( @ ColorInt int foreground , @ ColorInt int background ) { if ( Color . alpha ( background ) != 255 ) { throw new IllegalArgumentException ( "background can not be translucent: #" + Integer . toHexString ( background ) ) ; } if ( Color . alpha ( foreground ) < 255 ) { foreground = compositeColors ( foreground , background ) ; } final double luminance1 = calculateLuminance ( foreground ) + 0.05 ; final double luminance2 = calculateLuminance ( background ) + 0.05 ; return Math . max ( luminance1 , luminance2 ) / Math . min ( luminance1 , luminance2 ) ; }
Returns the contrast ratio between {@code foreground} and {@code background}.
Returns the contrast ratio between { @code foreground } and { @code background } .
// If the foreground is translucent, composite the foreground over the background // Now return the lighter luminance divided by the darker luminance