Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredrummler/8d8fb6de60574d8ec540df64509f0026 to your computer and use it in GitHub Desktop.
Save jaredrummler/8d8fb6de60574d8ec540df64509f0026 to your computer and use it in GitHub Desktop.
MaterialColor createMaterialColor(Color color) {
  List strengths = <double>[.05];
  Map swatch = <int, Color>{};
  final int r = color.red, g = color.green, b = color.blue;

  for (int i = 1; i < 10; i++) {
    strengths.add(0.1 * i);
  }
  strengths.forEach((strength) {
    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);
}

void printCode(MaterialColor color) {
  final radix = 16;
  print('MaterialColor(0x${color.value.toRadixString(radix).toUpperCase()}, <int, Color>{');
  print("  50: Color(0x${color.shade50.value.toRadixString(radix).toUpperCase()}),");
  print("  100: Color(0x${color.shade100.value.toRadixString(radix).toUpperCase()}),");
  print("  200: Color(0x${color.shade200.value.toRadixString(radix).toUpperCase()}),");
  print("  300: Color(0x${color.shade300.value.toRadixString(radix).toUpperCase()}),");
  print("  400: Color(0x${color.shade400.value.toRadixString(radix).toUpperCase()}),");
  print("  500: Color(0x${color.shade500.value.toRadixString(radix).toUpperCase()}),");
  print("  600: Color(0x${color.shade600.value.toRadixString(radix).toUpperCase()}),");
  print("  700: Color(0x${color.shade700.value.toRadixString(radix).toUpperCase()}),");
  print("  800: Color(0x${color.shade800.value.toRadixString(radix).toUpperCase()}),");
  print("  900: Color(0x${color.shade900.value.toRadixString(radix).toUpperCase()}),");
  print('});');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment