@@ -1058,51 +1058,73 @@ def process_constrain(self, name: str, tokens: list[Token]) -> None:
10581058 self .styles ._rules [name ] = value # type: ignore
10591059
10601060 def process_hatch (self , name : str , tokens : list [Token ]) -> None :
1061- character = " "
1061+ if not tokens :
1062+ return
1063+ character : str | None = None
10621064 color = TRANSPARENT
10631065 opacity = 1.0
10641066
1065- for token in tokens :
1066- if token .name == "token" :
1067- if token .value not in VALID_HATCH :
1068- self .error (
1069- name ,
1070- tokens [0 ],
1071- string_enum_help_text (name , VALID_HATCH , context = "css" ),
1072- )
1073- character = HATCHES [token .value ]
1074- elif token .name == "string" :
1075- character = token .value [1 :- 1 ]
1076- if len (character ) != 1 :
1077- self .error (
1078- name ,
1079- token ,
1080- f"Hatch requires a string of length 1; got { token .value } " ,
1081- )
1082- if cell_len (character ) != 1 :
1083- self .error (
1084- name ,
1085- token ,
1086- f"Hatch requires a string with a *cell length* of 1; got { token .value } " ,
1087- )
1088- elif token .name == "color" :
1089- try :
1090- color = Color .parse (token .value )
1091- except Exception as error :
1092- self .error (
1093- name ,
1094- token ,
1095- color_property_help_text (name , context = "css" , error = error ),
1096- )
1097- elif token .name == "scalar" :
1098- opacity_scalar = opacity = Scalar .parse (token .value )
1067+ if len (tokens ) not in (2 , 3 ):
1068+ self .error (name , tokens [0 ], "2 or 3 values expected here" )
1069+
1070+ character_token , color_token , * opacity_tokens = tokens
1071+
1072+ if character_token .name == "token" :
1073+ if character_token .value not in VALID_HATCH :
1074+ self .error (
1075+ name ,
1076+ tokens [0 ],
1077+ string_enum_help_text (name , VALID_HATCH , context = "css" ),
1078+ )
1079+ character = HATCHES [character_token .value ]
1080+ elif character_token .name == "string" :
1081+ character = character_token .value [1 :- 1 ]
1082+ if len (character ) != 1 :
1083+ self .error (
1084+ name ,
1085+ character_token ,
1086+ f"Hatch type requires a string of length 1; got { character_token .value } " ,
1087+ )
1088+ if cell_len (character ) != 1 :
1089+ self .error (
1090+ name ,
1091+ character_token ,
1092+ f"Hatch type requires a string with a *cell length* of 1; got { character_token .value } " ,
1093+ )
1094+
1095+ if color_token .name in ("color" , "token" ):
1096+ try :
1097+ color = Color .parse (color_token .value )
1098+ except Exception as error :
1099+ self .error (
1100+ name ,
1101+ color_token ,
1102+ color_property_help_text (name , context = "css" , error = error ),
1103+ )
1104+ else :
1105+ self .error (
1106+ name , color_token , f"Expected a color; found { color_token .value !r} "
1107+ )
1108+
1109+ if opacity_tokens :
1110+ opacity_token = opacity_tokens [0 ]
1111+ if opacity_token .name == "scalar" :
1112+ opacity_scalar = opacity = Scalar .parse (opacity_token .value )
10991113 if opacity_scalar .unit != Unit .PERCENT :
11001114 self .error (
1101- name , token , "hatch alpha must be given as a percentage."
1115+ name ,
1116+ opacity_token ,
1117+ "hatch alpha must be given as a percentage." ,
11021118 )
11031119 opacity = clamp (opacity_scalar .value / 100.0 , 0 , 1.0 )
1120+ else :
1121+ self .error (
1122+ name ,
1123+ opacity_token ,
1124+ f"expected a percentage here; found { opacity_token .value !r} " ,
1125+ )
11041126
1105- self .styles ._rules [name ] = (character , color .multiply_alpha (opacity ))
1127+ self .styles ._rules [name ] = (character or " " , color .multiply_alpha (opacity ))
11061128
11071129 def _get_suggested_property_name_for_rule (self , rule_name : str ) -> str | None :
11081130 """
0 commit comments