diff options
| author | mirror <mirror@localhost> | 2026-11-22 08:41:00 +0900 |
|---|---|---|
| committer | mirror <mirror@localhost> | 2026-11-22 08:41:00 +0900 |
| commit | 449d8b74bd6d9dae98a07254cdfad677b920e7b0 (patch) | |
| tree | c131c3528c4d517f56e19b854a7453fedb097d40 /plugins/aves_utils/lib | |
| download | aves-src-master.tar.gz aves-src-master.zip | |
Diffstat (limited to 'plugins/aves_utils/lib')
| -rw-r--r-- | plugins/aves_utils/lib/aves_utils.dart | 4 | ||||
| -rw-r--r-- | plugins/aves_utils/lib/src/colors.dart | 72 | ||||
| -rw-r--r-- | plugins/aves_utils/lib/src/listenable.dart | 55 | ||||
| -rw-r--r-- | plugins/aves_utils/lib/src/optional_event_channel.dart | 58 | ||||
| -rw-r--r-- | plugins/aves_utils/lib/src/vector_utils.dart | 15 |
5 files changed, 204 insertions, 0 deletions
diff --git a/plugins/aves_utils/lib/aves_utils.dart b/plugins/aves_utils/lib/aves_utils.dart new file mode 100644 index 0000000..0058969 --- /dev/null +++ b/plugins/aves_utils/lib/aves_utils.dart @@ -0,0 +1,4 @@ +export 'src/colors.dart'; +export 'src/listenable.dart'; +export 'src/optional_event_channel.dart'; +export 'src/vector_utils.dart'; diff --git a/plugins/aves_utils/lib/src/colors.dart b/plugins/aves_utils/lib/src/colors.dart new file mode 100644 index 0000000..95c9d66 --- /dev/null +++ b/plugins/aves_utils/lib/src/colors.dart @@ -0,0 +1,72 @@ +import 'dart:convert'; +import 'dart:ui'; + +import 'package:flutter/foundation.dart'; + +class ColorUtils { + // `Color(0x00FFFFFF)` is different from `Color(0x00000000)` (or `Colors.transparent`) + // when used in gradients or lerping to it + static const transparentWhite = Color(0x00FFFFFF); + static const transparentBlack = Color(0x00000000); + + static Color textColorOn(Color background) { + final l = luma( + background.intRed, + background.intGreen, + background.intBlue, + ); + return Color(l >= .5 ? 0xFF000000 : 0xFFFFFFFF); + } + + // quick computation of luma (Y component of YUV or YIQ) from RGB + // cf https://en.wikipedia.org/wiki/Y%E2%80%B2UV + // cf https://en.wikipedia.org/wiki/YIQ + // `Color.computeLuminance()` is more accurate, but slower + // r, g, b in [0, 255], luma in [0, 1] + static double luma(int r, int g, int b) { + return (r * .299 + g * .587 + b * .114) / 255; + } +} + +extension ExtraColor on Color { + int get intRed => (r * 255).round(); // sRGB red component + int get intGreen => (g * 255).round(); // sRGB green component + int get intBlue => (b * 255).round(); // sRGB blue component + + // serialization + + String toJson() => jsonEncode(_toMap()); + + static Color? fromJson(String? jsonString) { + if (jsonString == null || jsonString.isEmpty) return null; + + try { + final jsonMap = jsonDecode(jsonString); + if (jsonMap is Map<String, dynamic>) { + return _fromMap(jsonMap); + } + debugPrint('failed to parse color from json=$jsonString'); + } catch (error, stack) { + debugPrint('failed to parse color from json=$jsonString error=$error\n$stack'); + } + return null; + } + + Map<String, dynamic> _toMap() => { + 'a': a, + 'r': r, + 'g': g, + 'b': b, + 'colorSpace': colorSpace.name, + }; + + static Color _fromMap(Map<String, dynamic> map) { + return Color.from( + alpha: map['a'] as double, + red: map['r'] as double, + green: map['g'] as double, + blue: map['b'] as double, + colorSpace: ColorSpace.values.byName(map['colorSpace'] as String), + ); + } +} diff --git a/plugins/aves_utils/lib/src/listenable.dart b/plugins/aves_utils/lib/src/listenable.dart new file mode 100644 index 0000000..75701cd --- /dev/null +++ b/plugins/aves_utils/lib/src/listenable.dart @@ -0,0 +1,55 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; + +// `ChangeNotifier` wrapper to call `notify` without constraint +class AChangeNotifier extends ChangeNotifier { + void notify() { + // why is this protected? + super.notifyListeners(); + } +} + +// contrary to standard `ValueListenableBuilder`, this widget allows providing a null listenable +class NullableValueListenableBuilder<T> extends StatefulWidget { + final ValueListenable<T?>? valueListenable; + final ValueWidgetBuilder<T?> builder; + final Widget? child; + + const NullableValueListenableBuilder({ + super.key, + required this.valueListenable, + required this.builder, + this.child, + }); + + @override + State<NullableValueListenableBuilder> createState() => _NullableValueListenableBuilderState<T>(); +} + +class _NullableValueListenableBuilderState<T> extends State<NullableValueListenableBuilder<T>> { + ValueNotifier<T?>? _internalValueListenable; + + ValueListenable<T?> get _valueListenable { + var listenable = widget.valueListenable; + if (listenable == null) { + _internalValueListenable ??= ValueNotifier(null); + listenable = _internalValueListenable; + } + return listenable!; + } + + @override + void dispose() { + _internalValueListenable?.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return ValueListenableBuilder<T?>( + valueListenable: _valueListenable, + builder: widget.builder, + child: widget.child, + ); + } +} diff --git a/plugins/aves_utils/lib/src/optional_event_channel.dart b/plugins/aves_utils/lib/src/optional_event_channel.dart new file mode 100644 index 0000000..4f8aff6 --- /dev/null +++ b/plugins/aves_utils/lib/src/optional_event_channel.dart @@ -0,0 +1,58 @@ +import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; + +// adapted from Flutter `EventChannel` in `/services/platform_channel.dart` +// to use an `OptionalMethodChannel` when subscribing to events +class OptionalEventChannel extends EventChannel { + const OptionalEventChannel(super.name, [super.codec = const StandardMethodCodec(), super.binaryMessenger]); + + @override + Stream<dynamic> receiveBroadcastStream([dynamic arguments]) { + final MethodChannel methodChannel = OptionalMethodChannel(name, codec); + late StreamController<dynamic> controller; + controller = StreamController<dynamic>.broadcast(onListen: () async { + binaryMessenger.setMessageHandler(name, (reply) async { + if (reply == null) { + await controller.close(); + } else { + try { + controller.add(codec.decodeEnvelope(reply)); + } on PlatformException catch (e) { + controller.addError(e); + } + } + return null; + }); + try { + await methodChannel.invokeMethod<void>('listen', arguments); + } catch (error, stack) { + FlutterError.reportError(FlutterErrorDetails( + exception: error, + stack: stack, + library: 'services library', + context: ErrorDescription('while activating platform stream on channel $name'), + )); + } + }, onCancel: () async { + binaryMessenger.setMessageHandler(name, null); + try { + await methodChannel.invokeMethod<void>('cancel', arguments); + } catch (error, stack) { + if (error is PlatformException && error.message == 'No active stream to cancel') { + // ignore + return; + } + + FlutterError.reportError(FlutterErrorDetails( + exception: error, + stack: stack, + library: 'services library', + context: ErrorDescription('while de-activating platform stream on channel $name'), + )); + } + }); + return controller.stream; + } +} diff --git a/plugins/aves_utils/lib/src/vector_utils.dart b/plugins/aves_utils/lib/src/vector_utils.dart new file mode 100644 index 0000000..14f3207 --- /dev/null +++ b/plugins/aves_utils/lib/src/vector_utils.dart @@ -0,0 +1,15 @@ +import 'dart:ui'; + +import 'package:vector_math/vector_math_64.dart'; + +extension ExtraOffset on Offset { + Vector3 get toVector3 => Vector3(dx, dy, 0); +} + +extension ExtraVector3 on Vector3 { + Offset get toOffset => Offset(x, y); +} + +extension ExtraMatrix4 on Matrix4 { + Offset transformOffset(Offset v) => transform3(v.toVector3).toOffset; +} |
