Skip to content

[Color] Dynamic Contrast for ColorRoles #3524

Open
@patzly

Description

@patzly

Is your feature request related to a problem? Please describe.
In the alphas of 1.11.0 dynamic contrast was introduced and so far it works great! In the updated docs you mention that custom colors should be handled with the non-dynamic contrast API, but in my apps instead of one custom color resource per tone I use a custom color to get the harmonized ColorRoles programmatically:

public ColorRoles getHarmonizedRoles(Context context, @ColorInt int color) {
    return MaterialColors.getColorRoles(
        context, MaterialColors.harmonizeWithPrimary(context, color)
    );
}

I use these ColorRoles for example as Chip tinting for a yellow, harmonized Chip:

int colorYellow = ContextCompat.getColor(activity, R.color.yellow);
ColorRoles roles = ColorUtil.getHarmonizedRoles(activity, colorYellow);
chip.setChipIconTint(ColorStateList.valueOf(roles.getAccent()));
chip.setChipBackgroundColor(ColorStateList.valueOf(roles.getAccentContainer()));
chip.setTextColor(roles.getOnAccentContainer());

But when the contrast of the app changes with the contrast slider in Android 14, the Chip doesn't change its contrast.

Describe the solution you'd like
I think the solution with harmonized ColorRoles is much simpler than defining one color resource per tone, as ColorRoles handles light/dark theming automatically. Without ColorRoles, I need to define a whole range of tones for each custom color to get light/dark theming, color harmonization and dynamic contrast.
It would be amazing if dynamic contrast could be implemented into ColorRoles as well! Maybe in such a way that the output of the getter methods has already the specific contrast applied automatically.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions