Coverage for src / cosmic_toolbox / colors.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-31 12:38 +0000

1""" 

2Color utilities for matplotlib plots. 

3 

4Provides custom color cycles and utilities for setting matplotlib color schemes. 

5""" 

6 

7import matplotlib.pyplot as plt 

8from cycler import cycler 

9 

10 

11def get_colors(cycle="silvan"): 

12 """ 

13 Get a dictionary of named colors for a given color cycle. 

14 

15 :param cycle: Name of the color cycle to use. Currently only "silvan" is 

16 supported. If a different value is passed, it is returned as-is. 

17 :type cycle: str 

18 :return: Dictionary mapping color names to hex color codes, or the input 

19 value if not a recognized cycle name. 

20 :rtype: dict or any 

21 """ 

22 if cycle == "silvan": 

23 return { 

24 "b": "#0063B9", 

25 "r": "#BA1A13", 

26 "g": "#34BA09", 

27 "orange": "#ED6018", 

28 "violet": "#6E13BA", 

29 "brown": "#6E2C0B", 

30 "gray": "#808080", 

31 "pink": "#BA13A0", 

32 "olive": "#B0BA13", 

33 "bluegreen": "#09BAAF", 

34 "lightblue": "#1994FF", 

35 "lightred": "#FF3D33", 

36 "lightgreen": "#5CFF26", 

37 "lightorange": "#FFA547", 

38 "lightviolet": "#A333FF", 

39 "lightbrown": "#FFA541", 

40 "lightgray": "#BEBEBE", 

41 "lightpink": "#FF33E0", 

42 "lightolive": "#F1FF33", 

43 "lightbluegreen": "#33FFE7", 

44 } 

45 else: 

46 return cycle 

47 

48 

49def set_cycle(cycle="silvan"): 

50 """ 

51 Set the matplotlib color cycle to a predefined color scheme. 

52 

53 :param cycle: Name of the color cycle to use, or a list/dict of colors. 

54 If "silvan", uses the predefined Silvan color scheme. 

55 :type cycle: str or list or dict 

56 """ 

57 colors = get_colors(cycle=cycle) 

58 col = colors.values() if isinstance(colors, dict) else colors 

59 plt.rc("axes", prop_cycle=cycler(color=col))