From 02ca6c75ac948b253de4da4b436a45fa6e5fecd9 Mon Sep 17 00:00:00 2001 From: Hasan Mohsin Date: Sun, 31 Mar 2024 22:36:13 +0100 Subject: [PATCH 1/7] Support tonal colors on api 34 --- .../dynamic_color/DynamicColorPlugin.kt | 75 +++++++++++++++++++ .../lib/src/colorscheme_from_list.dart | 70 +++++++++++++++++ .../lib/src/dynamic_color_builder.dart | 20 +++++ .../lib/src/dynamic_color_plugin.dart | 7 ++ 4 files changed, 172 insertions(+) create mode 100644 packages/dynamic_color/lib/src/colorscheme_from_list.dart diff --git a/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt b/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt index d9292130..4deaca8e 100644 --- a/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt +++ b/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt @@ -37,6 +37,15 @@ class DynamicColorPlugin : FlutterPlugin, MethodCallHandler { } } + "getColorScheme" -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + val resources: Resources = binding.applicationContext.resources + result.success(getColorScheme(resources)) + } else { + result.success(null) + } + } + else -> result.notImplemented() } } @@ -120,4 +129,70 @@ class DynamicColorPlugin : FlutterPlugin, MethodCallHandler { resources.getColor(android.R.color.system_neutral2_0, null), ); } + + @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) + private fun getColorScheme(resources: Resources): IntArray { + return intArrayOf( + // light + resources.getColor(android.R.color.system_primary_light), + resources.getColor(android.R.color.system_on_primary_light), + resources.getColor(android.R.color.system_primary_container_light), + resources.getColor(android.R.color.system_on_primary_container_light), + resources.getColor(android.R.color.system_secondary_light), + resources.getColor(android.R.color.system_on_secondary_light), + resources.getColor(android.R.color.system_secondary_container_light), + resources.getColor(android.R.color.system_on_secondary_container_light), + resources.getColor(android.R.color.system_tertiary_light), + resources.getColor(android.R.color.system_on_tertiary_light), + resources.getColor(android.R.color.system_tertiary_container_light), + resources.getColor(android.R.color.system_on_tertiary_container_light), + resources.getColor(android.R.color.system_error_light), + resources.getColor(android.R.color.system_on_error_light), + resources.getColor(android.R.color.system_error_container_light), + resources.getColor(android.R.color.system_on_error_container_light), + resources.getColor(android.R.color.system_surface_light), + resources.getColor(android.R.color.system_on_surface_light), + resources.getColor(android.R.color.system_on_surface_variant_light), + resources.getColor(android.R.color.system_surface_container_lowest_light), + resources.getColor(android.R.color.system_surface_container_low_light), + resources.getColor(android.R.color.system_surface_container_light), + resources.getColor(android.R.color.system_surface_container_high_light), + resources.getColor(android.R.color.system_surface_container_highest_light), + resources.getColor(android.R.color.system_surface_dark), + resources.getColor(android.R.color.system_on_surface_dark), + resources.getColor(android.R.color.system_primary_dark), + resources.getColor(android.R.color.system_outline_light), + resources.getColor(android.R.color.system_outline_variant_light), + // dark + resources.getColor(android.R.color.system_primary_dark), + resources.getColor(android.R.color.system_on_primary_dark), + resources.getColor(android.R.color.system_primary_container_dark), + resources.getColor(android.R.color.system_on_primary_container_dark), + resources.getColor(android.R.color.system_secondary_dark), + resources.getColor(android.R.color.system_on_secondary_dark), + resources.getColor(android.R.color.system_secondary_container_dark), + resources.getColor(android.R.color.system_on_secondary_container_dark), + resources.getColor(android.R.color.system_tertiary_dark), + resources.getColor(android.R.color.system_on_tertiary_dark), + resources.getColor(android.R.color.system_tertiary_container_dark), + resources.getColor(android.R.color.system_on_tertiary_container_dark), + resources.getColor(android.R.color.system_error_dark), + resources.getColor(android.R.color.system_on_error_dark), + resources.getColor(android.R.color.system_error_container_dark), + resources.getColor(android.R.color.system_on_error_container_dark), + resources.getColor(android.R.color.system_surface_dark), + resources.getColor(android.R.color.system_on_surface_dark), + resources.getColor(android.R.color.system_on_surface_variant_dark), + resources.getColor(android.R.color.system_surface_container_lowest_dark), + resources.getColor(android.R.color.system_surface_container_low_dark), + resources.getColor(android.R.color.system_surface_container_dark), + resources.getColor(android.R.color.system_surface_container_high_dark), + resources.getColor(android.R.color.system_surface_container_highest_dark), + resources.getColor(android.R.color.system_surface_light), + resources.getColor(android.R.color.system_on_surface_light), + resources.getColor(android.R.color.system_primary_light), + resources.getColor(android.R.color.system_outline_dark), + resources.getColor(android.R.color.system_outline_variant_dark), + ) + } } diff --git a/packages/dynamic_color/lib/src/colorscheme_from_list.dart b/packages/dynamic_color/lib/src/colorscheme_from_list.dart new file mode 100644 index 00000000..fc9f28df --- /dev/null +++ b/packages/dynamic_color/lib/src/colorscheme_from_list.dart @@ -0,0 +1,70 @@ +import 'package:flutter/material.dart'; + +List colorSchemeFromList(List colors) { + return [ + ColorScheme( + brightness: Brightness.light, + primary: Color(colors[0]), + onPrimary: Color(colors[1]), + primaryContainer: Color(colors[2]), + onPrimaryContainer: Color(colors[3]), + secondary: Color(colors[4]), + onSecondary: Color(colors[5]), + secondaryContainer: Color(colors[6]), + onSecondaryContainer: Color(colors[7]), + tertiary: Color(colors[8]), + onTertiary: Color(colors[9]), + tertiaryContainer: Color(colors[10]), + onTertiaryContainer: Color(colors[11]), + error: Color(colors[12]), + onError: Color(colors[13]), + errorContainer: Color(colors[14]), + onErrorContainer: Color(colors[15]), + surface: Color(colors[16]), + onSurface: Color(colors[17]), + onSurfaceVariant: Color(colors[18]), + surfaceContainerLowest: Color(colors[19]), + surfaceContainerLow: Color(colors[20]), + surfaceContainer: Color(colors[21]), + surfaceContainerHigh: Color(colors[22]), + surfaceContainerHighest: Color(colors[23]), + inverseSurface: Color(colors[24]), + onInverseSurface: Color(colors[25]), + inversePrimary: Color(colors[26]), + outline: Color(colors[27]), + outlineVariant: Color(colors[28]), + ), + ColorScheme( + brightness: Brightness.dark, + primary: Color(colors[29]), + onPrimary: Color(colors[30]), + primaryContainer: Color(colors[31]), + onPrimaryContainer: Color(colors[32]), + secondary: Color(colors[33]), + onSecondary: Color(colors[34]), + secondaryContainer: Color(colors[35]), + onSecondaryContainer: Color(colors[36]), + tertiary: Color(colors[37]), + onTertiary: Color(colors[38]), + tertiaryContainer: Color(colors[39]), + onTertiaryContainer: Color(colors[40]), + error: Color(colors[41]), + onError: Color(colors[42]), + errorContainer: Color(colors[43]), + onErrorContainer: Color(colors[44]), + surface: Color(colors[45]), + onSurface: Color(colors[46]), + onSurfaceVariant: Color(colors[47]), + surfaceContainerLowest: Color(colors[48]), + surfaceContainerLow: Color(colors[49]), + surfaceContainer: Color(colors[50]), + surfaceContainerHigh: Color(colors[51]), + surfaceContainerHighest: Color(colors[52]), + inverseSurface: Color(colors[53]), + onInverseSurface: Color(colors[54]), + inversePrimary: Color(colors[55]), + outline: Color(colors[56]), + outlineVariant: Color(colors[57]), + ), + ]; +} diff --git a/packages/dynamic_color/lib/src/dynamic_color_builder.dart b/packages/dynamic_color/lib/src/dynamic_color_builder.dart index d98790c9..6caf4f55 100644 --- a/packages/dynamic_color/lib/src/dynamic_color_builder.dart +++ b/packages/dynamic_color/lib/src/dynamic_color_builder.dart @@ -55,6 +55,26 @@ class DynamicColorBuilderState extends State { // Platform messages are asynchronous, so we initialize in an async method. Future initPlatformState() async { // Platform messages may fail, so we use a try/catch PlatformException. + try { + List? schemes = await DynamicColorPlugin.getColorScheme(); + + // If the widget was removed from the tree while the asynchronous platform + // message was in flight, we want to discard the reply rather than calling + // setState to update our non-existent appearance. + if (!mounted) return; + + if (schemes != null) { + debugPrint('dynamic_color: Core palette detected.'); + setState(() { + _light = schemes[0]; + _dark = schemes[1]; + }); + return; + } + } on PlatformException { + debugPrint('dynamic_color: Failed to obtain core palette.'); + } + try { CorePalette? corePalette = await DynamicColorPlugin.getCorePalette(); diff --git a/packages/dynamic_color/lib/src/dynamic_color_plugin.dart b/packages/dynamic_color/lib/src/dynamic_color_plugin.dart index 8bb87c16..a7d49ce4 100644 --- a/packages/dynamic_color/lib/src/dynamic_color_plugin.dart +++ b/packages/dynamic_color/lib/src/dynamic_color_plugin.dart @@ -1,5 +1,7 @@ import 'dart:async'; +import 'package:dynamic_color/src/colorscheme_from_list.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:material_color_utilities/material_color_utilities.dart'; @@ -30,6 +32,11 @@ class DynamicColorPlugin { return result == null ? null : CorePalette.fromList(result.toList()); } + static Future?> getColorScheme() async { + final result = await channel.invokeMethod('getColorScheme'); + return result == null ? null : colorSchemeFromList(result.toList()); + } + /// Returns the OS' accent color asynchronously as a [Color]. /// /// Supported on macOS starting with 10.14 (Mojave), on Windows starting with From 9cdfd8cd71e74e9a20d376737ae409ab361a0828 Mon Sep 17 00:00:00 2001 From: Hasan Mohsin Date: Sat, 6 Jul 2024 09:41:05 +0100 Subject: [PATCH 2/7] Add missing colours --- .../dynamic_color/DynamicColorPlugin.kt | 146 +++++++++++------- .../dynamic_color/lib/src/color_schemes.dart | 66 ++++++++ .../lib/src/colorscheme_from_list.dart | 70 --------- .../lib/src/dynamic_color_builder.dart | 11 +- .../lib/src/dynamic_color_plugin.dart | 17 +- 5 files changed, 171 insertions(+), 139 deletions(-) create mode 100644 packages/dynamic_color/lib/src/color_schemes.dart delete mode 100644 packages/dynamic_color/lib/src/colorscheme_from_list.dart diff --git a/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt b/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt index 4deaca8e..4016e826 100644 --- a/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt +++ b/packages/dynamic_color/android/src/main/kotlin/io/material/plugins/dynamic_color/DynamicColorPlugin.kt @@ -37,7 +37,7 @@ class DynamicColorPlugin : FlutterPlugin, MethodCallHandler { } } - "getColorScheme" -> { + "getColorSchemes" -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { val resources: Resources = binding.applicationContext.resources result.success(getColorScheme(resources)) @@ -134,65 +134,93 @@ class DynamicColorPlugin : FlutterPlugin, MethodCallHandler { private fun getColorScheme(resources: Resources): IntArray { return intArrayOf( // light - resources.getColor(android.R.color.system_primary_light), - resources.getColor(android.R.color.system_on_primary_light), - resources.getColor(android.R.color.system_primary_container_light), - resources.getColor(android.R.color.system_on_primary_container_light), - resources.getColor(android.R.color.system_secondary_light), - resources.getColor(android.R.color.system_on_secondary_light), - resources.getColor(android.R.color.system_secondary_container_light), - resources.getColor(android.R.color.system_on_secondary_container_light), - resources.getColor(android.R.color.system_tertiary_light), - resources.getColor(android.R.color.system_on_tertiary_light), - resources.getColor(android.R.color.system_tertiary_container_light), - resources.getColor(android.R.color.system_on_tertiary_container_light), - resources.getColor(android.R.color.system_error_light), - resources.getColor(android.R.color.system_on_error_light), - resources.getColor(android.R.color.system_error_container_light), - resources.getColor(android.R.color.system_on_error_container_light), - resources.getColor(android.R.color.system_surface_light), - resources.getColor(android.R.color.system_on_surface_light), - resources.getColor(android.R.color.system_on_surface_variant_light), - resources.getColor(android.R.color.system_surface_container_lowest_light), - resources.getColor(android.R.color.system_surface_container_low_light), - resources.getColor(android.R.color.system_surface_container_light), - resources.getColor(android.R.color.system_surface_container_high_light), - resources.getColor(android.R.color.system_surface_container_highest_light), - resources.getColor(android.R.color.system_surface_dark), - resources.getColor(android.R.color.system_on_surface_dark), - resources.getColor(android.R.color.system_primary_dark), - resources.getColor(android.R.color.system_outline_light), - resources.getColor(android.R.color.system_outline_variant_light), + resources.getColor(android.R.color.system_primary_light, null), + resources.getColor(android.R.color.system_on_primary_light, null), + resources.getColor(android.R.color.system_primary_container_light, null), + resources.getColor(android.R.color.system_on_primary_container_light, null), + resources.getColor(android.R.color.system_primary_fixed, null), + resources.getColor(android.R.color.system_primary_fixed_dim, null), + resources.getColor(android.R.color.system_on_primary_fixed, null), + resources.getColor(android.R.color.system_on_primary_fixed_variant, null), + resources.getColor(android.R.color.system_secondary_light, null), + resources.getColor(android.R.color.system_on_secondary_light, null), + resources.getColor(android.R.color.system_secondary_container_light, null), + resources.getColor(android.R.color.system_on_secondary_container_light, null), + resources.getColor(android.R.color.system_secondary_fixed, null), + resources.getColor(android.R.color.system_secondary_fixed_dim, null), + resources.getColor(android.R.color.system_on_secondary_fixed, null), + resources.getColor(android.R.color.system_on_secondary_fixed_variant, null), + resources.getColor(android.R.color.system_tertiary_light, null), + resources.getColor(android.R.color.system_on_tertiary_light, null), + resources.getColor(android.R.color.system_tertiary_container_light, null), + resources.getColor(android.R.color.system_on_tertiary_container_light, null), + resources.getColor(android.R.color.system_tertiary_fixed, null), + resources.getColor(android.R.color.system_tertiary_fixed_dim, null), + resources.getColor(android.R.color.system_on_tertiary_fixed, null), + resources.getColor(android.R.color.system_on_tertiary_fixed_variant, null), + resources.getColor(android.R.color.system_error_light, null), + resources.getColor(android.R.color.system_on_error_light, null), + resources.getColor(android.R.color.system_error_container_light, null), + resources.getColor(android.R.color.system_on_error_container_light, null), + resources.getColor(android.R.color.system_surface_light, null), + resources.getColor(android.R.color.system_on_surface_light, null), + resources.getColor(android.R.color.system_surface_dim_light, null), + resources.getColor(android.R.color.system_surface_bright_light, null), + resources.getColor(android.R.color.system_on_surface_variant_light, null), + resources.getColor(android.R.color.system_surface_container_lowest_light, null), + resources.getColor(android.R.color.system_surface_container_low_light, null), + resources.getColor(android.R.color.system_surface_container_light, null), + resources.getColor(android.R.color.system_surface_container_high_light, null), + resources.getColor(android.R.color.system_surface_container_highest_light, null), + resources.getColor(android.R.color.system_surface_dark, null), + resources.getColor(android.R.color.system_on_surface_dark, null), + resources.getColor(android.R.color.system_primary_dark, null), + resources.getColor(android.R.color.system_outline_light, null), + resources.getColor(android.R.color.system_outline_variant_light, null), // dark - resources.getColor(android.R.color.system_primary_dark), - resources.getColor(android.R.color.system_on_primary_dark), - resources.getColor(android.R.color.system_primary_container_dark), - resources.getColor(android.R.color.system_on_primary_container_dark), - resources.getColor(android.R.color.system_secondary_dark), - resources.getColor(android.R.color.system_on_secondary_dark), - resources.getColor(android.R.color.system_secondary_container_dark), - resources.getColor(android.R.color.system_on_secondary_container_dark), - resources.getColor(android.R.color.system_tertiary_dark), - resources.getColor(android.R.color.system_on_tertiary_dark), - resources.getColor(android.R.color.system_tertiary_container_dark), - resources.getColor(android.R.color.system_on_tertiary_container_dark), - resources.getColor(android.R.color.system_error_dark), - resources.getColor(android.R.color.system_on_error_dark), - resources.getColor(android.R.color.system_error_container_dark), - resources.getColor(android.R.color.system_on_error_container_dark), - resources.getColor(android.R.color.system_surface_dark), - resources.getColor(android.R.color.system_on_surface_dark), - resources.getColor(android.R.color.system_on_surface_variant_dark), - resources.getColor(android.R.color.system_surface_container_lowest_dark), - resources.getColor(android.R.color.system_surface_container_low_dark), - resources.getColor(android.R.color.system_surface_container_dark), - resources.getColor(android.R.color.system_surface_container_high_dark), - resources.getColor(android.R.color.system_surface_container_highest_dark), - resources.getColor(android.R.color.system_surface_light), - resources.getColor(android.R.color.system_on_surface_light), - resources.getColor(android.R.color.system_primary_light), - resources.getColor(android.R.color.system_outline_dark), - resources.getColor(android.R.color.system_outline_variant_dark), + resources.getColor(android.R.color.system_primary_dark, null), + resources.getColor(android.R.color.system_on_primary_dark, null), + resources.getColor(android.R.color.system_primary_container_dark, null), + resources.getColor(android.R.color.system_on_primary_container_dark, null), + resources.getColor(android.R.color.system_primary_fixed, null), + resources.getColor(android.R.color.system_primary_fixed_dim, null), + resources.getColor(android.R.color.system_on_primary_fixed, null), + resources.getColor(android.R.color.system_on_primary_fixed_variant, null), + resources.getColor(android.R.color.system_secondary_dark, null), + resources.getColor(android.R.color.system_on_secondary_dark, null), + resources.getColor(android.R.color.system_secondary_container_dark, null), + resources.getColor(android.R.color.system_on_secondary_container_dark, null), + resources.getColor(android.R.color.system_secondary_fixed, null), + resources.getColor(android.R.color.system_secondary_fixed_dim, null), + resources.getColor(android.R.color.system_on_secondary_fixed, null), + resources.getColor(android.R.color.system_on_secondary_fixed_variant, null), + resources.getColor(android.R.color.system_tertiary_dark, null), + resources.getColor(android.R.color.system_on_tertiary_dark, null), + resources.getColor(android.R.color.system_tertiary_container_dark, null), + resources.getColor(android.R.color.system_on_tertiary_container_dark, null), + resources.getColor(android.R.color.system_tertiary_fixed, null), + resources.getColor(android.R.color.system_tertiary_fixed_dim, null), + resources.getColor(android.R.color.system_on_tertiary_fixed, null), + resources.getColor(android.R.color.system_on_tertiary_fixed_variant, null), + resources.getColor(android.R.color.system_error_dark, null), + resources.getColor(android.R.color.system_on_error_dark, null), + resources.getColor(android.R.color.system_error_container_dark, null), + resources.getColor(android.R.color.system_on_error_container_dark, null), + resources.getColor(android.R.color.system_surface_dark, null), + resources.getColor(android.R.color.system_on_surface_dark, null), + resources.getColor(android.R.color.system_surface_dim_dark, null), + resources.getColor(android.R.color.system_surface_bright_dark, null), + resources.getColor(android.R.color.system_on_surface_variant_dark, null), + resources.getColor(android.R.color.system_surface_container_lowest_dark, null), + resources.getColor(android.R.color.system_surface_container_low_dark, null), + resources.getColor(android.R.color.system_surface_container_dark, null), + resources.getColor(android.R.color.system_surface_container_high_dark, null), + resources.getColor(android.R.color.system_surface_container_highest_dark, null), + resources.getColor(android.R.color.system_surface_light, null), + resources.getColor(android.R.color.system_on_surface_light, null), + resources.getColor(android.R.color.system_primary_light, null), + resources.getColor(android.R.color.system_outline_dark, null), + resources.getColor(android.R.color.system_outline_variant_dark, null), ) } } diff --git a/packages/dynamic_color/lib/src/color_schemes.dart b/packages/dynamic_color/lib/src/color_schemes.dart new file mode 100644 index 00000000..bb2f3d8b --- /dev/null +++ b/packages/dynamic_color/lib/src/color_schemes.dart @@ -0,0 +1,66 @@ +import 'package:flutter/material.dart'; + +class ColorSchemes { + final ColorScheme light; + final ColorScheme dark; + + const ColorSchemes({required this.light, required this.dark}); + + factory ColorSchemes.fromList(List colors) { + return ColorSchemes( + light: _colorSchemeFromList(colors, 0, Brightness.light), + dark: _colorSchemeFromList(colors, 1, Brightness.dark), + ); + } +} + +ColorScheme _colorSchemeFromList( + List colors, int n, Brightness brightness) { + int offset = (colors.length ~/ 2) * n; + return ColorScheme( + brightness: brightness, + primary: Color(colors[offset + 0]), + onPrimary: Color(colors[offset + 1]), + primaryContainer: Color(colors[offset + 2]), + onPrimaryContainer: Color(colors[offset + 3]), + primaryFixed: Color(colors[offset + 4]), + primaryFixedDim: Color(colors[offset + 5]), + onPrimaryFixed: Color(colors[offset + 6]), + onPrimaryFixedVariant: Color(colors[offset + 7]), + secondary: Color(colors[offset + 8]), + onSecondary: Color(colors[offset + 9]), + secondaryContainer: Color(colors[offset + 10]), + onSecondaryContainer: Color(colors[offset + 11]), + secondaryFixed: Color(colors[offset + 12]), + secondaryFixedDim: Color(colors[offset + 13]), + onSecondaryFixed: Color(colors[offset + 14]), + onSecondaryFixedVariant: Color(colors[offset + 15]), + tertiary: Color(colors[offset + 16]), + onTertiary: Color(colors[offset + 17]), + tertiaryContainer: Color(colors[offset + 18]), + onTertiaryContainer: Color(colors[offset + 19]), + tertiaryFixed: Color(colors[offset + 20]), + tertiaryFixedDim: Color(colors[offset + 21]), + onTertiaryFixed: Color(colors[offset + 22]), + onTertiaryFixedVariant: Color(colors[offset + 23]), + error: Color(colors[offset + 24]), + onError: Color(colors[offset + 25]), + errorContainer: Color(colors[offset + 26]), + onErrorContainer: Color(colors[offset + 27]), + surface: Color(colors[offset + 28]), + onSurface: Color(colors[offset + 29]), + surfaceDim: Color(colors[offset + 30]), + surfaceBright: Color(colors[offset + 31]), + onSurfaceVariant: Color(colors[offset + 32]), + surfaceContainerLowest: Color(colors[offset + 33]), + surfaceContainerLow: Color(colors[offset + 34]), + surfaceContainer: Color(colors[offset + 35]), + surfaceContainerHigh: Color(colors[offset + 36]), + surfaceContainerHighest: Color(colors[offset + 37]), + inverseSurface: Color(colors[offset + 38]), + onInverseSurface: Color(colors[offset + 39]), + inversePrimary: Color(colors[offset + 40]), + outline: Color(colors[offset + 41]), + outlineVariant: Color(colors[offset + 42]), + ); +} diff --git a/packages/dynamic_color/lib/src/colorscheme_from_list.dart b/packages/dynamic_color/lib/src/colorscheme_from_list.dart deleted file mode 100644 index fc9f28df..00000000 --- a/packages/dynamic_color/lib/src/colorscheme_from_list.dart +++ /dev/null @@ -1,70 +0,0 @@ -import 'package:flutter/material.dart'; - -List colorSchemeFromList(List colors) { - return [ - ColorScheme( - brightness: Brightness.light, - primary: Color(colors[0]), - onPrimary: Color(colors[1]), - primaryContainer: Color(colors[2]), - onPrimaryContainer: Color(colors[3]), - secondary: Color(colors[4]), - onSecondary: Color(colors[5]), - secondaryContainer: Color(colors[6]), - onSecondaryContainer: Color(colors[7]), - tertiary: Color(colors[8]), - onTertiary: Color(colors[9]), - tertiaryContainer: Color(colors[10]), - onTertiaryContainer: Color(colors[11]), - error: Color(colors[12]), - onError: Color(colors[13]), - errorContainer: Color(colors[14]), - onErrorContainer: Color(colors[15]), - surface: Color(colors[16]), - onSurface: Color(colors[17]), - onSurfaceVariant: Color(colors[18]), - surfaceContainerLowest: Color(colors[19]), - surfaceContainerLow: Color(colors[20]), - surfaceContainer: Color(colors[21]), - surfaceContainerHigh: Color(colors[22]), - surfaceContainerHighest: Color(colors[23]), - inverseSurface: Color(colors[24]), - onInverseSurface: Color(colors[25]), - inversePrimary: Color(colors[26]), - outline: Color(colors[27]), - outlineVariant: Color(colors[28]), - ), - ColorScheme( - brightness: Brightness.dark, - primary: Color(colors[29]), - onPrimary: Color(colors[30]), - primaryContainer: Color(colors[31]), - onPrimaryContainer: Color(colors[32]), - secondary: Color(colors[33]), - onSecondary: Color(colors[34]), - secondaryContainer: Color(colors[35]), - onSecondaryContainer: Color(colors[36]), - tertiary: Color(colors[37]), - onTertiary: Color(colors[38]), - tertiaryContainer: Color(colors[39]), - onTertiaryContainer: Color(colors[40]), - error: Color(colors[41]), - onError: Color(colors[42]), - errorContainer: Color(colors[43]), - onErrorContainer: Color(colors[44]), - surface: Color(colors[45]), - onSurface: Color(colors[46]), - onSurfaceVariant: Color(colors[47]), - surfaceContainerLowest: Color(colors[48]), - surfaceContainerLow: Color(colors[49]), - surfaceContainer: Color(colors[50]), - surfaceContainerHigh: Color(colors[51]), - surfaceContainerHighest: Color(colors[52]), - inverseSurface: Color(colors[53]), - onInverseSurface: Color(colors[54]), - inversePrimary: Color(colors[55]), - outline: Color(colors[56]), - outlineVariant: Color(colors[57]), - ), - ]; -} diff --git a/packages/dynamic_color/lib/src/dynamic_color_builder.dart b/packages/dynamic_color/lib/src/dynamic_color_builder.dart index 6caf4f55..51e7f084 100644 --- a/packages/dynamic_color/lib/src/dynamic_color_builder.dart +++ b/packages/dynamic_color/lib/src/dynamic_color_builder.dart @@ -1,3 +1,4 @@ +import 'package:dynamic_color/src/color_schemes.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -56,7 +57,7 @@ class DynamicColorBuilderState extends State { Future initPlatformState() async { // Platform messages may fail, so we use a try/catch PlatformException. try { - List? schemes = await DynamicColorPlugin.getColorScheme(); + ColorSchemes? schemes = await DynamicColorPlugin.getColorSchemes(); // If the widget was removed from the tree while the asynchronous platform // message was in flight, we want to discard the reply rather than calling @@ -64,15 +65,15 @@ class DynamicColorBuilderState extends State { if (!mounted) return; if (schemes != null) { - debugPrint('dynamic_color: Core palette detected.'); + debugPrint('dynamic_color: Color scheme detected.'); setState(() { - _light = schemes[0]; - _dark = schemes[1]; + _light = schemes.light; + _dark = schemes.dark; }); return; } } on PlatformException { - debugPrint('dynamic_color: Failed to obtain core palette.'); + debugPrint('dynamic_color: Failed to obtain color scheme.'); } try { diff --git a/packages/dynamic_color/lib/src/dynamic_color_plugin.dart b/packages/dynamic_color/lib/src/dynamic_color_plugin.dart index a7d49ce4..b43d7ab1 100644 --- a/packages/dynamic_color/lib/src/dynamic_color_plugin.dart +++ b/packages/dynamic_color/lib/src/dynamic_color_plugin.dart @@ -1,7 +1,6 @@ import 'dart:async'; -import 'package:dynamic_color/src/colorscheme_from_list.dart'; -import 'package:flutter/material.dart'; +import 'package:dynamic_color/src/color_schemes.dart'; import 'package:flutter/services.dart'; import 'package:material_color_utilities/material_color_utilities.dart'; @@ -32,9 +31,17 @@ class DynamicColorPlugin { return result == null ? null : CorePalette.fromList(result.toList()); } - static Future?> getColorScheme() async { - final result = await channel.invokeMethod('getColorScheme'); - return result == null ? null : colorSchemeFromList(result.toList()); + /// Returns the Android OS' dynamic colors asynchronously as a [ColorSchemes]. + /// + /// Completes with null on pre-Android U and non-Android platforms. + /// + /// See also: + /// + /// * [DynamicColorBuilder] a convenience stateful builder widget that + /// provides the dynamic colors + static Future getColorSchemes() async { + final result = await channel.invokeMethod('getColorSchemes'); + return result == null ? null : ColorSchemes.fromList(result.toList()); } /// Returns the OS' accent color asynchronously as a [Color]. From 96c1f1d620c3eab919c6696e5c7f2689fea373e5 Mon Sep 17 00:00:00 2001 From: Hasan Mohsin Date: Sat, 6 Jul 2024 18:00:44 +0100 Subject: [PATCH 3/7] Update changelog --- packages/dynamic_color/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/dynamic_color/CHANGELOG.md b/packages/dynamic_color/CHANGELOG.md index 3e93e7ce..d94eafcf 100644 --- a/packages/dynamic_color/CHANGELOG.md +++ b/packages/dynamic_color/CHANGELOG.md @@ -4,6 +4,7 @@ - Update other dependencies - Update `compileSdkVersion` to `34` - Fix lints +- Add new tone-based colors from Flutter 3.22 ## 1.7.0 - 2024-03-01 ### Changed From afe5763741d9eedb48354f11aebc9fea22185f00 Mon Sep 17 00:00:00 2001 From: Hasan Mohsin Date: Sun, 7 Jul 2024 21:55:45 +0100 Subject: [PATCH 4/7] Update tests --- .../lib/src/dynamic_color_plugin.dart | 8 +++++--- packages/dynamic_color/lib/test_utils.dart | 8 +++++++- .../test/dynamic_color_builder_test.dart | 14 ++++++++++++++ .../test/dynamic_color_plugin_test.dart | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/dynamic_color/lib/src/dynamic_color_plugin.dart b/packages/dynamic_color/lib/src/dynamic_color_plugin.dart index b43d7ab1..fe61b3d3 100644 --- a/packages/dynamic_color/lib/src/dynamic_color_plugin.dart +++ b/packages/dynamic_color/lib/src/dynamic_color_plugin.dart @@ -12,7 +12,9 @@ class DynamicColorPlugin { ); /// A method name that the Kotlin plugin listens for. - static const methodName = 'getCorePalette'; + static const corePaletteMethodName = 'getCorePalette'; + + static const colorSchemesMethodName = 'getColorSchemes'; /// A method name that the macOS plugin listens for. static const accentColorMethodName = 'getAccentColor'; @@ -27,7 +29,7 @@ class DynamicColorPlugin { /// * [DynamicColorBuilder] a convenience stateful builder widget that /// provides the dynamic colors static Future getCorePalette() async { - final result = await channel.invokeMethod(methodName); + final result = await channel.invokeMethod(corePaletteMethodName); return result == null ? null : CorePalette.fromList(result.toList()); } @@ -40,7 +42,7 @@ class DynamicColorPlugin { /// * [DynamicColorBuilder] a convenience stateful builder widget that /// provides the dynamic colors static Future getColorSchemes() async { - final result = await channel.invokeMethod('getColorSchemes'); + final result = await channel.invokeMethod(colorSchemesMethodName); return result == null ? null : ColorSchemes.fromList(result.toList()); } diff --git a/packages/dynamic_color/lib/test_utils.dart b/packages/dynamic_color/lib/test_utils.dart index 3c5eaacf..1663832c 100644 --- a/packages/dynamic_color/lib/test_utils.dart +++ b/packages/dynamic_color/lib/test_utils.dart @@ -17,6 +17,7 @@ class DynamicColorTestingUtils { /// Initializes the dynamic color plugin with mock values for testing. @visibleForTesting static void setMockDynamicColors({ + List? colorSchemes, CorePalette? corePalette, Color? accentColor, }) { @@ -24,10 +25,13 @@ class DynamicColorTestingUtils { .setMockMethodCallHandler(DynamicColorPlugin.channel, ( MethodCall methodCall, ) async { - if (methodCall.method == DynamicColorPlugin.methodName) { + if (methodCall.method == DynamicColorPlugin.corePaletteMethodName) { return corePalette != null ? Int64List.fromList(corePalette.asList()) : null; + } else if (methodCall.method == + DynamicColorPlugin.colorSchemesMethodName) { + return colorSchemes != null ? Int64List.fromList(colorSchemes) : null; } else if (methodCall.method == DynamicColorPlugin.accentColorMethodName) { return accentColor?.value; @@ -44,3 +48,5 @@ class DynamicColorTestingUtils { }); } } + +final List sampleColorSchemesList = List.generate(86, (i) => i + 42); diff --git a/packages/dynamic_color/test/dynamic_color_builder_test.dart b/packages/dynamic_color/test/dynamic_color_builder_test.dart index 2074298c..cb4c0a9f 100644 --- a/packages/dynamic_color/test/dynamic_color_builder_test.dart +++ b/packages/dynamic_color/test/dynamic_color_builder_test.dart @@ -38,6 +38,20 @@ void main() { expect(container.color, const Color(0xff286c2a)); }); + testWidgets('DynamicColorBuilder is correct on Android with colorSchemes', + (WidgetTester tester) async { + DynamicColorTestingUtils.setMockDynamicColors( + colorSchemes: sampleColorSchemesList, + corePalette: SampleCorePalettes.green, + ); + + await tester.pumpWidget(dynamicColorBuilder()); + await tester.pumpAndSettle(); + + final container = tester.firstWidget(find.byKey(containerKey)) as Container; + expect(container.color, const Color(0x0000002a)); + }); + testWidgets('DynamicColorBuilder is correct on desktop', (WidgetTester tester) async { DynamicColorTestingUtils.setMockDynamicColors( diff --git a/packages/dynamic_color/test/dynamic_color_plugin_test.dart b/packages/dynamic_color/test/dynamic_color_plugin_test.dart index be9a7b10..526f6b38 100644 --- a/packages/dynamic_color/test/dynamic_color_plugin_test.dart +++ b/packages/dynamic_color/test/dynamic_color_plugin_test.dart @@ -2,6 +2,7 @@ import 'dart:ui'; import 'package:dynamic_color/dynamic_color.dart'; import 'package:dynamic_color/test_utils.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { @@ -25,6 +26,22 @@ void main() { expect(colors, equals(null)); }); + test('getColorSchemes', () async { + DynamicColorTestingUtils.setMockDynamicColors( + colorSchemes: sampleColorSchemesList, + ); + final colorSchemes = await DynamicColorPlugin.getColorSchemes(); + expect(colorSchemes?.dark.primary, equals(const Color(0x00000055))); + }); + + test('getColorSchemes returns null', () async { + DynamicColorTestingUtils.setMockDynamicColors( + colorSchemes: null, + ); + final colorSchemes = await DynamicColorPlugin.getColorSchemes(); + expect(colorSchemes, equals(null)); + }); + test('getAccentColor', () async { const color = Color.fromARGB(12, 24, 123, 53); From 95b450af8c5baad4250296898a8f8f90f81c3842 Mon Sep 17 00:00:00 2001 From: Hasan Mohsin Date: Sun, 7 Jul 2024 23:32:06 +0100 Subject: [PATCH 5/7] Update core palette mapping with tonal colors --- .../lib/src/corepalette_to_colorscheme.dart | 137 +++++++++++++----- 1 file changed, 98 insertions(+), 39 deletions(-) diff --git a/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart b/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart index 776c607d..5ed20b1e 100644 --- a/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart +++ b/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart @@ -1,6 +1,3 @@ -// TODO(guidezpl): remove ignore after migration to new color roles -// ignore_for_file: deprecated_member_use - import 'package:flutter/material.dart'; import 'package:material_color_utilities/material_color_utilities.dart'; @@ -9,47 +6,109 @@ extension CorePaletteToColorScheme on CorePalette { ColorScheme toColorScheme({ Brightness brightness = Brightness.light, }) { - final Scheme scheme; - switch (brightness) { case Brightness.light: - scheme = Scheme.lightFromCorePalette(this); - break; + return _toLightColorScheme(); case Brightness.dark: - scheme = Scheme.darkFromCorePalette(this); - break; + return _toDarkColorScheme(); } + } + + ColorScheme _toLightColorScheme() { + return ColorScheme( + brightness: Brightness.light, + primary: Color(primary.get(40)), + onPrimary: Color(primary.get(100)), + primaryContainer: Color(primary.get(90)), + onPrimaryContainer: Color(primary.get(10)), + primaryFixed: Color(primary.get(90)), + primaryFixedDim: Color(primary.get(80)), + onPrimaryFixed: Color(primary.get(10)), + onPrimaryFixedVariant: Color(primary.get(30)), + secondary: Color(secondary.get(40)), + onSecondary: Color(secondary.get(100)), + secondaryContainer: Color(secondary.get(90)), + onSecondaryContainer: Color(secondary.get(10)), + secondaryFixed: Color(secondary.get(90)), + secondaryFixedDim: Color(secondary.get(80)), + onSecondaryFixed: Color(secondary.get(10)), + onSecondaryFixedVariant: Color(secondary.get(30)), + tertiary: Color(tertiary.get(40)), + onTertiary: Color(tertiary.get(100)), + tertiaryContainer: Color(tertiary.get(90)), + onTertiaryContainer: Color(tertiary.get(10)), + tertiaryFixed: Color(tertiary.get(90)), + tertiaryFixedDim: Color(tertiary.get(80)), + onTertiaryFixed: Color(tertiary.get(10)), + onTertiaryFixedVariant: Color(tertiary.get(30)), + error: Color(error.get(40)), + onError: Color(error.get(100)), + errorContainer: Color(error.get(90)), + onErrorContainer: Color(error.get(10)), + surface: Color(neutral.get(98)), + onSurface: Color(neutral.get(10)), + surfaceDim: Color(neutral.get(87)), + surfaceBright: Color(neutral.get(98)), + onSurfaceVariant: Color(neutralVariant.get(30)), + surfaceContainerLowest: Color(neutral.get(100)), + surfaceContainerLow: Color(neutral.get(96)), + surfaceContainer: Color(neutral.get(94)), + surfaceContainerHigh: Color(neutral.get(92)), + surfaceContainerHighest: Color(neutral.get(90)), + inverseSurface: Color(neutral.get(20)), + onInverseSurface: Color(neutral.get(95)), + inversePrimary: Color(primary.get(80)), + outline: Color(neutralVariant.get(50)), + outlineVariant: Color(neutralVariant.get(80)), + ); + } + + ColorScheme _toDarkColorScheme() { return ColorScheme( - primary: Color(scheme.primary), - onPrimary: Color(scheme.onPrimary), - primaryContainer: Color(scheme.primaryContainer), - onPrimaryContainer: Color(scheme.onPrimaryContainer), - secondary: Color(scheme.secondary), - onSecondary: Color(scheme.onSecondary), - secondaryContainer: Color(scheme.secondaryContainer), - onSecondaryContainer: Color(scheme.onSecondaryContainer), - tertiary: Color(scheme.tertiary), - onTertiary: Color(scheme.onTertiary), - tertiaryContainer: Color(scheme.tertiaryContainer), - onTertiaryContainer: Color(scheme.onTertiaryContainer), - error: Color(scheme.error), - onError: Color(scheme.onError), - errorContainer: Color(scheme.errorContainer), - onErrorContainer: Color(scheme.onErrorContainer), - outline: Color(scheme.outline), - outlineVariant: Color(scheme.outlineVariant), - background: Color(scheme.background), - onBackground: Color(scheme.onBackground), - surface: Color(scheme.surface), - onSurface: Color(scheme.onSurface), - surfaceVariant: Color(scheme.surfaceVariant), - onSurfaceVariant: Color(scheme.onSurfaceVariant), - inverseSurface: Color(scheme.inverseSurface), - onInverseSurface: Color(scheme.inverseOnSurface), - inversePrimary: Color(scheme.inversePrimary), - shadow: Color(scheme.shadow), - scrim: Color(scheme.scrim), - brightness: brightness, + brightness: Brightness.dark, + primary: Color(primary.get(80)), + onPrimary: Color(primary.get(20)), + primaryContainer: Color(primary.get(30)), + onPrimaryContainer: Color(primary.get(90)), + primaryFixed: Color(primary.get(90)), + primaryFixedDim: Color(primary.get(80)), + onPrimaryFixed: Color(primary.get(10)), + onPrimaryFixedVariant: Color(primary.get(30)), + secondary: Color(secondary.get(80)), + onSecondary: Color(secondary.get(20)), + secondaryContainer: Color(secondary.get(30)), + onSecondaryContainer: Color(secondary.get(90)), + secondaryFixed: Color(secondary.get(90)), + secondaryFixedDim: Color(secondary.get(80)), + onSecondaryFixed: Color(secondary.get(10)), + onSecondaryFixedVariant: Color(secondary.get(30)), + tertiary: Color(tertiary.get(80)), + onTertiary: Color(tertiary.get(20)), + tertiaryContainer: Color(tertiary.get(30)), + onTertiaryContainer: Color(tertiary.get(90)), + tertiaryFixed: Color(tertiary.get(90)), + tertiaryFixedDim: Color(tertiary.get(80)), + onTertiaryFixed: Color(tertiary.get(10)), + onTertiaryFixedVariant: Color(tertiary.get(30)), + error: Color(error.get(80)), + onError: Color(error.get(20)), + errorContainer: Color(error.get(30)), + onErrorContainer: Color(error.get(90)), + surface: Color(neutral.get(6)), + onSurface: Color(neutral.get(90)), + surfaceDim: Color(neutral.get(6)), + surfaceBright: Color(neutral.get(24)), + onSurfaceVariant: Color(neutralVariant.get(80)), + surfaceContainerLowest: Color(neutral.get(4)), + surfaceContainerLow: Color(neutral.get(10)), + surfaceContainer: Color(neutral.get(12)), + surfaceContainerHigh: Color(neutral.get(17)), + surfaceContainerHighest: Color(neutral.get(22)), + inverseSurface: Color(neutral.get(90)), + onInverseSurface: Color(neutral.get(20)), + inversePrimary: Color(primary.get(40)), + outline: Color(neutralVariant.get(60)), + outlineVariant: Color(neutralVariant.get(30)), ); } } From 293eabfca19f13375829afad8886b3f371fb1933 Mon Sep 17 00:00:00 2001 From: Hasan Mohsin Date: Tue, 9 Jul 2024 23:41:22 +0100 Subject: [PATCH 6/7] Guess uncommon tones from TonalPalette --- .../lib/src/corepalette_to_colorscheme.dart | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart b/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart index 5ed20b1e..114251aa 100644 --- a/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart +++ b/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart @@ -45,15 +45,15 @@ extension CorePaletteToColorScheme on CorePalette { onError: Color(error.get(100)), errorContainer: Color(error.get(90)), onErrorContainer: Color(error.get(10)), - surface: Color(neutral.get(98)), + surface: _guessTone(neutral, 98), onSurface: Color(neutral.get(10)), - surfaceDim: Color(neutral.get(87)), - surfaceBright: Color(neutral.get(98)), + surfaceDim: _guessTone(neutral, 87), + surfaceBright: _guessTone(neutral, 98), onSurfaceVariant: Color(neutralVariant.get(30)), surfaceContainerLowest: Color(neutral.get(100)), - surfaceContainerLow: Color(neutral.get(96)), - surfaceContainer: Color(neutral.get(94)), - surfaceContainerHigh: Color(neutral.get(92)), + surfaceContainerLow: _guessTone(neutral, 96), + surfaceContainer: _guessTone(neutral, 94), + surfaceContainerHigh: _guessTone(neutral, 92), surfaceContainerHighest: Color(neutral.get(90)), inverseSurface: Color(neutral.get(20)), onInverseSurface: Color(neutral.get(95)), @@ -94,16 +94,16 @@ extension CorePaletteToColorScheme on CorePalette { onError: Color(error.get(20)), errorContainer: Color(error.get(30)), onErrorContainer: Color(error.get(90)), - surface: Color(neutral.get(6)), + surface: _guessTone(neutral, 6), onSurface: Color(neutral.get(90)), - surfaceDim: Color(neutral.get(6)), - surfaceBright: Color(neutral.get(24)), + surfaceDim: _guessTone(neutral, 6), + surfaceBright: _guessTone(neutral, 24), onSurfaceVariant: Color(neutralVariant.get(80)), - surfaceContainerLowest: Color(neutral.get(4)), + surfaceContainerLowest: _guessTone(neutral, 4), surfaceContainerLow: Color(neutral.get(10)), - surfaceContainer: Color(neutral.get(12)), - surfaceContainerHigh: Color(neutral.get(17)), - surfaceContainerHighest: Color(neutral.get(22)), + surfaceContainer: _guessTone(neutral, 12), + surfaceContainerHigh: _guessTone(neutral, 17), + surfaceContainerHighest: _guessTone(neutral, 22), inverseSurface: Color(neutral.get(90)), onInverseSurface: Color(neutral.get(20)), inversePrimary: Color(primary.get(40)), @@ -112,3 +112,31 @@ extension CorePaletteToColorScheme on CorePalette { ); } } + +// This logic is taken from material_color_utilities 0.12 - https://github.com/material-foundation/material-color-utilities/blob/be615fc90286787bbe0c04ef58a6987e0e8fdc29/dart/lib/palettes/tonal_palette.dart#L93C5-L111. +// Once flutter updates to the latest version, this workaround can be removed. +Color _guessTone(TonalPalette palette, double tone) { + // Approximately deduces the original hue and chroma that generated this + // list of colors. + // Uses the hue and chroma of the provided color with the highest chroma. + + var bestHue = 0.0, bestChroma = 0.0; + for (final argb in palette.asList) { + final hct = Hct.fromInt(argb); + if (hct.tone == tone) { + return Color(hct.toInt()); + } + + // If the color is too close to white, its chroma may have been + // affected by a known issue, so we ignore it. + // https://github.com/material-foundation/material-color-utilities/issues/140 + + if (hct.tone > 98.0) continue; + + if (hct.chroma > bestChroma) { + bestHue = hct.hue; + bestChroma = hct.chroma; + } + } + return Color(Hct.from(bestHue, bestChroma, tone).toInt()); +} From 78667e1513de5cfd73b81ee29fe319de4eac5d85 Mon Sep 17 00:00:00 2001 From: Hasan Mohsin Date: Fri, 9 Aug 2024 08:16:14 +0100 Subject: [PATCH 7/7] Fix visibility of private helpers --- .../lib/src/corepalette_to_colorscheme.dart | 196 +++++++++--------- 1 file changed, 98 insertions(+), 98 deletions(-) diff --git a/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart b/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart index 114251aa..2cb40119 100644 --- a/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart +++ b/packages/dynamic_color/lib/src/corepalette_to_colorscheme.dart @@ -8,109 +8,109 @@ extension CorePaletteToColorScheme on CorePalette { }) { switch (brightness) { case Brightness.light: - return _toLightColorScheme(); + return _toLightColorScheme(this); case Brightness.dark: - return _toDarkColorScheme(); + return _toDarkColorScheme(this); } } +} - ColorScheme _toLightColorScheme() { - return ColorScheme( - brightness: Brightness.light, - primary: Color(primary.get(40)), - onPrimary: Color(primary.get(100)), - primaryContainer: Color(primary.get(90)), - onPrimaryContainer: Color(primary.get(10)), - primaryFixed: Color(primary.get(90)), - primaryFixedDim: Color(primary.get(80)), - onPrimaryFixed: Color(primary.get(10)), - onPrimaryFixedVariant: Color(primary.get(30)), - secondary: Color(secondary.get(40)), - onSecondary: Color(secondary.get(100)), - secondaryContainer: Color(secondary.get(90)), - onSecondaryContainer: Color(secondary.get(10)), - secondaryFixed: Color(secondary.get(90)), - secondaryFixedDim: Color(secondary.get(80)), - onSecondaryFixed: Color(secondary.get(10)), - onSecondaryFixedVariant: Color(secondary.get(30)), - tertiary: Color(tertiary.get(40)), - onTertiary: Color(tertiary.get(100)), - tertiaryContainer: Color(tertiary.get(90)), - onTertiaryContainer: Color(tertiary.get(10)), - tertiaryFixed: Color(tertiary.get(90)), - tertiaryFixedDim: Color(tertiary.get(80)), - onTertiaryFixed: Color(tertiary.get(10)), - onTertiaryFixedVariant: Color(tertiary.get(30)), - error: Color(error.get(40)), - onError: Color(error.get(100)), - errorContainer: Color(error.get(90)), - onErrorContainer: Color(error.get(10)), - surface: _guessTone(neutral, 98), - onSurface: Color(neutral.get(10)), - surfaceDim: _guessTone(neutral, 87), - surfaceBright: _guessTone(neutral, 98), - onSurfaceVariant: Color(neutralVariant.get(30)), - surfaceContainerLowest: Color(neutral.get(100)), - surfaceContainerLow: _guessTone(neutral, 96), - surfaceContainer: _guessTone(neutral, 94), - surfaceContainerHigh: _guessTone(neutral, 92), - surfaceContainerHighest: Color(neutral.get(90)), - inverseSurface: Color(neutral.get(20)), - onInverseSurface: Color(neutral.get(95)), - inversePrimary: Color(primary.get(80)), - outline: Color(neutralVariant.get(50)), - outlineVariant: Color(neutralVariant.get(80)), - ); - } +ColorScheme _toLightColorScheme(CorePalette corePalette) { + return ColorScheme( + brightness: Brightness.light, + primary: Color(corePalette.primary.get(40)), + onPrimary: Color(corePalette.primary.get(100)), + primaryContainer: Color(corePalette.primary.get(90)), + onPrimaryContainer: Color(corePalette.primary.get(10)), + primaryFixed: Color(corePalette.primary.get(90)), + primaryFixedDim: Color(corePalette.primary.get(80)), + onPrimaryFixed: Color(corePalette.primary.get(10)), + onPrimaryFixedVariant: Color(corePalette.primary.get(30)), + secondary: Color(corePalette.secondary.get(40)), + onSecondary: Color(corePalette.secondary.get(100)), + secondaryContainer: Color(corePalette.secondary.get(90)), + onSecondaryContainer: Color(corePalette.secondary.get(10)), + secondaryFixed: Color(corePalette.secondary.get(90)), + secondaryFixedDim: Color(corePalette.secondary.get(80)), + onSecondaryFixed: Color(corePalette.secondary.get(10)), + onSecondaryFixedVariant: Color(corePalette.secondary.get(30)), + tertiary: Color(corePalette.tertiary.get(40)), + onTertiary: Color(corePalette.tertiary.get(100)), + tertiaryContainer: Color(corePalette.tertiary.get(90)), + onTertiaryContainer: Color(corePalette.tertiary.get(10)), + tertiaryFixed: Color(corePalette.tertiary.get(90)), + tertiaryFixedDim: Color(corePalette.tertiary.get(80)), + onTertiaryFixed: Color(corePalette.tertiary.get(10)), + onTertiaryFixedVariant: Color(corePalette.tertiary.get(30)), + error: Color(corePalette.error.get(40)), + onError: Color(corePalette.error.get(100)), + errorContainer: Color(corePalette.error.get(90)), + onErrorContainer: Color(corePalette.error.get(10)), + surface: _guessTone(corePalette.neutral, 98), + onSurface: Color(corePalette.neutral.get(10)), + surfaceDim: _guessTone(corePalette.neutral, 87), + surfaceBright: _guessTone(corePalette.neutral, 98), + onSurfaceVariant: Color(corePalette.neutralVariant.get(30)), + surfaceContainerLowest: Color(corePalette.neutral.get(100)), + surfaceContainerLow: _guessTone(corePalette.neutral, 96), + surfaceContainer: _guessTone(corePalette.neutral, 94), + surfaceContainerHigh: _guessTone(corePalette.neutral, 92), + surfaceContainerHighest: Color(corePalette.neutral.get(90)), + inverseSurface: Color(corePalette.neutral.get(20)), + onInverseSurface: Color(corePalette.neutral.get(95)), + inversePrimary: Color(corePalette.primary.get(80)), + outline: Color(corePalette.neutralVariant.get(50)), + outlineVariant: Color(corePalette.neutralVariant.get(80)), + ); +} - ColorScheme _toDarkColorScheme() { - return ColorScheme( - brightness: Brightness.dark, - primary: Color(primary.get(80)), - onPrimary: Color(primary.get(20)), - primaryContainer: Color(primary.get(30)), - onPrimaryContainer: Color(primary.get(90)), - primaryFixed: Color(primary.get(90)), - primaryFixedDim: Color(primary.get(80)), - onPrimaryFixed: Color(primary.get(10)), - onPrimaryFixedVariant: Color(primary.get(30)), - secondary: Color(secondary.get(80)), - onSecondary: Color(secondary.get(20)), - secondaryContainer: Color(secondary.get(30)), - onSecondaryContainer: Color(secondary.get(90)), - secondaryFixed: Color(secondary.get(90)), - secondaryFixedDim: Color(secondary.get(80)), - onSecondaryFixed: Color(secondary.get(10)), - onSecondaryFixedVariant: Color(secondary.get(30)), - tertiary: Color(tertiary.get(80)), - onTertiary: Color(tertiary.get(20)), - tertiaryContainer: Color(tertiary.get(30)), - onTertiaryContainer: Color(tertiary.get(90)), - tertiaryFixed: Color(tertiary.get(90)), - tertiaryFixedDim: Color(tertiary.get(80)), - onTertiaryFixed: Color(tertiary.get(10)), - onTertiaryFixedVariant: Color(tertiary.get(30)), - error: Color(error.get(80)), - onError: Color(error.get(20)), - errorContainer: Color(error.get(30)), - onErrorContainer: Color(error.get(90)), - surface: _guessTone(neutral, 6), - onSurface: Color(neutral.get(90)), - surfaceDim: _guessTone(neutral, 6), - surfaceBright: _guessTone(neutral, 24), - onSurfaceVariant: Color(neutralVariant.get(80)), - surfaceContainerLowest: _guessTone(neutral, 4), - surfaceContainerLow: Color(neutral.get(10)), - surfaceContainer: _guessTone(neutral, 12), - surfaceContainerHigh: _guessTone(neutral, 17), - surfaceContainerHighest: _guessTone(neutral, 22), - inverseSurface: Color(neutral.get(90)), - onInverseSurface: Color(neutral.get(20)), - inversePrimary: Color(primary.get(40)), - outline: Color(neutralVariant.get(60)), - outlineVariant: Color(neutralVariant.get(30)), - ); - } +ColorScheme _toDarkColorScheme(CorePalette corePalette) { + return ColorScheme( + brightness: Brightness.dark, + primary: Color(corePalette.primary.get(80)), + onPrimary: Color(corePalette.primary.get(20)), + primaryContainer: Color(corePalette.primary.get(30)), + onPrimaryContainer: Color(corePalette.primary.get(90)), + primaryFixed: Color(corePalette.primary.get(90)), + primaryFixedDim: Color(corePalette.primary.get(80)), + onPrimaryFixed: Color(corePalette.primary.get(10)), + onPrimaryFixedVariant: Color(corePalette.primary.get(30)), + secondary: Color(corePalette.secondary.get(80)), + onSecondary: Color(corePalette.secondary.get(20)), + secondaryContainer: Color(corePalette.secondary.get(30)), + onSecondaryContainer: Color(corePalette.secondary.get(90)), + secondaryFixed: Color(corePalette.secondary.get(90)), + secondaryFixedDim: Color(corePalette.secondary.get(80)), + onSecondaryFixed: Color(corePalette.secondary.get(10)), + onSecondaryFixedVariant: Color(corePalette.secondary.get(30)), + tertiary: Color(corePalette.tertiary.get(80)), + onTertiary: Color(corePalette.tertiary.get(20)), + tertiaryContainer: Color(corePalette.tertiary.get(30)), + onTertiaryContainer: Color(corePalette.tertiary.get(90)), + tertiaryFixed: Color(corePalette.tertiary.get(90)), + tertiaryFixedDim: Color(corePalette.tertiary.get(80)), + onTertiaryFixed: Color(corePalette.tertiary.get(10)), + onTertiaryFixedVariant: Color(corePalette.tertiary.get(30)), + error: Color(corePalette.error.get(80)), + onError: Color(corePalette.error.get(20)), + errorContainer: Color(corePalette.error.get(30)), + onErrorContainer: Color(corePalette.error.get(90)), + surface: _guessTone(corePalette.neutral, 6), + onSurface: Color(corePalette.neutral.get(90)), + surfaceDim: _guessTone(corePalette.neutral, 6), + surfaceBright: _guessTone(corePalette.neutral, 24), + onSurfaceVariant: Color(corePalette.neutralVariant.get(80)), + surfaceContainerLowest: _guessTone(corePalette.neutral, 4), + surfaceContainerLow: Color(corePalette.neutral.get(10)), + surfaceContainer: _guessTone(corePalette.neutral, 12), + surfaceContainerHigh: _guessTone(corePalette.neutral, 17), + surfaceContainerHighest: _guessTone(corePalette.neutral, 22), + inverseSurface: Color(corePalette.neutral.get(90)), + onInverseSurface: Color(corePalette.neutral.get(20)), + inversePrimary: Color(corePalette.primary.get(40)), + outline: Color(corePalette.neutralVariant.get(60)), + outlineVariant: Color(corePalette.neutralVariant.get(30)), + ); } // This logic is taken from material_color_utilities 0.12 - https://github.com/material-foundation/material-color-utilities/blob/be615fc90286787bbe0c04ef58a6987e0e8fdc29/dart/lib/palettes/tonal_palette.dart#L93C5-L111.