|
| 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 | +} |
0 commit comments