Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/bones/color.py: 11%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-16 22:16 +0000

1import string 

2from viur.core.bones.base import BaseBone, ReadFromClientError, ReadFromClientErrorSeverity 

3 

4 

5class ColorBone(BaseBone): 

6 r""" 

7 ColorBone is a custom bone class for storing color values in the ViUR framework. 

8 It inherits from the BaseBone class in the viur.core.bones.base module. 

9 

10 :param type: A string representing the bone type, set to "color". 

11 :param mode: A string specifying the color mode, either "rgb" or "rgba". Default is "rgb". 

12 :param \**kwargs: Additional keyword arguments passed to the BaseBone constructor. 

13 """ 

14 type = "color" 

15 

16 def __init__(self, *, mode="rgb", **kwargs): # mode rgb/rgba 

17 super().__init__(**kwargs) 

18 assert mode in {"rgb", "rgba"} 

19 self.mode = mode 

20 

21 def singleValueFromClient(self, value, skel, bone_name, client_data): 

22 value = value.lower() 

23 if value.count("#") > 1: 

24 return self.getEmptyValue(), [ 

25 ReadFromClientError(ReadFromClientErrorSeverity.Invalid, "Invalid value entered")] 

26 for char in value: 

27 if char not in string.hexdigits + "#": 

28 return self.getEmptyValue(), [ 

29 ReadFromClientError(ReadFromClientErrorSeverity.Invalid, "Invalid value entered")] 

30 if self.mode == "rgb": 

31 if len(value) == 3: 

32 value = "#" + value 

33 if len(value) == 4: 

34 value = value[0:2] + value[1] + 2 * value[2] + 2 * value[3] 

35 if len(value) == 6 or len(value) == 7: 

36 if len(value) == 6: 

37 value = "#" + value 

38 else: 

39 return self.getEmptyValue(), [ 

40 ReadFromClientError(ReadFromClientErrorSeverity.Invalid, "Invalid value entered")] 

41 if self.mode == "rgba": 

42 if len(value) == 8 or len(value) == 9: 

43 if len(value) == 8: 

44 value = "#" + value 

45 else: 

46 return self.getEmptyValue(), [ 

47 ReadFromClientError(ReadFromClientErrorSeverity.Invalid, "Invalid value entered")] 

48 err = self.isInvalid(value) 

49 if not err: 

50 return value, None 

51 return self.getEmptyValue(), [ReadFromClientError(ReadFromClientErrorSeverity.Invalid, err)]