createPrimarySwatch static method

MaterialColor createPrimarySwatch(
  1. Color? color
)

Create a primary Material color swatch from a given color.

The provided color is used as the Material swatch default color 500 in the returned swatch, with lighter hues for lower indexes and darker shades for higher index values.

If you give this function a standard Material color index 500 value, eg Colors.red[500] it will not return the same swatch as Colors.red. This function is an approximation and gives an automated way of creating a Material like primary swatch.

Implementation

static MaterialColor createPrimarySwatch(Color? color) {
  // Null default fallback is default material primary light color.
  // ignore: parameter_assignments
  color ??= FlexColor.materialLightPrimary;
  const List<double> strengths = <double> //
      [0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9];
  final Map<int, Color> swatch = <int, Color>{};
  final int r = color.red;
  final int g = color.green;
  final int b = color.blue;
  for (final double strength in strengths) {
    final double ds = 0.5 - strength;
    swatch[(strength * 1000).round()] = Color.fromRGBO(
      r + ((ds < 0 ? r : (255 - r)) * ds).round(),
      g + ((ds < 0 ? g : (255 - g)) * ds).round(),
      b + ((ds < 0 ? b : (255 - b)) * ds).round(),
      1,
    );
  }
  return MaterialColor(color.value, swatch);
}