Skip to content

Commit

Permalink
Change implementation of darken and lighten
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Feb 25, 2024
1 parent 8210b17 commit be4b87c
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/src/styles/styles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,25 @@ Color? dividerColor(BuildContext context) =>
? CupertinoColors.separator.resolveFrom(context)
: null;

Color darken(Color color, [double amount = .1]) {
Color darken(Color c, [double amount = .1]) {
assert(amount >= 0 && amount <= 1);

final hsl = HSLColor.fromColor(color);
final hslDark = hsl.withLightness((hsl.lightness - amount).clamp(0.0, 1.0));

return hslDark.toColor();
final f = 1 - amount;
return Color.fromARGB(
c.alpha,
(c.red * f).round(),
(c.green * f).round(),
(c.blue * f).round(),
);
}

Color lighten(Color color, [double amount = .1]) {
Color lighten(Color c, [double amount = .1]) {
assert(amount >= 0 && amount <= 1);

final hsl = HSLColor.fromColor(color);
final hslLight = hsl.withLightness((hsl.lightness + amount).clamp(0.0, 1.0));

return hslLight.toColor();
return Color.fromARGB(
c.alpha,
c.red + ((255 - c.red) * amount).round(),
c.green + ((255 - c.green) * amount).round(),
c.blue + ((255 - c.blue) * amount).round(),
);
}

@immutable
Expand Down

0 comments on commit be4b87c

Please sign in to comment.