public static int getColor(@NonNull final Context context, @StyleRes final int themeResourceId,
                               @AttrRes final int resourceId) {
        TypedArray typedArray = null;

        try {
            typedArray = obtainStyledAttributes(context, themeResourceId, resourceId);
            int color = typedArray.getColor(0, -1);

            if (color == -1) {
                int id = typedArray.getResourceId(0, -1);
                color = ContextCompat.getColor(context, id);

                if (color == 0) {
                    throw new NotFoundException(
                            "Resource ID #0x" + Integer.toHexString(resourceId) + " is not valid");
                }
            }

            return color;
        } finally {
            if (typedArray != null) {
                typedArray.recycle();
            }
        }
    }
public static int getColor ( @ NonNull final Context context , @ StyleRes final int themeResourceId , @ AttrRes final int resourceId ) { TypedArray typedArray = null ; try { typedArray = obtainStyledAttributes ( context , themeResourceId , resourceId ) ; int color = typedArray . getColor ( 0 , - 1 ) ; if ( color == - 1 ) { int id = typedArray . getResourceId ( 0 , - 1 ) ; color = ContextCompat . getColor ( context , id ) ; if ( color == 0 ) { throw new NotFoundException ( "Resource ID #0x" + Integer . toHexString ( resourceId ) + " is not valid" ) ; } } return color ; } finally { if ( typedArray != null ) { typedArray . recycle ( ) ; } } }
Obtains the color, which corresponds to a specific resource id, from a specific theme. If the
given resource id is invalid, a {@link NotFoundException} will be thrown.

@param context
The context, which should be used, as an instance of the class {@link Context}. The
context may not be null
@param themeResourceId
The resource id of the theme, the attribute should be obtained from, as an {@link
Integer} value or -1, if the attribute should be obtained from the given context's
theme
@param resourceId
The resource id of the attribute, which should be obtained, as an {@link Integer}
value. The resource id must corresponds to a valid theme attribute
@return The color, which has been obtained, as an {@link Integer} value