Coverage for plotting/graph.py: 0%

14 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-16 22:49 +1300

1""" 

2Automatic Colour Conversion Graph Plotting 

3========================================== 

4 

5Define the automatic colour conversion graph plotting objects. 

6 

7- :func:`colour.plotting.plot_automatic_colour_conversion_graph` 

8""" 

9 

10from __future__ import annotations 

11 

12import os 

13import typing 

14 

15import colour 

16from colour.graph import CONVERSION_GRAPH_NODE_LABELS, describe_conversion_path 

17 

18if typing.TYPE_CHECKING: 

19 from colour.hints import Literal 

20 

21from colour.hints import cast 

22from colour.utilities import required, validate_method 

23 

24__author__ = "Colour Developers" 

25__copyright__ = "Copyright 2013 Colour Developers" 

26__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" 

27__maintainer__ = "Colour Developers" 

28__email__ = "colour-developers@colour-science.org" 

29__status__ = "Production" 

30 

31__all__ = [ 

32 "plot_automatic_colour_conversion_graph", 

33] 

34 

35 

36@required("Pydot") 

37@required("NetworkX") 

38def plot_automatic_colour_conversion_graph( 

39 filename: str, 

40 prog: Literal["circo", "dot", "fdp", "neato", "nop", "twopi"] | str = "fdp", 

41) -> Dot: # pyright: ignore # noqa: F821 # pragma: no cover 

42 """ 

43 Plot *Colour* automatic colour conversion graph using 

44 `Graphviz <https://www.graphviz.org>`__ and 

45 `pyraphviz <https://pygraphviz.github.io>`__. 

46 

47 Parameters 

48 ---------- 

49 filename 

50 Filename to use to save the image. 

51 prog 

52 *Graphviz* layout method. 

53 

54 Returns 

55 ------- 

56 :class:`pydot.Dot` 

57 *Pydot* graph. 

58 

59 Notes 

60 ----- 

61 - This definition does not directly plot the *Colour* automatic 

62 colour conversion graph but instead writes it to an image. 

63 

64 Examples 

65 -------- 

66 >>> import tempfile 

67 >>> import colour 

68 >>> from colour import read_image 

69 >>> from colour.plotting import plot_image 

70 >>> filename = "{0}.png".format(tempfile.mkstemp()[-1]) 

71 >>> _ = plot_automatic_colour_conversion_graph(filename, "dot") 

72 ... # doctest: +SKIP 

73 >>> plot_image(read_image(filename)) # doctest: +SKIP 

74 

75 .. image:: ../_static/Plotting_Plot_Colour_Automatic_Conversion_Graph.png 

76 :align: center 

77 :alt: plot_automatic_colour_conversion_graph 

78 """ 

79 

80 import networkx as nx # noqa: PLC0415 

81 

82 prog = validate_method( 

83 prog, 

84 ("circo", "dot", "fdp", "neato", "nop", "twopi"), 

85 '"{0}" program is invalid, it must be one of {1}!', 

86 ) 

87 

88 # TODO: Investigate API to trigger the conversion graph build. 

89 describe_conversion_path("RGB", "RGB", print_callable=lambda x: x) 

90 

91 dot = nx.drawing.nx_pydot.to_pydot( 

92 cast("nx.DiGraph", colour.graph.CONVERSION_GRAPH) 

93 ) 

94 

95 for node in dot.get_nodes(): 

96 label = CONVERSION_GRAPH_NODE_LABELS.get(node.get_name()) 

97 

98 if label is None: 

99 continue 

100 

101 node.set_label(label) 

102 node.set_style("filled") 

103 node.set_shape("circle") 

104 node.set_color("#2196F3FF") 

105 node.set_fillcolor("#2196F370") 

106 node.set_fontname("Helvetica") 

107 node.set_fontcolor("#263238") 

108 

109 for name in ("CIE XYZ", "RGB", "Spectral Distribution"): 

110 node = next(iter(dot.get_node(name.lower()))) 

111 

112 node.set_shape("doublecircle") 

113 node.set_color("#673AB7FF") 

114 node.set_fillcolor("#673AB770") 

115 node.set_fontsize(30) 

116 

117 for name in ( 

118 "ATD95", 

119 "CAM16", 

120 "CIECAM02", 

121 "Hellwig 2022", 

122 "Hunt", 

123 "Kim 2009", 

124 "LLAB", 

125 "Nayatani95", 

126 "RLAB", 

127 "ZCAM", 

128 ): 

129 node = next(iter(dot.get_node(name.lower()))) 

130 

131 node.set_color("#00BCD4FF") 

132 node.set_fillcolor("#00BCD470") 

133 

134 for edge in dot.get_edges(): 

135 edge.set_color("#26323870") 

136 

137 file_format = os.path.splitext(filename)[-1][1:] 

138 write_method = getattr(dot, f"write_{file_format}") 

139 write_method(filename, prog=prog, f=file_format) 

140 

141 return dot