Skip to content

Commit 0a56fc7

Browse files
Material Engcopybara-github
authored andcommitted
Implement New Contrast Surfaces Spec.
PiperOrigin-RevId: 585959265
1 parent f16ff28 commit 0a56fc7

23 files changed

+400
-304
lines changed

cpp/dynamiccolor/contrast_curve.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
namespace material_color_utilities {
2323

2424
/**
25-
* Documents a constraint between two DynamicColors, in which their tones must
26-
* have a certain distance from each other.
25+
* A class containing a value that changes with the contrast level.
26+
*
27+
* Usually represents the contrast requirements for a dynamic color on its
28+
* background. The four values correspond to values for contrast levels -1.0,
29+
* 0.0, 0.5, and 1.0, respectively.
2730
*/
2831
struct ContrastCurve {
2932
double low;
@@ -34,22 +37,22 @@ struct ContrastCurve {
3437
/**
3538
* Creates a `ContrastCurve` object.
3639
*
37-
* @param low Contrast requirement for contrast level -1.0
38-
* @param normal Contrast requirement for contrast level 0.0
39-
* @param medium Contrast requirement for contrast level 0.5
40-
* @param high Contrast requirement for contrast level 1.0
40+
* @param low Value for contrast level -1.0
41+
* @param normal Value for contrast level 0.0
42+
* @param medium Value for contrast level 0.5
43+
* @param high Value for contrast level 1.0
4144
*/
4245
ContrastCurve(double low, double normal, double medium, double high)
4346
: low(low), normal(normal), medium(medium), high(high) {}
4447

4548
/**
46-
* Returns the contrast ratio at a given contrast level.
49+
* Returns the value at a given contrast level.
4750
*
48-
* @param contrastLevel The contrast level. 0.0 is the default (normal);
49-
* -1.0 is the lowest; 1.0 is the highest.
50-
* @return The contrast ratio, a number between 1.0 and 21.0.
51+
* @param contrastLevel The contrast level. 0.0 is the default (normal); -1.0
52+
* is the lowest; 1.0 is the highest.
53+
* @return The value. For contrast ratios, a number between 1.0 and 21.0.
5154
*/
52-
double getContrast(double contrastLevel) {
55+
double get(double contrastLevel) {
5356
if (contrastLevel <= -1.0) {
5457
return low;
5558
} else if (contrastLevel < 0.0) {

cpp/dynamiccolor/dynamic_color.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ double DynamicColor::GetTone(const DynamicScheme& scheme) {
154154

155155
// 1st round: solve to min, each
156156
double n_contrast =
157-
nearer.contrast_curve_.value().getContrast(scheme.contrast_level);
157+
nearer.contrast_curve_.value().get(scheme.contrast_level);
158158
double f_contrast =
159-
farther.contrast_curve_.value().getContrast(scheme.contrast_level);
159+
farther.contrast_curve_.value().get(scheme.contrast_level);
160160

161161
// If a color is good enough, it is not adjusted.
162162
// Initial and adjusted tones for `nearer`
@@ -234,8 +234,7 @@ double DynamicColor::GetTone(const DynamicScheme& scheme) {
234234

235235
double bg_tone = background_.value()(scheme).GetTone(scheme);
236236

237-
double desired_ratio =
238-
contrast_curve_.value().getContrast(scheme.contrast_level);
237+
double desired_ratio = contrast_curve_.value().get(scheme.contrast_level);
239238

240239
if (RatioOfTones(bg_tone, answer) >= desired_ratio) {
241240
// Don't "improve" what's good enough.

cpp/dynamiccolor/dynamic_color_test.cc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ TEST(DynamicColorTest, One) {
5050
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Surface().GetArgb(s)),
5151
0xfffff8f6);
5252
EXPECT_EQ((unsigned int)(MaterialDynamicColors::SurfaceDim().GetArgb(s)),
53-
0xfff0d4cf);
53+
0xffdcc0bc);
5454
EXPECT_EQ((unsigned int)(MaterialDynamicColors::SurfaceBright().GetArgb(s)),
5555
0xfffff8f6);
5656
EXPECT_EQ(
@@ -62,74 +62,74 @@ TEST(DynamicColorTest, One) {
6262
0xfffff0ee);
6363
EXPECT_EQ(
6464
(unsigned int)(MaterialDynamicColors::SurfaceContainer().GetArgb(s)),
65-
0xffffe9e6);
65+
0xffffe2dd);
6666
EXPECT_EQ(
6767
(unsigned int)(MaterialDynamicColors::SurfaceContainerHigh().GetArgb(s)),
68-
0xffffe2dd);
68+
0xfff3d7d2);
6969
EXPECT_EQ(
7070
(unsigned int)(MaterialDynamicColors::SurfaceContainerHighest().GetArgb(
7171
s)),
72-
0xfff9dcd8);
72+
0xffe7cbc7);
7373
EXPECT_EQ((unsigned int)(MaterialDynamicColors::OnSurface().GetArgb(s)),
74-
0xff271815);
74+
0xff1b0e0b);
7575
EXPECT_EQ((unsigned int)(MaterialDynamicColors::SurfaceVariant().GetArgb(s)),
7676
0xfffddbd5);
7777
EXPECT_EQ(
7878
(unsigned int)(MaterialDynamicColors::OnSurfaceVariant().GetArgb(s)),
79-
0xff543d3a);
79+
0xff46312e);
8080
EXPECT_EQ((unsigned int)(MaterialDynamicColors::InverseSurface().GetArgb(s)),
8181
0xff3d2c29);
8282
EXPECT_EQ(
8383
(unsigned int)(MaterialDynamicColors::InverseOnSurface().GetArgb(s)),
8484
0xffffedea);
8585
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Outline().GetArgb(s)),
86-
0xff725955);
86+
0xff654d49);
8787
EXPECT_EQ((unsigned int)(MaterialDynamicColors::OutlineVariant().GetArgb(s)),
88-
0xff907470);
88+
0xff816763);
8989
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Shadow().GetArgb(s)),
9090
0xff000000);
9191
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Scrim().GetArgb(s)),
9292
0xff000000);
9393
EXPECT_EQ((unsigned int)(MaterialDynamicColors::SurfaceTint().GetArgb(s)),
9494
0xffc00100);
9595
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Primary().GetArgb(s)),
96-
0xff8c0100);
96+
0xff740100);
9797
EXPECT_EQ((unsigned int)(MaterialDynamicColors::OnPrimary().GetArgb(s)),
9898
0xffffffff);
9999
EXPECT_EQ(
100100
(unsigned int)(MaterialDynamicColors::PrimaryContainer().GetArgb(s)),
101-
0xffeb0000);
101+
0xffdc0100);
102102
EXPECT_EQ(
103103
(unsigned int)(MaterialDynamicColors::OnPrimaryContainer().GetArgb(s)),
104104
0xffffffff);
105105
EXPECT_EQ((unsigned int)(MaterialDynamicColors::InversePrimary().GetArgb(s)),
106106
0xffffb4a8);
107107
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Secondary().GetArgb(s)),
108-
0xff603924);
108+
0xff522d19);
109109
EXPECT_EQ((unsigned int)(MaterialDynamicColors::OnSecondary().GetArgb(s)),
110110
0xffffffff);
111111
EXPECT_EQ(
112112
(unsigned int)(MaterialDynamicColors::SecondaryContainer().GetArgb(s)),
113-
0xff996952);
113+
0xff91624b);
114114
EXPECT_EQ(
115115
(unsigned int)(MaterialDynamicColors::OnSecondaryContainer().GetArgb(s)),
116116
0xffffffff);
117117
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Tertiary().GetArgb(s)),
118-
0xff633909);
118+
0xff532d00);
119119
EXPECT_EQ((unsigned int)(MaterialDynamicColors::OnTertiary().GetArgb(s)),
120120
0xffffffff);
121121
EXPECT_EQ(
122122
(unsigned int)(MaterialDynamicColors::TertiaryContainer().GetArgb(s)),
123-
0xff9d6937);
123+
0xff946230);
124124
EXPECT_EQ(
125125
(unsigned int)(MaterialDynamicColors::OnTertiaryContainer().GetArgb(s)),
126126
0xffffffff);
127127
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Error().GetArgb(s)),
128-
0xff8c0009);
128+
0xff740006);
129129
EXPECT_EQ((unsigned int)(MaterialDynamicColors::OnError().GetArgb(s)),
130130
0xffffffff);
131131
EXPECT_EQ((unsigned int)(MaterialDynamicColors::ErrorContainer().GetArgb(s)),
132-
0xffda342e);
132+
0xffcf2c27);
133133
EXPECT_EQ(
134134
(unsigned int)(MaterialDynamicColors::OnErrorContainer().GetArgb(s)),
135135
0xffffffff);

0 commit comments

Comments
 (0)