chore: add high level classes

Signed-off-by: The one with the braid <the-one@with-the-braid.cf>
This commit is contained in:
The one with the braid 2023-08-27 17:47:11 +02:00
parent 165226bee1
commit 78f88305ec
13 changed files with 678 additions and 316 deletions

View file

@ -0,0 +1,71 @@
/// Creates in [int] with a parsed [Color] value from RGB(A) color value.
///
/// Credits : https://pub.dev/packages/from_css_color - ported since dependency
/// on `dart:ui`, here reimplemented without but with raw int.
int parseRgbToInt(String color) {
List<String> channels = _parseChannels(color)!;
int result = 0xFF000000;
int shift = 16;
if (channels.length == 4) {
result = (_opacityChannelToHex(channels.removeLast()) << 24) & result;
} else if (channels.length != 3) {
throw FormatException(
'Incorrect number of values in RGB color string, there must be 3 or 4 of them.',
color,
);
}
if (_isPercentFormat(channels)) {
for (var ch in channels) {
result = (_rgbChannelPercentToHex(ch) << shift) | result;
shift -= 8;
}
} else {
for (var ch in channels) {
result = (_rgbChannelNumToHex(ch) << shift) | result;
shift -= 8;
}
}
return result;
}
/// Parses channels from RGBA/HSLA string representation.
List<String>? _parseChannels(color) {
return color.substring(color.indexOf('(') + 1, color.length - 1).split(',');
}
/// Converts RGB channel numeric [value] to hexadecimal integer form.
int _rgbChannelNumToHex(String value) {
return double.parse(value).clamp(0, 255).floor();
}
/// Returns `true` if all [rgb] channels are in percent format, `false` in non of them, throws otherwise.
bool _isPercentFormat(List<String> rgb) {
if (rgb.every((ch) => ch.endsWith('%'))) {
return true;
} else if (rgb.every((ch) => !ch.endsWith('%'))) {
return false;
} else {
throw FormatException(
'Mixing integer and percentage values in the same RGB color definition is forbidden.',
rgb.toString(),
);
}
}
/// Converts RGBA/HSLA opacity channel [value] to hexadecimal integer form.
int _opacityChannelToHex(String value) {
return (double.parse(value).clamp(0, 1) * 255).floor();
}
/// Converts RGB channel percentage [value] to hexadecimal integer form.
int _rgbChannelPercentToHex(String value) {
return (_parsePercent(value) * 255 / 100).floor();
}
/// Parses numeric value from string [percent] representation.
num _parsePercent(String percent) {
return (double.parse(percent.substring(0, percent.length - 1)).clamp(0, 100));
}

View file

@ -0,0 +1,29 @@
import '../models/pass_structure_dictionary.dart';
import 'color_helper.dart';
abstract class MaybeDecode {
const MaybeDecode._();
static int? maybeColor(String? colorCode) {
if (colorCode == null) return null;
return parseRgbToInt(colorCode);
}
static DateTime? maybeDateTime(String? timeStamp) {
if (timeStamp == null) return null;
return DateTime.tryParse(timeStamp);
}
static PassTextAlign? maybeTextAlign(String? align) {
switch (align) {
case 'PKTextAlignmentLeft':
return PassTextAlign.left;
case 'PKTextAlignmentCenter':
return PassTextAlign.center;
case 'PKTextAlignmentRight':
return PassTextAlign.right;
default:
return PassTextAlign.natural;
}
}
}