基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

color.dart 1.1 KiB

4 years ago
1234567891011121314151617181920212223242526272829303132
  1. /*
  2. * @Author: your name
  3. * @Date: 2020-05-18 14:01:56
  4. * @LastEditTime: 2020-06-24 13:45:26
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: /hairuyi_flutter_yunfadan/lib/utils/color.dart
  8. */
  9. import 'dart:ui';
  10. import 'dart:math';
  11. extension HexColor on Color {
  12. /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  13. static Color fromHex(String hexString) {
  14. final buffer = StringBuffer();
  15. if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
  16. buffer.write(hexString.replaceFirst('#', ''));
  17. return Color(int.parse(buffer.toString(), radix: 16));
  18. }
  19. static Color random() {
  20. return Color(Random().nextInt(1 << 32));
  21. }
  22. /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  23. String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
  24. '${alpha.toRadixString(16).padLeft(2, '0')}'
  25. '${red.toRadixString(16).padLeft(2, '0')}'
  26. '${green.toRadixString(16).padLeft(2, '0')}'
  27. '${blue.toRadixString(16).padLeft(2, '0')}';
  28. }