Skip to content

Commit e749397

Browse files
pekingmehunterstich
authored andcommitted
[Color] Internal color library update.
PiperOrigin-RevId: 655678296
1 parent d7645ae commit e749397

26 files changed

+780
-267
lines changed

lib/java/com/google/android/material/color/utilities/Cam16.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
2020
import static java.lang.Math.max;
2121

22+
import androidx.annotation.NonNull;
2223
import androidx.annotation.RestrictTo;
2324

2425
/**
@@ -75,7 +76,7 @@ public final class Cam16 {
7576
* astar, bstar in code. CAM16-UCS is included in the CAM16 specification, and is used to measure
7677
* distances between colors.
7778
*/
78-
double distance(Cam16 other) {
79+
public double distance(@NonNull Cam16 other) {
7980
double dJ = getJstar() - other.getJstar();
8081
double dA = getAstar() - other.getAstar();
8182
double dB = getBstar() - other.getBstar();

lib/java/com/google/android/material/color/utilities/Contrast.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public final class Contrast {
5959
private static final double CONTRAST_RATIO_EPSILON = 0.04;
6060

6161
// Color spaces that measure luminance, such as Y in XYZ, L* in L*a*b*, or T in HCT, are known as
62-
// perceptual accurate color spaces.
62+
// perceptually accurate color spaces.
6363
//
6464
// To be displayed, they must gamut map to a "display space", one that has a defined limit on the
6565
// number of colors. Display spaces include sRGB, more commonly understood as RGB/HSL/HSV/HSB.
@@ -79,7 +79,7 @@ public final class Contrast {
7979
// result such that the desired contrast ratio will be reached even if inaccuracy is introduced.
8080
//
8181
// 0.4 is generous, ex. HCT requires much less delta. It was chosen because it provides a rough
82-
// guarantee that as long as a percetual color space gamut maps lightness such that the resulting
82+
// guarantee that as long as a perceptual color space gamut maps lightness such that the resulting
8383
// lightness rounds to the same as the requested, the desired contrast ratio will be reached.
8484
private static final double LUMINANCE_GAMUT_MAP_TOLERANCE = 0.4;
8585

lib/java/com/google/android/material/color/utilities/ContrastCurve.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,34 @@
2121
import androidx.annotation.RestrictTo;
2222

2323
/**
24-
* A class containing the contrast curve for a dynamic color on its background.
24+
* A class containing a value that changes with the contrast level.
2525
*
26-
* <p>The four values correspond to contrast requirements for contrast levels -1.0, 0.0, 0.5, and
27-
* 1.0, respectively.
26+
* <p>Usually represents the contrast requirements for a dynamic color on its background. The four
27+
* values correspond to values for contrast levels -1.0, 0.0, 0.5, and 1.0, respectively.
2828
*
2929
* @hide
3030
*/
3131
@RestrictTo(LIBRARY_GROUP)
3232
public final class ContrastCurve {
33-
/** Contrast requirement for contrast level -1.0 */
33+
/** Value for contrast level -1.0 */
3434
private final double low;
3535

36-
/** Contrast requirement for contrast level 0.0 */
36+
/** Value for contrast level 0.0 */
3737
private final double normal;
3838

39-
/** Contrast requirement for contrast level 0.5 */
39+
/** Value for contrast level 0.5 */
4040
private final double medium;
4141

42-
/** Contrast requirement for contrast level 1.0 */
42+
/** Value for contrast level 1.0 */
4343
private final double high;
4444

4545
/**
4646
* Creates a `ContrastCurve` object.
4747
*
48-
* @param low Contrast requirement for contrast level -1.0
49-
* @param normal Contrast requirement for contrast level 0.0
50-
* @param medium Contrast requirement for contrast level 0.5
51-
* @param high Contrast requirement for contrast level 1.0
48+
* @param low Value for contrast level -1.0
49+
* @param normal Value for contrast level 0.0
50+
* @param medium Value for contrast level 0.5
51+
* @param high Value for contrast level 1.0
5252
*/
5353
public ContrastCurve(double low, double normal, double medium, double high) {
5454
this.low = low;
@@ -58,13 +58,13 @@ public ContrastCurve(double low, double normal, double medium, double high) {
5858
}
5959

6060
/**
61-
* Returns the contrast ratio at a given contrast level.
61+
* Returns the value at a given contrast level.
6262
*
6363
* @param contrastLevel The contrast level. 0.0 is the default (normal); -1.0 is the lowest; 1.0
6464
* is the highest.
65-
* @return The contrast ratio, a number between 1.0 and 21.0.
65+
* @return The value. For contrast ratios, a number between 1.0 and 21.0.
6666
*/
67-
public double getContrast(double contrastLevel) {
67+
public double get(double contrastLevel) {
6868
if (contrastLevel <= -1.0) {
6969
return this.low;
7070
} else if (contrastLevel < 0.0) {

lib/java/com/google/android/material/color/utilities/DislikeAnalyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Check and/or fix universally disliked colors.
2525
*
2626
* <p>Color science studies of color preference indicate universal distaste for dark yellow-greens,
27-
* and also show this is correlated to distate for biological waste and rotting food.
27+
* and also show this is correlated to distaste for biological waste and rotting food.
2828
*
2929
* <p>See Palmer and Schloss, 2010 or Schloss and Palmer's Chapter 21 in Handbook of Color
3030
* Psychology (2015).

lib/java/com/google/android/material/color/utilities/DynamicColor.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,36 @@ public DynamicColor(
120120
this.opacity = null;
121121
}
122122

123+
/**
124+
* A constructor for DynamicColor.
125+
*
126+
* <p>_Strongly_ prefer using one of the convenience constructors. This class is arguably too
127+
* flexible to ensure it can support any scenario. Functional arguments allow overriding without
128+
* risks that come with subclasses.
129+
*
130+
* <p>For example, the default behavior of adjust tone at max contrast to be at a 7.0 ratio with
131+
* its background is principled and matches accessibility guidance. That does not mean it's the
132+
* desired approach for _every_ design system, and every color pairing, always, in every case.
133+
*
134+
* <p>For opaque colors (colors with alpha = 100%).
135+
*
136+
* @param name The name of the dynamic color.
137+
* @param palette Function that provides a TonalPalette given DynamicScheme. A TonalPalette is
138+
* defined by a hue and chroma, so this replaces the need to specify hue/chroma. By providing
139+
* a tonal palette, when contrast adjustments are made, intended chroma can be preserved.
140+
* @param tone Function that provides a tone, given a DynamicScheme.
141+
* @param isBackground Whether this dynamic color is a background, with some other color as the
142+
* foreground.
143+
* @param background The background of the dynamic color (as a function of a `DynamicScheme`), if
144+
* it exists.
145+
* @param secondBackground A second background of the dynamic color (as a function of a
146+
* `DynamicScheme`), if it exists.
147+
* @param contrastCurve A `ContrastCurve` object specifying how its contrast against its
148+
* background should behave in various contrast levels options.
149+
* @param toneDeltaPair A `ToneDeltaPair` object specifying a tone delta constraint between two
150+
* colors. One of them must be the color being constructed.
151+
* @param opacity A function returning the opacity of a color, as a number between 0 and 1.
152+
*/
123153
public DynamicColor(
124154
@NonNull String name,
125155
@NonNull Function<DynamicScheme, TonalPalette> palette,
@@ -304,8 +334,8 @@ public double getTone(@NonNull DynamicScheme scheme) {
304334
double expansionDir = scheme.isDark ? 1 : -1;
305335

306336
// 1st round: solve to min, each
307-
double nContrast = nearer.contrastCurve.getContrast(scheme.contrastLevel);
308-
double fContrast = farther.contrastCurve.getContrast(scheme.contrastLevel);
337+
double nContrast = nearer.contrastCurve.get(scheme.contrastLevel);
338+
double fContrast = farther.contrastCurve.get(scheme.contrastLevel);
309339

310340
// If a color is good enough, it is not adjusted.
311341
// Initial and adjusted tones for `nearer`
@@ -385,7 +415,7 @@ public double getTone(@NonNull DynamicScheme scheme) {
385415

386416
double bgTone = background.apply(scheme).getTone(scheme);
387417

388-
double desiredRatio = contrastCurve.getContrast(scheme.contrastLevel);
418+
double desiredRatio = contrastCurve.get(scheme.contrastLevel);
389419

390420
if (Contrast.ratioOfTones(bgTone, answer) >= desiredRatio) {
391421
// Don't "improve" what's good enough.

0 commit comments

Comments
 (0)