Skip to content

Commit 182a924

Browse files
Material Engcopybara-github
authored andcommitted
Add key colors for palettes and latest scheme changes.
PiperOrigin-RevId: 538195437
1 parent 0207b59 commit 182a924

File tree

70 files changed

+4187
-2029
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4187
-2029
lines changed

cpp/dynamiccolor/dynamic_color_test.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ TEST(DynamicColorTest, One) {
4646
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Background().GetArgb(s)),
4747
0xfffff8f6);
4848
EXPECT_EQ((unsigned int)(MaterialDynamicColors::OnBackground().GetArgb(s)),
49-
0xff241816);
49+
0xff261715);
5050
EXPECT_EQ((unsigned int)(MaterialDynamicColors::Surface().GetArgb(s)),
5151
0xfffff8f6);
5252
EXPECT_EQ((unsigned int)(MaterialDynamicColors::SurfaceDim().GetArgb(s)),
53-
0xffecd5d1);
53+
0xfff0d4cf);
5454
EXPECT_EQ((unsigned int)(MaterialDynamicColors::SurfaceBright().GetArgb(s)),
5555
0xfffff8f6);
5656
EXPECT_EQ(
@@ -65,20 +65,20 @@ TEST(DynamicColorTest, One) {
6565
0xffffe9e6);
6666
EXPECT_EQ(
6767
(unsigned int)(MaterialDynamicColors::SurfaceContainerHigh().GetArgb(s)),
68-
0xfffbe3df);
68+
0xffffe2dd);
6969
EXPECT_EQ(
7070
(unsigned int)(MaterialDynamicColors::SurfaceContainerHighest().GetArgb(
7171
s)),
72-
0xfff5ddda);
72+
0xfff9dcd8);
7373
EXPECT_EQ((unsigned int)(MaterialDynamicColors::OnSurface().GetArgb(s)),
74-
0xff241816);
74+
0xff261715);
7575
EXPECT_EQ((unsigned int)(MaterialDynamicColors::SurfaceVariant().GetArgb(s)),
7676
0xfffddbd5);
7777
EXPECT_EQ(
7878
(unsigned int)(MaterialDynamicColors::OnSurfaceVariant().GetArgb(s)),
7979
0xff58413d);
8080
EXPECT_EQ((unsigned int)(MaterialDynamicColors::InverseSurface().GetArgb(s)),
81-
0xff3b2d2b);
81+
0xff3d2c29);
8282
EXPECT_EQ(
8383
(unsigned int)(MaterialDynamicColors::InverseOnSurface().GetArgb(s)),
8484
0xffffedeb);

cpp/dynamiccolor/material_dynamic_colors.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,66 @@ DynamicColor highestSurface(const DynamicScheme& s) {
164164
: MaterialDynamicColors::SurfaceDim();
165165
}
166166

167+
DynamicColor MaterialDynamicColors::PrimaryPaletteKeyColor() {
168+
return DynamicColor::FromPalette(
169+
/*palette*/ [](const DynamicScheme& s)
170+
-> TonalPalette { return s.primary_palette; },
171+
/*tone*/
172+
[](const DynamicScheme& s) -> double {
173+
return s.primary_palette.get_key_color().get_tone();
174+
},
175+
/*toneDeltaConstraint*/ std::nullopt,
176+
/*background*/ std::nullopt);
177+
}
178+
179+
DynamicColor MaterialDynamicColors::SecondaryPaletteKeyColor() {
180+
return DynamicColor::FromPalette(
181+
/*palette*/ [](const DynamicScheme& s)
182+
-> TonalPalette { return s.secondary_palette; },
183+
/*tone*/
184+
[](const DynamicScheme& s) -> double {
185+
return s.secondary_palette.get_key_color().get_tone();
186+
},
187+
/*toneDeltaConstraint*/ std::nullopt,
188+
/*background*/ std::nullopt);
189+
}
190+
191+
DynamicColor MaterialDynamicColors::TertiaryPaletteKeyColor() {
192+
return DynamicColor::FromPalette(
193+
/*palette*/ [](const DynamicScheme& s)
194+
-> TonalPalette { return s.tertiary_palette; },
195+
/*tone*/
196+
[](const DynamicScheme& s) -> double {
197+
return s.tertiary_palette.get_key_color().get_tone();
198+
},
199+
/*toneDeltaConstraint*/ std::nullopt,
200+
/*background*/ std::nullopt);
201+
}
202+
203+
DynamicColor MaterialDynamicColors::NeutralPaletteKeyColor() {
204+
return DynamicColor::FromPalette(
205+
/*palette*/ [](const DynamicScheme& s)
206+
-> TonalPalette { return s.neutral_palette; },
207+
/*tone*/
208+
[](const DynamicScheme& s) -> double {
209+
return s.neutral_palette.get_key_color().get_tone();
210+
},
211+
/*toneDeltaConstraint*/ std::nullopt,
212+
/*background*/ std::nullopt);
213+
}
214+
215+
DynamicColor MaterialDynamicColors::NeutralVariantPaletteKeyColor() {
216+
return DynamicColor::FromPalette(
217+
/*palette*/ [](const DynamicScheme& s)
218+
-> TonalPalette { return s.neutral_variant_palette; },
219+
/*tone*/
220+
[](const DynamicScheme& s) -> double {
221+
return s.neutral_variant_palette.get_key_color().get_tone();
222+
},
223+
/*toneDeltaConstraint*/ std::nullopt,
224+
/*background*/ std::nullopt);
225+
}
226+
167227
DynamicColor MaterialDynamicColors::Background() {
168228
return DynamicColor::FromPalette(
169229
/*palette*/ [](const DynamicScheme& s)

cpp/dynamiccolor/material_dynamic_colors.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace material_color_utilities {
2323

2424
class MaterialDynamicColors {
2525
public:
26+
static DynamicColor PrimaryPaletteKeyColor();
27+
static DynamicColor SecondaryPaletteKeyColor();
28+
static DynamicColor TertiaryPaletteKeyColor();
29+
static DynamicColor NeutralPaletteKeyColor();
30+
static DynamicColor NeutralVariantPaletteKeyColor();
2631
static DynamicColor Background();
2732
static DynamicColor OnBackground();
2833
static DynamicColor Surface();

cpp/palettes/tones.cc

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,34 @@
1616

1717
#include "cpp/palettes/tones.h"
1818

19-
#include <cstdlib>
20-
2119
#include "cpp/cam/cam.h"
20+
#include "cpp/cam/hct.h"
2221

2322
namespace material_color_utilities {
2423

25-
TonalPalette::TonalPalette(Argb argb) {
24+
TonalPalette::TonalPalette(Argb argb) : key_color_(0.0, 0.0, 0.0) {
2625
Cam cam = CamFromInt(argb);
2726
hue_ = cam.hue;
2827
chroma_ = cam.chroma;
28+
key_color_ = createKeyColor(cam.hue, cam.chroma);
29+
}
30+
31+
TonalPalette::TonalPalette(Hct hct)
32+
: key_color_(hct.get_hue(), hct.get_chroma(), hct.get_tone()) {
33+
hue_ = hct.get_hue();
34+
chroma_ = hct.get_chroma();
2935
}
3036

31-
TonalPalette::TonalPalette(double hue, double chroma) {
37+
TonalPalette::TonalPalette(double hue, double chroma)
38+
: key_color_(hue, chroma, 0.0) {
39+
hue_ = hue;
40+
chroma_ = chroma;
41+
key_color_ = createKeyColor(hue, chroma);
42+
}
43+
44+
TonalPalette::TonalPalette(double hue, double chroma, Hct key_color)
45+
: key_color_(key_color.get_hue(), key_color.get_chroma(),
46+
key_color.get_tone()) {
3247
hue_ = hue;
3348
chroma_ = chroma;
3449
}
@@ -37,4 +52,38 @@ Argb TonalPalette::get(double tone) const {
3752
return IntFromHcl(hue_, chroma_, tone);
3853
}
3954

55+
Hct TonalPalette::createKeyColor(double hue, double chroma) {
56+
double start_tone = 50.0;
57+
Hct smallest_delta_hct(hue, chroma, start_tone);
58+
double smallest_delta = abs(smallest_delta_hct.get_chroma() - chroma);
59+
// Starting from T50, check T+/-delta to see if they match the requested
60+
// chroma.
61+
//
62+
// Starts from T50 because T50 has the most chroma available, on
63+
// average. Thus it is most likely to have a direct answer and minimize
64+
// iteration.
65+
for (double delta = 1.0; delta < 50.0; delta += 1.0) {
66+
// Termination condition rounding instead of minimizing delta to avoid
67+
// case where requested chroma is 16.51, and the closest chroma is 16.49.
68+
// Error is minimized, but when rounded and displayed, requested chroma
69+
// is 17, key color's chroma is 16.
70+
if (round(chroma) == round(smallest_delta_hct.get_chroma())) {
71+
return smallest_delta_hct;
72+
}
73+
Hct hct_add(hue, chroma, start_tone + delta);
74+
double hct_add_delta = abs(hct_add.get_chroma() - chroma);
75+
if (hct_add_delta < smallest_delta) {
76+
smallest_delta = hct_add_delta;
77+
smallest_delta_hct = hct_add;
78+
}
79+
Hct hct_subtract(hue, chroma, start_tone - delta);
80+
double hct_subtract_delta = abs(hct_subtract.get_chroma() - chroma);
81+
if (hct_subtract_delta < smallest_delta) {
82+
smallest_delta = hct_subtract_delta;
83+
smallest_delta_hct = hct_subtract;
84+
}
85+
}
86+
return smallest_delta_hct;
87+
}
88+
4089
} // namespace material_color_utilities

cpp/palettes/tones.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
#ifndef CPP_PALETTES_TONES_H_
1818
#define CPP_PALETTES_TONES_H_
1919

20+
#include "cpp/cam/hct.h"
2021
#include "cpp/utils/utils.h"
2122

2223
namespace material_color_utilities {
2324

2425
class TonalPalette {
2526
public:
2627
explicit TonalPalette(Argb argb);
28+
TonalPalette(Hct hct);
2729
TonalPalette(double hue, double chroma);
30+
TonalPalette(double hue, double chroma, Hct key_color);
2831

2932
/**
3033
* Returns the color for a given tone in this palette.
@@ -36,10 +39,14 @@ class TonalPalette {
3639

3740
double get_hue() const { return hue_; }
3841
double get_chroma() const { return chroma_; }
42+
Hct get_key_color() const { return key_color_; }
3943

4044
private:
4145
double hue_;
4246
double chroma_;
47+
Hct key_color_;
48+
49+
Hct createKeyColor(double hue, double chroma);
4350
};
4451

4552
} // namespace material_color_utilities

cpp/scheme/scheme_expressive.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cpp/scheme/scheme_expressive.h"
18+
19+
#include "cpp/cam/hct.h"
20+
#include "cpp/palettes/tones.h"
21+
#include "cpp/scheme/dynamic_scheme.h"
22+
#include "cpp/scheme/variant.h"
23+
24+
namespace material_color_utilities {
25+
26+
const std::vector<double> kHues = {0, 21, 51, 121, 151, 191, 271, 321, 360};
27+
28+
const std::vector<double> kSecondaryRotations = {45, 95, 45, 20, 45,
29+
90, 45, 45, 45};
30+
31+
const std::vector<double> kTertiaryRotations = {120, 120, 20, 45, 20,
32+
15, 20, 120, 120};
33+
34+
SchemeExpressive::SchemeExpressive(Hct set_source_color_hct, bool set_is_dark,
35+
double set_contrast_level)
36+
: DynamicScheme(
37+
/*source_color_argb:*/ set_source_color_hct.ToInt(),
38+
/*variant:*/ Variant::kExpressive,
39+
/*contrast_level:*/ set_contrast_level,
40+
/*is_dark:*/ set_is_dark,
41+
/*primary_palette:*/
42+
TonalPalette(set_source_color_hct.get_hue() + 240.0, 40.0),
43+
/*secondary_palette:*/
44+
TonalPalette(DynamicScheme::GetRotatedHue(set_source_color_hct, kHues,
45+
kSecondaryRotations),
46+
24.0),
47+
/*tertiary_palette:*/
48+
TonalPalette(DynamicScheme::GetRotatedHue(set_source_color_hct, kHues,
49+
kTertiaryRotations),
50+
32.0),
51+
/*neutral_palette:*/
52+
TonalPalette(set_source_color_hct.get_hue() + 15.0, 8.0),
53+
/*neutral_variant_palette:*/
54+
TonalPalette(set_source_color_hct.get_hue() + 15, 12.0)) {}
55+
56+
SchemeExpressive::SchemeExpressive(Hct set_source_color_hct, bool set_is_dark)
57+
: SchemeExpressive::SchemeExpressive(set_source_color_hct, set_is_dark,
58+
0.0) {}
59+
60+
} // namespace material_color_utilities

cpp/scheme/scheme_expressive.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef CPP_SCHEME_SCHEME_EXPRESSIVE_H_
18+
#define CPP_SCHEME_SCHEME_EXPRESSIVE_H_
19+
20+
#include "cpp/cam/hct.h"
21+
#include "cpp/scheme/dynamic_scheme.h"
22+
23+
namespace material_color_utilities {
24+
25+
struct SchemeExpressive : public DynamicScheme {
26+
SchemeExpressive(Hct source_color_hct, bool is_dark, double contrast_level);
27+
SchemeExpressive(Hct source_color_hct, bool is_dark);
28+
};
29+
30+
} // namespace material_color_utilities
31+
32+
#endif // CPP_SCHEME_SCHEME_EXPRESSIVE_H_

0 commit comments

Comments
 (0)