|
12345678910111213141516171819202122232425262728293031323334353637383940 |
- /*
- * @Author: your name
- * @Date: 2020-05-18 14:01:56
- * @LastEditTime: 2020-06-24 13:45:26
- * @LastEditors: Please set LastEditors
- * @Description: In User Settings Edit
- * @FilePath: /hairuyi_flutter_yunfadan/lib/utils/color.dart
- */
-
- import 'dart:ui';
- import 'dart:math';
-
- import 'package:flutter/material.dart';
- import 'package:zhiying_comm/util/log/let_log.dart';
-
- extension HexColor on Color {
- /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
- static Color fromHex(String hexString) {
- try{
- final buffer = StringBuffer();
- if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
- buffer.write(hexString.replaceFirst('#', ''));
- return Color(int.parse(buffer.toString(), radix: 16));
- }catch(e){
- Logger.log(e);
- }
- return Colors.white;
- }
-
- static Color random() {
- return Color(Random().nextInt(1 << 32));
- }
-
- /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
- String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
- '${alpha.toRadixString(16).padLeft(2, '0')}'
- '${red.toRadixString(16).padLeft(2, '0')}'
- '${green.toRadixString(16).padLeft(2, '0')}'
- '${blue.toRadixString(16).padLeft(2, '0')}';
- }
|