Coverage for install/scipp/visualization/colors.py: 38%

24 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-04-28 01:28 +0000

1# SPDX-License-Identifier: BSD-3-Clause 

2# Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 

3# @author Neil Vaytet 

4 

5import numpy as np 

6 

7STYLE = { 

8 'attrs': '#ff5555', 

9 'coords': '#c6e590', 

10 'data': '#f6d028', 

11 'masks': '#c8c8c8', 

12} 

13 

14 

15def hex_to_rgb(hex_color): 

16 rgb_hex = [hex_color[x : x + 2] for x in [1, 3, 5]] 

17 return np.array([int(hex_value, 16) for hex_value in rgb_hex]) 

18 

19 

20def rgb_to_hex(rgb): 

21 hex_value = [] 

22 for i in rgb: 

23 h = hex(int(i))[2:] 

24 if len(h) < 2: 

25 h = "{}0".format(h) 

26 hex_value.append(h) 

27 return "#" + "".join(hex_value) 

28 

29 

30def make_random_color(fmt='rgb'): 

31 """ 

32 Generate a random color. 

33 Possible output formats are: 

34 

35 - 'rgb' (default): (255, 255, 100) 

36 - 'dec': (1.0, 1.0, 0.392) 

37 - 'rgba': (1.0, 1.0, 0.392, 1.0) 

38 - 'hex': #ffff64 

39 """ 

40 rgb = np.random.randint(0, 255, 3) 

41 if fmt == 'rgb': 

42 return rgb 

43 elif fmt == 'dec': 

44 return rgb.astype(np.float) / 255.0 

45 elif fmt == 'rgba': 

46 return np.concatenate((rgb.astype(np.float) / 255.0, [1.0])) 

47 elif fmt == 'hex': 

48 return rgb_to_hex(rgb) 

49 else: 

50 raise RuntimeError( 

51 "Unknown color format {}. Possible choices are: " 

52 "rgb, dec, rgba, and hex.".format(fmt) 

53 )