Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/bones/color.py: 12%
34 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-13 11:04 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-13 11:04 +0000
1import string
2from viur.core import i18n
3from .base import BaseBone, ReadFromClientError, ReadFromClientErrorSeverity
6class ColorBone(BaseBone):
7 r"""
8 ColorBone is a custom bone class for storing color values in the ViUR framework.
9 It inherits from the BaseBone class in the viur.core.bones.base module.
11 :param type: A string representing the bone type, set to "color".
12 :param mode: A string specifying the color mode, either "rgb" or "rgba". Default is "rgb".
13 :param \**kwargs: Additional keyword arguments passed to the BaseBone constructor.
14 """
15 type = "color"
17 def __init__(self, *, mode="rgb", **kwargs): # mode rgb/rgba
18 super().__init__(**kwargs)
19 assert mode in {"rgb", "rgba"}
20 self.mode = mode
22 def singleValueFromClient(self, value, skel, bone_name, client_data):
23 value = value.lower()
25 if value.count("#") > 1:
26 return self.getEmptyValue(), [
27 ReadFromClientError(ReadFromClientErrorSeverity.Invalid)]
29 for char in value:
30 if char not in string.hexdigits + "#":
31 return self.getEmptyValue(), [
32 ReadFromClientError(ReadFromClientErrorSeverity.Invalid)]
34 if self.mode == "rgb":
35 if len(value) == 3:
36 value = "#" + value
37 if len(value) == 4:
38 value = value[0:2] + value[1] + 2 * value[2] + 2 * value[3]
39 if len(value) == 6 or len(value) == 7:
40 if len(value) == 6:
41 value = "#" + value
42 else:
43 return self.getEmptyValue(), [
44 ReadFromClientError(ReadFromClientErrorSeverity.Invalid)]
46 if self.mode == "rgba":
47 if len(value) == 8 or len(value) == 9:
48 if len(value) == 8:
49 value = "#" + value
50 else:
51 return self.getEmptyValue(), [
52 ReadFromClientError(ReadFromClientErrorSeverity.Invalid)]
54 err = self.isInvalid(value)
55 if not err:
56 return value, None
58 return self.getEmptyValue(), [ReadFromClientError(ReadFromClientErrorSeverity.Invalid, err)]