k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
402 stars 33 forks source link

dart_json_mapper_flutter.dart Color Adapter #74

Closed jccard closed 4 years ago

jccard commented 4 years ago

First, thank you very much for these packages! They are incredibly helpful!

I believe the colorToString() and parseColor() functions need corrections. In their current state, they are not addressing the alpha component of the Color class.

colorToString() does not account for the alpha component.

parseColor() sets a value of zero (completely transparent).

I wasn't able to create a branch and make a pull request, but I wanted to mention this here in case anyone runs into the same problem that I did. I'll leave my changes here for your review.

String colorToString(Color color) {
    final aValue = color.alpha.toRadixString(16).padLeft(2, '0');
    final rValue = color.red.toRadixString(16).padLeft(2, '0');
    final gValue = color.green.toRadixString(16).padLeft(2, '0');
    final bValue = color.blue.toRadixString(16).padLeft(2, '0');
    return '#$aValue$rValue$gValue$bValue'.toUpperCase();
  }

  Color parseColor(String value) {
    return Color.fromARGB(
        int.tryParse(value.substring(1, 3), radix: 16),
        int.tryParse(value.substring(3, 5), radix: 16),
        int.tryParse(value.substring(5, 7), radix: 16),
        int.tryParse(value.substring(7), radix: 16));
  }

Thank you again for all of the work that you've put into these packages!

k-paxian commented 4 years ago

Thank you for your help!

I've added your changes to flutter adapter.

I wasn't able to create a branch and make a pull request, ...

You can always fork the repo, modify your own copy, and file a PR against original repo.

jccard commented 4 years ago

Noted.

Thank you; I'm still very new to all of this.