Skip to content

Commit

Permalink
[M3][Color] Color component internal update
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 518068669
  • Loading branch information
Material Design Team authored and pekingme committed Mar 20, 2023
1 parent 78fa157 commit 206a468
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions lib/java/com/google/android/material/color/MaterialColors.java
Expand Up @@ -18,6 +18,7 @@
import com.google.android.material.R;

import static android.graphics.Color.TRANSPARENT;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.content.Context;
import android.content.res.ColorStateList;
Expand All @@ -30,6 +31,7 @@
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils;
import com.google.android.material.color.utilities.Blend;
Expand All @@ -54,10 +56,15 @@ public class MaterialColors {
private static final int TONE_ON_ACCENT_LIGHT = 100;
private static final int TONE_ACCENT_CONTAINER_LIGHT = 90;
private static final int TONE_ON_ACCENT_CONTAINER_LIGHT = 10;
private static final int TONE_SURFACE_CONTAINER_LIGHT = 94;
private static final int TONE_SURFACE_CONTAINER_HIGH_LIGHT = 92;
private static final int TONE_ACCENT_DARK = 80;
private static final int TONE_ON_ACCENT_DARK = 20;
private static final int TONE_ACCENT_CONTAINER_DARK = 30;
private static final int TONE_ON_ACCENT_CONTAINER_DARK = 90;
private static final int TONE_SURFACE_CONTAINER_DARK = 12;
private static final int TONE_SURFACE_CONTAINER_HIGH_DARK = 17;
private static final int CHROMA_NEUTRAL = 6;

private MaterialColors() {
// Private constructor to prevent unwanted construction.
Expand Down Expand Up @@ -272,9 +279,7 @@ public static int harmonize(@ColorInt int colorToHarmonize, @ColorInt int colorT
*/
@NonNull
public static ColorRoles getColorRoles(@NonNull Context context, @ColorInt int color) {
return getColorRoles(
color,
MaterialAttributes.resolveBoolean(context, R.attr.isLightTheme, /* defaultValue= */ true));
return getColorRoles(color, isLightTheme(context));
}

/**
Expand All @@ -298,10 +303,59 @@ public static ColorRoles getColorRoles(@ColorInt int color, boolean isLightTheme
getColorRole(color, TONE_ON_ACCENT_CONTAINER_DARK));
}

/**
* Returns the color int of the surface container color role, based on the provided input color.
* This method should be only used internally.
*
* @param context The target context.
* @param seedColor The input color provided for generating surface container color role.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
@ColorInt
public static int getSurfaceContainerFromSeed(@NonNull Context context, @ColorInt int seedColor) {
int tone = isLightTheme(context) ? TONE_SURFACE_CONTAINER_LIGHT : TONE_SURFACE_CONTAINER_DARK;
return getColorRole(seedColor, tone, CHROMA_NEUTRAL);
}

/**
* Returns the color int of the surface container high color role, based on the provided input
* color. This method should be only used internally.
*
* @param context The target context.
* @param seedColor The input color provided for generating surface container high color role.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
@ColorInt
public static int getSurfaceContainerHighFromSeed(
@NonNull Context context, @ColorInt int seedColor) {
int tone =
isLightTheme(context)
? TONE_SURFACE_CONTAINER_HIGH_LIGHT
: TONE_SURFACE_CONTAINER_HIGH_DARK;
return getColorRole(seedColor, tone, CHROMA_NEUTRAL);
}

private static boolean isLightTheme(@NonNull Context context) {
return MaterialAttributes.resolveBoolean(
context, R.attr.isLightTheme, /* defaultValue= */ true);
}

@ColorInt
private static int getColorRole(@ColorInt int color, @IntRange(from = 0, to = 100) int tone) {
Hct hctColor = Hct.fromInt(color);
hctColor.setTone(tone);
return hctColor.toInt();
}

@ColorInt
private static int getColorRole(
@ColorInt int color, @IntRange(from = 0, to = 100) int tone, int chroma) {
Hct hctColor = Hct.fromInt(getColorRole(color, tone));
hctColor.setChroma(chroma);
return hctColor.toInt();
}
}

0 comments on commit 206a468

Please sign in to comment.