Skip to content

Commit b4239d5

Browse files
committed
✨ Added ColorConverter
1 parent 7caa3d0 commit b4239d5

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

lib/chopper/chopper.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/// DCC Toolkit chopper util.
22
library chopper;
33

4+
export 'color_converter.dart';
5+
export 'datetime_converter.dart';
46
export 'json_serializable_converter.dart';
57
export 'no_content.dart';

lib/chopper/color_converter.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'dart:ui';
2+
3+
import 'package:freezed_annotation/freezed_annotation.dart';
4+
5+
/// A class that implements JsonConverter to convert Color objects to and from JSON.
6+
class ColorConverter implements JsonConverter<Color, String> {
7+
/// Converts a hexadecimal color string from JSON to a Color object.
8+
///
9+
/// The input string [json] is expected to be a hexadecimal color string.
10+
/// The method returns a Color object representing the color.
11+
@override
12+
Color fromJson(String json) {
13+
var hexColor = json.toUpperCase().replaceAll('#', '');
14+
if (hexColor.length == 6) {
15+
hexColor = 'FF$hexColor';
16+
}
17+
return Color(int.parse(hexColor, radix: 16));
18+
}
19+
20+
/// Converts a Color object to a hexadecimal color string for JSON.
21+
///
22+
/// The input [color] is a Color object.
23+
/// The method returns a string in the format #AARRGGBB, where AA is the alpha value,
24+
/// RR is the red value, GG is the green value, and BB is the blue value.
25+
/// Each value is a two-digit hexadecimal number.
26+
@override
27+
String toJson(Color color) {
28+
return '#${color.value.toRadixString(16).padLeft(8, '0')}';
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'dart:ui';
2+
3+
import 'package:dcc_toolkit/chopper/color_converter.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:parameterized_test/parameterized_test.dart';
6+
7+
void main() {
8+
parameterizedTest2('converts color to json value', [
9+
[const Color(0xFF000000), '#ff000000'],
10+
[const Color(0xFFFF0000), '#ffff0000'],
11+
[const Color(0xFFFFFF00), '#ffffff00'],
12+
[const Color(0xFFFFFFFF), '#ffffffff'],
13+
[const Color(0xF1F1F1F1), '#f1f1f1f1'],
14+
], (Color color, String expected) {
15+
final result = ColorConverter().toJson(color);
16+
17+
expect(result, expected);
18+
});
19+
20+
parameterizedTest2('converts json value to color', [
21+
[const Color(0xFF000000), '#ff000000'],
22+
[const Color(0xFFFF0000), '#ffff0000'],
23+
[const Color(0xFFFFFF00), '#ffffff00'],
24+
[const Color(0xFFFFFFFF), '#ffffffff'],
25+
[const Color(0xF1F1F1F1), '#f1f1f1f1'],
26+
[const Color(0xFFFFFFFF), '#ffffff'],
27+
], (Color expected, String json) {
28+
final result = ColorConverter().fromJson(json);
29+
30+
expect(result, expected);
31+
});
32+
}

0 commit comments

Comments
 (0)