Skip to content

Commit 248bb0c

Browse files
Material Engcopybara-github
authored andcommitted
Update DynamicScheme API for Dart.
PiperOrigin-RevId: 581920954
1 parent 45798af commit 248bb0c

18 files changed

+211
-126
lines changed

dart/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.9.0 - 2023-11-06
2+
### Added
3+
- Added getters for `DynamicScheme`.
4+
5+
### Changed
6+
- Moved `DynamicScheme` from `scheme/` to `dynamiccolor/`.
7+
18
## 0.8.0 - 2023-06-22
29
### Added
310
- Add fruit salad color scheme

dart/lib/dynamiccolor/dynamic_color.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import 'dart:math' as math;
1616

1717
import 'package:material_color_utilities/contrast/contrast.dart';
18+
import 'package:material_color_utilities/dynamiccolor/dynamic_scheme.dart';
1819
import 'package:material_color_utilities/hct/hct.dart';
1920
import 'package:material_color_utilities/palettes/tonal_palette.dart';
20-
import 'package:material_color_utilities/scheme/dynamic_scheme.dart';
2121
import 'package:material_color_utilities/utils/math_utils.dart';
2222

2323
import 'src/contrast_curve.dart';
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
import 'package:material_color_utilities/hct/hct.dart';
15+
import 'package:material_color_utilities/palettes/tonal_palette.dart';
16+
import 'package:material_color_utilities/utils/math_utils.dart';
17+
18+
import 'dynamic_color.dart';
19+
import 'material_dynamic_colors.dart';
20+
import 'variant.dart';
21+
22+
/// Constructed by a set of values representing the current UI state (such as
23+
/// whether or not its dark theme, what the theme style is, etc.), and
24+
/// provides a set of [TonalPalette]s that can create colors that fit in
25+
/// with the theme style. Used by [DynamicColor] to resolve into a color.
26+
class DynamicScheme {
27+
/// The source color of the theme as an ARGB integer.
28+
final int sourceColorArgb;
29+
30+
/// The source color of the theme in HCT.
31+
final Hct sourceColorHct;
32+
33+
/// The variant, or style, of the theme.
34+
final Variant variant;
35+
36+
/// Whether or not the scheme is in 'dark mode' or 'light mode'.
37+
final bool isDark;
38+
39+
/// Value from -1 to 1. -1 represents minimum contrast, 0 represents
40+
/// standard (i.e. the design as spec'd), and 1 represents maximum contrast.
41+
final double contrastLevel;
42+
43+
/// Given a tone, produces a color. Hue and chroma of the color are specified
44+
/// in the design specification of the variant. Usually colorful.
45+
final TonalPalette primaryPalette;
46+
47+
/// Given a tone, produces a color. Hue and chroma of the color are specified
48+
/// in the design specification of the variant. Usually less colorful.
49+
final TonalPalette secondaryPalette;
50+
51+
/// Given a tone, produces a color. Hue and chroma of the color are specified
52+
/// in the design specification of the variant. Usually a different hue from
53+
/// primary and colorful.
54+
final TonalPalette tertiaryPalette;
55+
56+
/// Given a tone, produces a color. Hue and chroma of the color are specified
57+
/// in the design specification of the variant. Usually not colorful at all,
58+
/// intended for background & surface colors.
59+
final TonalPalette neutralPalette;
60+
61+
/// Given a tone, produces a color. Hue and chroma of the color are specified
62+
/// in the design specification of the variant. Usually not colorful, but
63+
/// slightly more colorful than Neutral. Intended for backgrounds & surfaces.
64+
final TonalPalette neutralVariantPalette;
65+
66+
/// Given a tone, produces a reddish, colorful, color.
67+
final TonalPalette errorPalette;
68+
69+
DynamicScheme({
70+
required this.sourceColorArgb,
71+
required this.variant,
72+
this.contrastLevel = 0.0,
73+
required this.isDark,
74+
required this.primaryPalette,
75+
required this.secondaryPalette,
76+
required this.tertiaryPalette,
77+
required this.neutralPalette,
78+
required this.neutralVariantPalette,
79+
}) : sourceColorHct = Hct.fromInt(sourceColorArgb),
80+
errorPalette = TonalPalette.of(25.0, 84.0);
81+
82+
static double getRotatedHue(
83+
Hct sourceColor, List<double> hues, List<double> rotations) {
84+
final sourceHue = sourceColor.hue;
85+
assert(hues.length == rotations.length);
86+
if (rotations.length == 1) {
87+
return MathUtils.sanitizeDegreesDouble(sourceColor.hue + rotations[0]);
88+
}
89+
final size = hues.length;
90+
for (var i = 0; i <= (size - 2); i++) {
91+
final thisHue = hues[i];
92+
final nextHue = hues[i + 1];
93+
if (thisHue < sourceHue && sourceHue < nextHue) {
94+
return MathUtils.sanitizeDegreesDouble(sourceHue + rotations[i]);
95+
}
96+
}
97+
// If this statement executes, something is wrong, there should have been a rotation
98+
// found using the arrays.
99+
return sourceHue;
100+
}
101+
102+
Hct getHct(DynamicColor dynamicColor) => dynamicColor.getHct(this);
103+
int getArgb(DynamicColor dynamicColor) => dynamicColor.getArgb(this);
104+
105+
// Getters.
106+
int get primaryPaletteKeyColor =>
107+
getArgb(MaterialDynamicColors.primaryPaletteKeyColor);
108+
int get secondaryPaletteKeyColor =>
109+
getArgb(MaterialDynamicColors.secondaryPaletteKeyColor);
110+
int get tertiaryPaletteKeyColor =>
111+
getArgb(MaterialDynamicColors.tertiaryPaletteKeyColor);
112+
int get neutralPaletteKeyColor =>
113+
getArgb(MaterialDynamicColors.neutralPaletteKeyColor);
114+
int get neutralVariantPaletteKeyColor =>
115+
getArgb(MaterialDynamicColors.neutralVariantPaletteKeyColor);
116+
int get background => getArgb(MaterialDynamicColors.background);
117+
int get onBackground => getArgb(MaterialDynamicColors.onBackground);
118+
int get surface => getArgb(MaterialDynamicColors.surface);
119+
int get surfaceDim => getArgb(MaterialDynamicColors.surfaceDim);
120+
int get surfaceBright => getArgb(MaterialDynamicColors.surfaceBright);
121+
int get surfaceContainerLowest =>
122+
getArgb(MaterialDynamicColors.surfaceContainerLowest);
123+
int get surfaceContainerLow =>
124+
getArgb(MaterialDynamicColors.surfaceContainerLow);
125+
int get surfaceContainer => getArgb(MaterialDynamicColors.surfaceContainer);
126+
int get surfaceContainerHigh =>
127+
getArgb(MaterialDynamicColors.surfaceContainerHigh);
128+
int get surfaceContainerHighest =>
129+
getArgb(MaterialDynamicColors.surfaceContainerHighest);
130+
int get onSurface => getArgb(MaterialDynamicColors.onSurface);
131+
int get surfaceVariant => getArgb(MaterialDynamicColors.surfaceVariant);
132+
int get onSurfaceVariant => getArgb(MaterialDynamicColors.onSurfaceVariant);
133+
int get inverseSurface => getArgb(MaterialDynamicColors.inverseSurface);
134+
int get inverseOnSurface => getArgb(MaterialDynamicColors.inverseOnSurface);
135+
int get outline => getArgb(MaterialDynamicColors.outline);
136+
int get outlineVariant => getArgb(MaterialDynamicColors.outlineVariant);
137+
int get shadow => getArgb(MaterialDynamicColors.shadow);
138+
int get scrim => getArgb(MaterialDynamicColors.scrim);
139+
int get surfaceTint => getArgb(MaterialDynamicColors.surfaceTint);
140+
int get primary => getArgb(MaterialDynamicColors.primary);
141+
int get onPrimary => getArgb(MaterialDynamicColors.onPrimary);
142+
int get primaryContainer => getArgb(MaterialDynamicColors.primaryContainer);
143+
int get onPrimaryContainer =>
144+
getArgb(MaterialDynamicColors.onPrimaryContainer);
145+
int get inversePrimary => getArgb(MaterialDynamicColors.inversePrimary);
146+
int get secondary => getArgb(MaterialDynamicColors.secondary);
147+
int get onSecondary => getArgb(MaterialDynamicColors.onSecondary);
148+
int get secondaryContainer =>
149+
getArgb(MaterialDynamicColors.secondaryContainer);
150+
int get onSecondaryContainer =>
151+
getArgb(MaterialDynamicColors.onSecondaryContainer);
152+
int get tertiary => getArgb(MaterialDynamicColors.tertiary);
153+
int get onTertiary => getArgb(MaterialDynamicColors.onTertiary);
154+
int get tertiaryContainer => getArgb(MaterialDynamicColors.tertiaryContainer);
155+
int get onTertiaryContainer =>
156+
getArgb(MaterialDynamicColors.onTertiaryContainer);
157+
int get error => getArgb(MaterialDynamicColors.error);
158+
int get onError => getArgb(MaterialDynamicColors.onError);
159+
int get errorContainer => getArgb(MaterialDynamicColors.errorContainer);
160+
int get onErrorContainer => getArgb(MaterialDynamicColors.onErrorContainer);
161+
int get primaryFixed => getArgb(MaterialDynamicColors.primaryFixed);
162+
int get primaryFixedDim => getArgb(MaterialDynamicColors.primaryFixedDim);
163+
int get onPrimaryFixed => getArgb(MaterialDynamicColors.onPrimaryFixed);
164+
int get onPrimaryFixedVariant =>
165+
getArgb(MaterialDynamicColors.onPrimaryFixedVariant);
166+
int get secondaryFixed => getArgb(MaterialDynamicColors.secondaryFixed);
167+
int get secondaryFixedDim => getArgb(MaterialDynamicColors.secondaryFixedDim);
168+
int get onSecondaryFixed => getArgb(MaterialDynamicColors.onSecondaryFixed);
169+
int get onSecondaryFixedVariant =>
170+
getArgb(MaterialDynamicColors.onSecondaryFixedVariant);
171+
int get tertiaryFixed => getArgb(MaterialDynamicColors.tertiaryFixed);
172+
int get tertiaryFixedDim => getArgb(MaterialDynamicColors.tertiaryFixedDim);
173+
int get onTertiaryFixed => getArgb(MaterialDynamicColors.onTertiaryFixed);
174+
int get onTertiaryFixedVariant =>
175+
getArgb(MaterialDynamicColors.onTertiaryFixedVariant);
176+
}

dart/lib/dynamiccolor/material_dynamic_colors.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import 'dart:math' as math;
1616

1717
import 'package:material_color_utilities/dislike/dislike_analyzer.dart';
18+
import 'package:material_color_utilities/dynamiccolor/dynamic_scheme.dart';
19+
import 'package:material_color_utilities/dynamiccolor/variant.dart';
1820
import 'package:material_color_utilities/hct/hct.dart';
19-
import 'package:material_color_utilities/scheme/dynamic_scheme.dart';
20-
import 'package:material_color_utilities/scheme/variant.dart';
2121

2222
import 'dynamic_color.dart';
2323
import 'src/contrast_curve.dart';
File renamed without changes.

dart/lib/material_color_utilities.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export 'blend/blend.dart';
1818
export 'contrast/contrast.dart';
1919
export 'dislike/dislike_analyzer.dart';
2020
export 'dynamiccolor/dynamic_color.dart';
21+
export 'dynamiccolor/dynamic_scheme.dart';
2122
export 'dynamiccolor/material_dynamic_colors.dart';
23+
export 'dynamiccolor/variant.dart';
2224
export 'hct/cam16.dart';
2325
export 'hct/hct.dart';
2426
export 'hct/viewing_conditions.dart';
@@ -29,7 +31,6 @@ export 'quantize/quantizer_celebi.dart';
2931
export 'quantize/quantizer_map.dart';
3032
export 'quantize/quantizer_wsmeans.dart';
3133
export 'quantize/quantizer_wu.dart';
32-
export 'scheme/dynamic_scheme.dart';
3334
export 'scheme/scheme.dart';
3435
export 'scheme/scheme_content.dart';
3536
export 'scheme/scheme_expressive.dart';
@@ -40,7 +41,6 @@ export 'scheme/scheme_neutral.dart';
4041
export 'scheme/scheme_rainbow.dart';
4142
export 'scheme/scheme_tonal_spot.dart';
4243
export 'scheme/scheme_vibrant.dart';
43-
export 'scheme/variant.dart';
4444
export 'score/score.dart';
4545
export 'temperature/temperature_cache.dart';
4646
export 'utils/color_utils.dart';

dart/lib/scheme/dynamic_scheme.dart

Lines changed: 0 additions & 98 deletions
This file was deleted.

dart/lib/scheme/scheme_content.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
import 'dart:math' as math;
1515

1616
import 'package:material_color_utilities/dislike/dislike_analyzer.dart';
17+
import 'package:material_color_utilities/dynamiccolor/dynamic_scheme.dart';
18+
import 'package:material_color_utilities/dynamiccolor/variant.dart';
1719
import 'package:material_color_utilities/hct/hct.dart';
1820
import 'package:material_color_utilities/palettes/tonal_palette.dart';
1921
import 'package:material_color_utilities/temperature/temperature_cache.dart';
20-
import 'dynamic_scheme.dart';
21-
import 'variant.dart';
2222

2323
/// A scheme that places the source color in [Scheme.primaryContainer].
2424
///

dart/lib/scheme/scheme_expressive.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
import 'package:material_color_utilities/dynamiccolor/dynamic_scheme.dart';
15+
import 'package:material_color_utilities/dynamiccolor/variant.dart';
1416
import 'package:material_color_utilities/hct/hct.dart';
1517
import 'package:material_color_utilities/palettes/tonal_palette.dart';
1618
import 'package:material_color_utilities/utils/math_utils.dart';
17-
import 'dynamic_scheme.dart';
18-
import 'variant.dart';
1919

2020
/// A Dynamic Color theme that is intentionally detached from the input color.
2121
class SchemeExpressive extends DynamicScheme {

dart/lib/scheme/scheme_fidelity.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
import 'dart:math' as math;
1515

1616
import 'package:material_color_utilities/dislike/dislike_analyzer.dart';
17+
import 'package:material_color_utilities/dynamiccolor/dynamic_scheme.dart';
18+
import 'package:material_color_utilities/dynamiccolor/variant.dart';
1719
import 'package:material_color_utilities/hct/hct.dart';
1820
import 'package:material_color_utilities/palettes/tonal_palette.dart';
1921
import 'package:material_color_utilities/temperature/temperature_cache.dart';
20-
import 'dynamic_scheme.dart';
21-
import 'variant.dart';
2222

2323
/// A scheme that places the source color in [Scheme.primaryContainer].
24-
///
24+
///
2525
/// Primary Container is the source color, adjusted for color relativity.
26-
/// It maintains constant appearance in light mode and dark mode.
27-
/// This adds ~5 tone in light mode, and subtracts ~5 tone in dark mode.
26+
/// It maintains constant appearance in light mode and dark mode.
27+
/// This adds ~5 tone in light mode, and subtracts ~5 tone in dark mode.
2828
///
2929
/// Tertiary Container is the complement to the source color, using
3030
/// [TemperatureCache]. It also maintains constant appearance.

0 commit comments

Comments
 (0)