diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a85ca0..ae50a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.1.1 + +- Added `Color` extension for luminance. Thanks, @rania-run! +- Fixed a bug in the `splitByLength` extension on `String` + ## 1.1.0 - [BREAKING] Removed `Unfocuser` widget to ensure pure Dart compatibility. diff --git a/README.md b/README.md index 8461253..c3fe70a 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ providing utility functions and extensions that simplify common tasks. class, making it easier to format dates and calculate differences. - **String Extensions**: Enhances the `String` class by adding new methods for common transformations and checks, including JWT parsing. +- **Color Extensions**: Adds the ability to determine the luminance of a `Color` ## Getting Started @@ -330,6 +331,16 @@ objects: Additionally, the `CommonString` class provides a quick shortcut to common strings, such as punctuation marks that are otherwise cumbersome to find or type. +### Color Examples + +The following utilities have been added to enhance working with `Color` objects: + +- `luminance`: Computes the luminance of a given color. + + ```dart + final Color luminanceBasedColor = myColor.isDark ? Colors.white : Colors.black; + ``` + ## Contributing Contributions are welcome! Feel free to fork the repository and submit pull diff --git a/example/lib/main.dart b/example/lib/main.dart index 4a45d03..29880dc 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,4 +1,6 @@ import "package:arcane_helper_utils/arcane_helper_utils.dart"; +import "package:arcane_helper_utils/src/extensions/color.dart"; +import "package:pure_dart_ui/pure_dart_ui.dart"; void main() { // DateTime @@ -33,4 +35,10 @@ void main() { const String lowercase = "hello"; final String capitalized = lowercase.capitalize; print(capitalized); // "Hello" + + // Color + const Color myColor = Color.fromARGB(255, 153, 97, 227); + final Color luminanceBasedcolor = + myColor.isDark ? const Color(0xff000000) : const Color(0xffffffff); + print(luminanceBasedcolor); // Color(0xff000000) } diff --git a/lib/src/extensions/color.dart b/lib/src/extensions/color.dart new file mode 100644 index 0000000..6db45b0 --- /dev/null +++ b/lib/src/extensions/color.dart @@ -0,0 +1,14 @@ +import "package:pure_dart_ui/pure_dart_ui.dart"; + +/// A collection of extensions for the `Color` class. +extension ColorsExtensions on Color { + /// Determines if the color is considered dark. + /// + /// A color is considered dark if its luminance is less than 0.5. + bool get isDark { + final luminance = computeLuminance(); + // Luminance values range from 0 to 1, where 0 is black and 1 is white. + // A luminance value below 0.5 indicates a dark color. + return luminance < 0.5; + } +} diff --git a/lib/src/extensions/string.dart b/lib/src/extensions/string.dart index 7102a88..8fb5e28 100644 --- a/lib/src/extensions/string.dart +++ b/lib/src/extensions/string.dart @@ -63,8 +63,13 @@ extension Split on String { String string = this; while (string.isNotEmpty) { - parts.add(string.substring(0, length)); - string = string.substring(length); + if (string.length >= length) { + parts.add(string.substring(0, length)); + string = string.substring(length); + } else { + parts.add(string.substring(0, string.length)); + string = string.substring(string.length); + } } return parts; } diff --git a/pubspec.yaml b/pubspec.yaml index 355f082..0fc12ef 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: arcane_helper_utils description: Provides a variety of helpful utilities and extensions for Flutter and Dart. -version: 1.1.0 +version: 1.1.1 repository: https://github.com/hanskokx/arcane_helper_utils issue_tracker: https://github.com/hanskokx/arcane_helper_utils/issues @@ -15,6 +15,7 @@ environment: dependencies: freezed_annotation: ^2.4.4 + pure_dart_ui: ^0.1.10 week_number: ^1.1.0 dev_dependencies: