Skip to content

Instantly share code, notes, and snippets.

@YusufAbdelaziz
Last active November 8, 2022 10:41
Show Gist options
  • Save YusufAbdelaziz/33357399766883407c5624fa69b39886 to your computer and use it in GitHub Desktop.
Save YusufAbdelaziz/33357399766883407c5624fa69b39886 to your computer and use it in GitHub Desktop.
Lightens and darkens a material Color. Taken from a solution from StackOverflow.
import 'package:flutter/material.dart';
extension ColorDegree on Color {
Color darken([int percent = 10]) {
assert(1 <= percent && percent <= 100);
var f = 1 - percent / 100;
return Color.fromARGB(
this.alpha, (this.red * f).round(), (this.green * f).round(), (this.blue * f).round());
}
Color lighten([int percent = 10]) {
assert(1 <= percent && percent <= 100);
var p = percent / 100;
this.withRed(this.red + ((255 - this.red) * p).round());
this.withGreen(this.green + ((255 - this.green) * p).round());
this.withBlue(this.blue + ((255 - this.blue) * p).round());
return Color.fromARGB(this.alpha, this.red + ((255 - this.red) * p).round(),
this.green + ((255 - this.green) * p).round(), this.blue + ((255 - this.blue) * p).round());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment