feat(xep): Provide an implementation of XEP-0392

This commit is contained in:
PapaTutuWawa 2023-09-24 18:43:06 +02:00
parent 4a681b9483
commit c1c48d0a83
9 changed files with 163 additions and 0 deletions

View File

@ -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

30
packages/moxxmpp_color/.gitignore vendored Normal file
View File

@ -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/

View File

@ -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

View File

@ -0,0 +1,3 @@
## 0.1.0
- Implement functions to compute a color from a given input, following XEP-0392.

View File

@ -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.

View File

@ -0,0 +1,7 @@
# moxxmpp_color
An implementation of [XEP-0392](https://xmpp.org/extensions/xep-0392.html).
## License
See `LICENSE`.

View File

@ -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

View File

@ -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<int> 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<Color> consistentColor(
String input, {
double? saturation,
double? lightness,
}) async {
return _computeColor(
(await Sha1().hash(utf8.encode(input))).bytes,
saturation: saturation,
lightness: lightness,
);
}

View File

@ -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: