Skip to content

Commit

Permalink
Added contrast extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Jan 29, 2024
1 parent 2bdf7e3 commit 7f8552c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/src/core/extensions/color_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ extension ColorExt on Color {
);
}

Color contrast([int amount = 100]) {
final p = RangeError.checkValueInInterval(amount, 0, 100, 'amount') / 100;

final luminance = computeLuminance();

return luminance > 0.5 ? darken((p).round()) : brighten((p).round());
}

Color darken([int amount = 10]) {
final p = RangeError.checkValueInInterval(amount, 0, 100, 'amount') / 100;
final hsl = HSLColor.fromColor(this);
Expand Down
24 changes: 24 additions & 0 deletions test/src/core/extensions/color_ext_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,28 @@ void main() {
expect(colorDto, equals(const ColorDto(color)));
});
});

// contrast
// group('contrast() Tests', () {
// test('should return the correct color', () {
// const color = Color.fromARGB(255, 255, 255, 255);
// final contrastColor = color.contrast();

// expect(contrastColor, equals(const Color.fromARGB(255, 0, 0, 0)));
// });

// test('should return the correct color', () {
// const color = Colors.black;
// final contrastColor = color.contrast();

// expect(contrastColor, Colors.white);
// });

// test('should return the correct color', () {
// const color = Colors.red;
// final contrastColor = color.contrast();

// expect(contrastColor, Colors.black);
// });
// });
}

0 comments on commit 7f8552c

Please sign in to comment.