Skip to content

Instantly share code, notes, and snippets.

@moritzmorgenroth
Created December 31, 2019 11:01
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save moritzmorgenroth/5602102d855efde2d686b0c7c5a095ad to your computer and use it in GitHub Desktop.
Save moritzmorgenroth/5602102d855efde2d686b0c7c5a095ad to your computer and use it in GitHub Desktop.
import 'dart:math';
import 'package:flutter/material.dart';
MaterialColor generateMaterialColor(Color color) {
return MaterialColor(color.value, {
50: tintColor(color, 0.5),
100: tintColor(color, 0.4),
200: tintColor(color, 0.3),
300: tintColor(color, 0.2),
400: tintColor(color, 0.1),
500: tintColor(color, 0),
600: tintColor(color, -0.1),
700: tintColor(color, -0.2),
800: tintColor(color, -0.3),
900: tintColor(color, -0.4),
});
}
int tintValue(int value, double factor) =>
max(0, min((value + ((255 - value) * factor)).round(), 255));
Color tintColor(Color color, double factor) => Color.fromRGBO(
tintValue(color.red, factor),
tintValue(color.green, factor),
tintValue(color.blue, factor),
1);
@mars3142
Copy link

What's the license of this file?

@gadmel
Copy link

gadmel commented Jan 20, 2021

Hello, Moritz.
I'm trying to implement this snippet for custom material color schemes. And unfortunately getting the same issue. Could you kindly advise on how to solve it?

@gadmel
Copy link

gadmel commented Jan 20, 2021

Got it! Sorry, was my bad. It was the primary color, which had to be fully opaque. Thank you for the gist!

@ernestlipson
Copy link

Works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment