Skip to content

Commit

Permalink
add toHexString extension
Browse files Browse the repository at this point in the history
  • Loading branch information
petrnymsa committed Jan 13, 2025
1 parent b42e041 commit 4ea10c7
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ extension ColorExtensions on Color {

return Color(int.parse(value, radix: 16));
}

// ignore: prefer-boolean-prefixes, ok naming.
String toHexString({bool includeSign = false, bool includeAlpha = true, bool asUpperCase = true}) {
final alpha = (a * 255).toInt().toRadixString(16).padLeft(2, '0');
final red = (r * 255).toInt().toRadixString(16).padLeft(2, '0');
final green = (g * 255).toInt().toRadixString(16).padLeft(2, '0');
final blue = (b * 255).toInt().toRadixString(16).padLeft(2, '0');

final result = '${includeSign ? '#' : ''}${includeAlpha ? alpha : ''}$red$green$blue';

return asUpperCase ? result.toUpperCase() : result;
}
}

0 comments on commit 4ea10c7

Please sign in to comment.