1.0.0
This commit is contained in:
43
example/lib/util/extensions/color_extensions.dart
Normal file
43
example/lib/util/extensions/color_extensions.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'dart:ui';
|
||||
|
||||
extension ColorExtension on Color {
|
||||
/// Convert the color to a darken color based on the [percent]
|
||||
Color darken([int percent = 40]) {
|
||||
assert(1 <= percent && percent <= 100);
|
||||
final value = 1 - percent / 100;
|
||||
return Color.fromARGB(
|
||||
_floatToInt8(a),
|
||||
(_floatToInt8(r) * value).round(),
|
||||
(_floatToInt8(g) * value).round(),
|
||||
(_floatToInt8(b) * value).round(),
|
||||
);
|
||||
}
|
||||
|
||||
Color lighten([int percent = 40]) {
|
||||
assert(1 <= percent && percent <= 100);
|
||||
final value = percent / 100;
|
||||
return Color.fromARGB(
|
||||
_floatToInt8(a),
|
||||
(_floatToInt8(r) + ((255 - _floatToInt8(r)) * value)).round(),
|
||||
(_floatToInt8(g) + ((255 - _floatToInt8(g)) * value)).round(),
|
||||
(_floatToInt8(b) + ((255 - _floatToInt8(b)) * value)).round(),
|
||||
);
|
||||
}
|
||||
|
||||
Color avg(Color other) {
|
||||
final red = (_floatToInt8(r) + _floatToInt8(other.r)) ~/ 2;
|
||||
final green = (_floatToInt8(g) + _floatToInt8(other.g)) ~/ 2;
|
||||
final blue = (_floatToInt8(b) + _floatToInt8(other.b)) ~/ 2;
|
||||
final alpha = (_floatToInt8(a) + _floatToInt8(other.a)) ~/ 2;
|
||||
return Color.fromARGB(alpha, red, green, blue);
|
||||
}
|
||||
|
||||
// Int color components were deprecated in Flutter 3.27.0.
|
||||
// This method is used to convert the new double color components to the
|
||||
// old int color components.
|
||||
//
|
||||
// Taken from the Color class.
|
||||
int _floatToInt8(double x) {
|
||||
return (x * 255.0).round() & 0xff;
|
||||
}
|
||||
}
|
||||
3
example/lib/util/extensions/iterable_extensions.dart
Normal file
3
example/lib/util/extensions/iterable_extensions.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
extension IterableToMapExtension<K, V> on Iterable<MapEntry<K, V>> {
|
||||
Map<K, V> get asMap => Map.fromEntries(this);
|
||||
}
|
||||
3
example/lib/util/extensions/list_extensions.dart
Normal file
3
example/lib/util/extensions/list_extensions.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
extension ListToMapExtension<K, V> on List<MapEntry<K, V>> {
|
||||
Map<K, V> get asMap => Map.fromEntries(this);
|
||||
}
|
||||
Reference in New Issue
Block a user