forked from Syphdias/i3nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3nodes_graph.py
executable file
·52 lines (49 loc) · 2.09 KB
/
i3nodes_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
from i3ipc import Connection
import pydot
import random
from sys import argv
import cv2
import numpy as np
random.seed(666)
def adjust_color(color, level):
if level == 0:
return "#FFFFFF"
rgb = int(color[1:3], 16), int(color[3:5], 16) , int(color[5:], 16)
print("rgb",rgb)
h, s, v = cv2.cvtColor(np.uint8([[rgb]]), cv2.COLOR_RGB2HSV)[0][0]
print("hsv",h,s,v)
# Génération de facteurs de réduction aléatoires
h_factor = h + 1 / (2 + level) * random.uniform(-1.0, 1.0)*179
s_factor = s + 1 / (2 + level) * random.uniform(-1.0, 1.0)*255
v_factor = v + 1 / (2 + level) * random.uniform(-1.0, 1.0)*255
print("factors",h_factor,s_factor,v_factor)
# Calcul des nouvelles valeurs HSV
h = float(max(1, min(179, h_factor))) # La teinte doit être entre 0 et 179
s = float(max(70, min(200, s_factor))) # La saturation doit être entre 0 et 255
v = float(max(150, min(200, v_factor))) # La valeur doit être entre 0 et 255
print("hsv2", h,s,v)
r, g, b = cv2.cvtColor(np.uint8([[[h, s, v]]]), cv2.COLOR_HSV2RGB)[0][0]
print("rgb2",r,g,b)
return f"#{r:02x}{g:02x}{b:02x}"
def add_nodes_edges(node, graph, parent_color="#FFFFFF", level=0):
name = node.name if node.name else "container"
name_parts = name.split('-')
name = name_parts[-1].strip()+f" ({node.layout})"
print(name, level)
parent_color = adjust_color(parent_color,level)
graph.add_node(pydot.Node(node.id, color=parent_color, fontcolor=parent_color, label=name))
for n in node.nodes:
child_color = adjust_color(parent_color,level)
add_nodes_edges(n, graph, child_color, level+1)
child_name = n.name if n.name else "container"
print(' '*level,node.id,n.id,name, child_name,child_color, parent_color)
graph.add_edge(pydot.Edge(node.id, n.id, color=child_color))
if __name__ == "__main__":
i3 = Connection()
tree = i3.get_tree()
G = pydot.Dot(graph_type='graph')
add_nodes_edges(tree, G)
G.set_graph_defaults(bgcolor="transparent")
G.write_png('i3_tree.png')
G.write_dot('i3_tree.dot')