|
| 1 | +// Copyright (c) 2023- All flutter_graph_view authors. All rights reserved. |
| 2 | +// |
| 3 | +// This source code is licensed under Apache 2.0 License. |
| 4 | + |
| 5 | +import 'dart:ui'; |
| 6 | +import 'dart:math' as math; |
| 7 | + |
| 8 | +import 'package:flutter_graph_view/flutter_graph_view.dart'; |
| 9 | + |
| 10 | +/// The graph style configuration. |
| 11 | +/// |
| 12 | +/// 图的样式配置类。 |
| 13 | +class GraphStyle { |
| 14 | + /// [tagColor] is prior to [tagColorByIndex]. use [Vertex.tags] to get color in [vertexColors] |
| 15 | + /// |
| 16 | + /// [tagColor]的优先级比[tagColorByIndex]高。 |
| 17 | + /// 在[vertexColors]方法中使用[Vertex.tags]属性获取颜色 |
| 18 | + Map<String, Color>? tagColor; |
| 19 | + |
| 20 | + /// [tagColor] is prior to [tagColorByIndex]. use [Vertex.tags] to get color in [vertexColors] |
| 21 | + /// |
| 22 | + /// [tagColor]的优先级比[tagColorByIndex]高。 |
| 23 | + /// 在[vertexColors]方法中使用[Vertex.tags]属性获取颜色 |
| 24 | + late List<Color> tagColorByIndex = []; |
| 25 | + |
| 26 | + /// set elements color in [graph] |
| 27 | + /// |
| 28 | + /// 对[graph]中的元素设置颜色 |
| 29 | + void graphColor(Graph graph) { |
| 30 | + for (var vertex in graph.vertexes) { |
| 31 | + vertex.colors = vertexColors(vertex); |
| 32 | + } |
| 33 | + // TODO set edge color |
| 34 | + } |
| 35 | + |
| 36 | + /// get color list by [vertex]'s `tags`. |
| 37 | + /// |
| 38 | + /// 通过[vertex]中的`tags`属性获取颜色列表 |
| 39 | + List<Color> vertexColors(Vertex vertex) { |
| 40 | + var tags = vertex.tags; |
| 41 | + var allTags = vertex.cpn!.gameRef.graph.allTags; |
| 42 | + |
| 43 | + if (tags == null) { |
| 44 | + return defaultColor(); |
| 45 | + } |
| 46 | + List<Color> colors = []; |
| 47 | + |
| 48 | + for (var tag in tags) { |
| 49 | + Color? color; |
| 50 | + if (tagColor != null) { |
| 51 | + color = tagColor![tag]; |
| 52 | + } |
| 53 | + if (color == null) { |
| 54 | + var idx = allTags.indexOf(tag); |
| 55 | + if (idx < tagColorByIndex.length) color = tagColorByIndex[idx]; |
| 56 | + } |
| 57 | + if (color != null) { |
| 58 | + colors.add(color); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (colors.isEmpty) { |
| 63 | + return defaultColor(); |
| 64 | + } |
| 65 | + return colors; |
| 66 | + } |
| 67 | + |
| 68 | + /// when there is not color matched in [tagColor] on [tagColorByIndex], return random color. |
| 69 | + /// |
| 70 | + /// 当在 [tagColor] 与 [tagColorByIndex] 中匹配不到颜色时,返回随机颜色 |
| 71 | + var defaultColor = () { |
| 72 | + var r = math.Random(); |
| 73 | + return [ |
| 74 | + Color.fromRGBO( |
| 75 | + r.nextInt(160) + 80, |
| 76 | + r.nextInt(160) + 80, |
| 77 | + r.nextInt(160) + 80, |
| 78 | + 1, |
| 79 | + ), |
| 80 | + Color.fromRGBO( |
| 81 | + r.nextInt(160) + 80, |
| 82 | + r.nextInt(160) + 80, |
| 83 | + r.nextInt(160) + 80, |
| 84 | + 1, |
| 85 | + ), |
| 86 | + ]; |
| 87 | + }; |
| 88 | +} |
0 commit comments