Coverage for fastagency/ui/mesop/components/helpers.py: 80%

8 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-19 12:16 +0000

1def darken_hex_color(hex_color: str, factor: float = 0.8) -> str: 1abc

2 """Darkens a hex color by a given factor. 

3 

4 Args: 

5 hex_color: The hex color code (e.g., '#FF0000'). 

6 factor: The darkening factor (0.0 to 1.0, where 1.0 is no change and 0.0 is completely dark). 

7 

8 Returns: 

9 The darkened hex color code. 

10 """ 

11 # Remove the '#' prefix if it exists 

12 hex_color = hex_color.lstrip("#") 1abc

13 

14 if len(hex_color) == 3: 14 ↛ 15line 14 didn't jump to line 15 because the condition on line 14 was never true1abc

15 hex_color = "".join(char * 2 for char in hex_color) 

16 

17 # Convert hex to RGB values 

18 rgb = tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4)) 1abc

19 

20 # Darken each component 

21 darkened_rgb = tuple(int(channel * factor) for channel in rgb) 1abc

22 

23 # Convert back to hex 

24 darkened_hex = "#{:02X}{:02X}{:02X}".format(*darkened_rgb) 1abc

25 

26 return darkened_hex 1abc