Coverage for src/colorspace/cmap.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-29 15:11 +0000

1 

2 

3 

4def cmap_to_sRGB(x, n = None): 

5 """Convert Matplotlib Colormap to sRGB Color Object 

6 

7 This function is for internal use, e.g., when providing 

8 a `LinearSegmentedColormap` (cmap) to `specplot()`. 

9 Internally, the cmap will be converted to an sRGB colorobject 

10 used to extract the required information for plotting. 

11 

12 Args: 

13 x (LinearSegmentedColormap, ListedColormap): matplotlib cmap. 

14 n (None, int): None or integer >= 2. 

15 

16 Raises: 

17 TypeError: If argument `x` is not LinearSegmentedColormap or ListedColormap. 

18 TypeError: If `n` is not None or int. 

19 ValueError: If `n` is smaller or equal to 1. 

20 

21 Return: 

22 sRGB: A colorobject of class `sRGB`. 

23 """ 

24 

25 from colorspace.palettes import palette 

26 from matplotlib.colors import LinearSegmentedColormap, ListedColormap 

27 from numpy import linspace 

28 from colorspace.colorlib import sRGB 

29 

30 if not isinstance(x, (LinearSegmentedColormap, ListedColormap)): 

31 raise TypeError("argument `x` must be LinearSegmentedColormap or ListedColormap") 

32 

33 if not isinstance(n, (type(None), int)): 

34 raise TypeError("argument `n` must be None or int") 

35 if isinstance(n, int) and n <= 1: 

36 raise ValueError("argument `n` must be > 1") 

37 

38 # Taking 'N' from colormap 

39 if n is None: n = x.N 

40 

41 at = linspace(0., 1., n) 

42 tmp = x(at).transpose() 

43 sRGB = sRGB(R = tmp[0], G = tmp[1], B = tmp[2]) 

44 

45 return sRGB 

46 

47