diff --git a/README.md b/README.md index 3b8c78a..e2e561a 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ Documentation is available [here](https://moxxy.org/developers/docs/moxxmpp/). implements the RFC6120 connection algorithm and XEP-0368 direct TLS connections, if a DNS implementation is given, and supports StartTLS. +### moxxmpp_color + +Implementation of [XEP-0392](https://xmpp.org/extensions/xep-0392.html). + ## Development To begin, use [melos](https://github.com/invertase/melos) to bootstrap the project: `melos bootstrap`. Then, the example diff --git a/packages/moxxmpp_color/.gitignore b/packages/moxxmpp_color/.gitignore new file mode 100644 index 0000000..96486fd --- /dev/null +++ b/packages/moxxmpp_color/.gitignore @@ -0,0 +1,30 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +.packages +build/ diff --git a/packages/moxxmpp_color/.metadata b/packages/moxxmpp_color/.metadata new file mode 100644 index 0000000..e498da5 --- /dev/null +++ b/packages/moxxmpp_color/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "efbf63d9c66b9f6ec30e9ad4611189aa80003d31" + channel: "stable" + +project_type: package diff --git a/packages/moxxmpp_color/CHANGELOG.md b/packages/moxxmpp_color/CHANGELOG.md new file mode 100644 index 0000000..56999f9 --- /dev/null +++ b/packages/moxxmpp_color/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.1.0 + +- Implement functions to compute a color from a given input, following XEP-0392. diff --git a/packages/moxxmpp_color/LICENSE b/packages/moxxmpp_color/LICENSE new file mode 100644 index 0000000..b841600 --- /dev/null +++ b/packages/moxxmpp_color/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2023 Alexander "PapaTutuWawa" + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/packages/moxxmpp_color/README.md b/packages/moxxmpp_color/README.md new file mode 100644 index 0000000..2ba7fed --- /dev/null +++ b/packages/moxxmpp_color/README.md @@ -0,0 +1,7 @@ +# moxxmpp_color + +An implementation of [XEP-0392](https://xmpp.org/extensions/xep-0392.html). + +## License + +See `LICENSE`. diff --git a/packages/moxxmpp_color/analysis_options.yaml b/packages/moxxmpp_color/analysis_options.yaml new file mode 100644 index 0000000..bc5b27f --- /dev/null +++ b/packages/moxxmpp_color/analysis_options.yaml @@ -0,0 +1,10 @@ +include: package:very_good_analysis/analysis_options.yaml +linter: + rules: + public_member_api_docs: false + lines_longer_than_80_chars: false + use_setters_to_change_properties: false + avoid_positional_boolean_parameters: false + avoid_bool_literals_in_conditional_expressions: false + file_names: false + unnecessary_library_directive: false diff --git a/packages/moxxmpp_color/lib/moxxmpp_color.dart b/packages/moxxmpp_color/lib/moxxmpp_color.dart new file mode 100644 index 0000000..b32e501 --- /dev/null +++ b/packages/moxxmpp_color/lib/moxxmpp_color.dart @@ -0,0 +1,56 @@ +library moxxmpp_color; + +import 'dart:convert'; +import 'package:cryptography/cryptography.dart'; +import 'package:flutter/material.dart'; +import 'package:hsluv/extensions.dart'; + +/// The default saturation to use. +const _defaultSaturation = 50; + +/// The default lightness to use. +const _defaultLightness = 50; + +/// Implementation of the algorithm in XEP-0392. [hashBytes] are the bytes +/// of the SHA-1 hash of the input. +Color _computeColor( + List hashBytes, { + double? saturation, + double? lightness, +}) { + final bytes = hashBytes.sublist(0, 2); + final angle = (bytes.last << 8 + bytes.first).toDouble() / 65565; + return hsluvToRGBColor([ + angle * 360, + (saturation ?? _defaultSaturation).remainder(360), + (lightness ?? _defaultLightness).remainder(360), + ]); +} + +/// Like [consistentColor], but synchronous. +Color consistentColorSync( + String input, { + double? saturation, + double? lightness, +}) { + return _computeColor( + Sha1().toSync().hashSync(utf8.encode(input)).bytes, + saturation: saturation, + lightness: lightness, + ); +} + +/// Compute the color based on the algorithm described in XEP-0392. +/// [saturation] and [lightness] can be used to supply values to use +/// instead of the default. +Future consistentColor( + String input, { + double? saturation, + double? lightness, +}) async { + return _computeColor( + (await Sha1().hash(utf8.encode(input))).bytes, + saturation: saturation, + lightness: lightness, + ); +} diff --git a/packages/moxxmpp_color/pubspec.yaml b/packages/moxxmpp_color/pubspec.yaml new file mode 100644 index 0000000..82f858e --- /dev/null +++ b/packages/moxxmpp_color/pubspec.yaml @@ -0,0 +1,21 @@ +name: moxxmpp_color +description: Implementation of XEP-0392 +version: 0.1.0 +homepage: + +environment: + sdk: '>=3.1.0 <4.0.0' + flutter: ">=1.17.0" + +dependencies: + cryptography: ^2.7.0 + flutter: + sdk: flutter + hsluv: ^1.1.3 + +dev_dependencies: + flutter_test: + sdk: flutter + very_good_analysis: ^5.1.0 + +flutter: