-
Notifications
You must be signed in to change notification settings - Fork 157
/
harmonization.dart
61 lines (57 loc) · 2.26 KB
/
harmonization.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import 'package:flutter/material.dart';
import 'package:material_color_utilities/material_color_utilities.dart';
/// Shifts color [from] towards color [to].
Color _harmonizeColor(Color from, Color to) {
if (from == to) return from;
return Color(Blend.harmonize(from.value, to.value));
}
extension ColorHarmonization on Color {
/// Harmonizes this color with [color].
///
/// [color] will typically be a dynamic [ColorScheme.primary] color.
Color harmonizeWith(Color color) => _harmonizeColor(this, color);
}
extension ColorSchemeHarmonization on ColorScheme {
/// Harmonizes [color] with this [ColorScheme]'s [primary].
Color _harmonizeWithPrimary(Color color) => _harmonizeColor(color, primary);
/// Harmonizes semantic and custom [ColorScheme] colors with its [primary] color.
///
/// Harmonization makes adding and introducing new colors to your app more
/// seamless by automatically shifting hue and chroma slightly so that a
/// product's colors feel more cohesive with dynamic user colors.
///
/// Semantic colors (i.e. colors with meaning) include colors such as [error].
/// See https://m3.material.io/styles/color/the-color-system/custom-colors#harmonization
/// for more information.
///
/// Subclasses of [ColorScheme] that add custom colors should re-implement
/// [harmonized]. For example:
/// import 'package:dynamic_color/dynamic_color.dart';
///
/// class CustomColorScheme extends ColorScheme {
/// const CustomColorScheme(this.customYellow) : super(...);
///
/// final Color customYellow;
///
/// CustomColorScheme copyWith({ ... }) {}
///
/// CustomColorScheme harmonized() {
/// return copyWith(
/// customYellow: _harmonizeWithPrimary(customYellow),
/// error: _harmonizeWithPrimary(error),
/// onError: _harmonizeWithPrimary(onError),
/// errorContainer: _harmonizeWithPrimary(errorContainer),
/// onErrorContainer: _harmonizeWithPrimary(onErrorContainer),
/// );
/// }
/// }
///
ColorScheme harmonized() {
return copyWith(
error: _harmonizeWithPrimary(error),
onError: _harmonizeWithPrimary(onError),
errorContainer: _harmonizeWithPrimary(errorContainer),
onErrorContainer: _harmonizeWithPrimary(onErrorContainer),
);
}
}