Skip to content

Commit 428f63a

Browse files
committed
feat: enhance ColorV1 with from_list and from_hex methods; update color field validation in models
1 parent f370d03 commit 428f63a

File tree

8 files changed

+241
-202
lines changed

8 files changed

+241
-202
lines changed

deadlock_assets_api/models/v1/colors.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Self
2+
13
from pydantic import BaseModel, ConfigDict, Field
24

35

@@ -8,3 +10,31 @@ class ColorV1(BaseModel):
810
green: int = Field(..., description="The green value of the color.", ge=0, le=255)
911
blue: int = Field(..., description="The blue value of the color.", ge=0, le=255)
1012
alpha: int = Field(..., description="The alpha value of the color.", ge=0, le=255)
13+
14+
@classmethod
15+
def from_list(cls, color: list[int]) -> Self:
16+
if len(color) == 3:
17+
r, g, b = color
18+
a = 255
19+
elif len(color) == 4:
20+
r, g, b, a = color
21+
else:
22+
raise ValueError("Color must be a tuple or list of 3 or 4 integers.")
23+
return cls(red=r, green=g, blue=b, alpha=a)
24+
25+
@classmethod
26+
def from_hex(cls, hex_str: str) -> Self:
27+
hex_str = hex_str.lstrip("#")
28+
if len(hex_str) == 6:
29+
r = int(hex_str[0:2], 16)
30+
g = int(hex_str[2:4], 16)
31+
b = int(hex_str[4:6], 16)
32+
a = 255
33+
elif len(hex_str) == 8:
34+
r = int(hex_str[0:2], 16)
35+
g = int(hex_str[2:4], 16)
36+
b = int(hex_str[4:6], 16)
37+
a = int(hex_str[6:8], 16)
38+
else:
39+
raise ValueError("Hex string must be in the format RRGGBB or RRGGBBAA.")
40+
return cls(red=r, green=g, blue=b, alpha=a)

deadlock_assets_api/models/v1/generic_data.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
from functools import lru_cache
33

4-
from pydantic import BaseModel, ConfigDict, Field
4+
from pydantic import BaseModel, ConfigDict, Field, field_validator
5+
6+
from deadlock_assets_api.models.v1.colors import ColorV1
57

68

79
class FlashDataV1(BaseModel):
@@ -11,11 +13,22 @@ class FlashDataV1(BaseModel):
1113
coverage: float = Field(..., validation_alias="m_flCoverage")
1214
hardness: float = Field(..., validation_alias="m_flHardness")
1315
brightness: float = Field(..., validation_alias="m_flBrightness")
14-
color: list[int] = Field(..., validation_alias="m_Color")
16+
color: ColorV1 = Field(..., validation_alias="m_Color")
1517
brightness_in_light_sensitivity_mode: float | None = Field(
1618
None, validation_alias="m_flBrightnessInLightSensitivityMode"
1719
)
1820

21+
@field_validator("color", mode="before")
22+
@classmethod
23+
def validate_colors(cls, v: ColorV1 | list[int] | dict[str, int]) -> ColorV1:
24+
if isinstance(v, ColorV1):
25+
return v
26+
if isinstance(v, dict):
27+
return ColorV1.model_validate(v)
28+
if isinstance(v, list):
29+
return ColorV1.from_list(v)
30+
raise TypeError(f"Invalid type for color field: {type(v)}")
31+
1932

2033
class DamageFlashV1(BaseModel):
2134
model_config = ConfigDict(populate_by_name=True)

deadlock_assets_api/models/v1/map.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
LANES,
1212
MAP_RADIUS,
1313
)
14+
from deadlock_assets_api.models.v1.colors import ColorV1
1415

1516
TOWER_IDS = {
1617
**{f"#Team{team_id + 1}Core": f"team{team_id}_core" for team_id in range(2)},
@@ -116,6 +117,11 @@ def from_pathnodes(cls, pathnodes: list[list[float]], **kwargs) -> ZiplanePathV1
116117
**kwargs,
117118
)
118119

120+
@computed_field
121+
@property
122+
def color_parsed(self) -> ColorV1:
123+
return ColorV1.from_hex(self.color)
124+
119125

120126
class MapV1(BaseModel):
121127
model_config = ConfigDict(populate_by_name=True)

deadlock_assets_api/models/v2/npc_unit.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,7 @@ def validate_colors(cls, v: ColorV1 | list[int] | None | dict[str, int]) -> Colo
130130
if isinstance(v, ColorV1):
131131
return v
132132
if isinstance(v, dict):
133-
try:
134-
return ColorV1.model_validate(v)
135-
except ValueError as e:
136-
LOGGER.error(f"Error parsing color {v}: {e}")
137-
raise
133+
return ColorV1.model_validate(v)
138134
if isinstance(v, list):
139-
try:
140-
color = parse_color(v)
141-
return color
142-
except ValueError as e:
143-
LOGGER.error(f"Error parsing color {v}: {e}")
144-
raise
135+
return ColorV1.from_list(v)
145136
raise TypeError(f"Invalid type for color field: {type(v)}")
146-
147-
148-
def parse_color(color: list[int]) -> ColorV1:
149-
if len(color) == 3:
150-
r, g, b = color
151-
a = 255
152-
elif len(color) == 4:
153-
r, g, b, a = color
154-
else:
155-
raise ValueError("Color must be a tuple or list of 3 or 4 integers.")
156-
return ColorV1(red=r, green=g, blue=b, alpha=a)

res/builds/5972/v2/generic_data.json

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,64 @@
33
"bullet_damage": {
44
"brightness": 1.2,
55
"brightness_in_light_sensitivity_mode": null,
6-
"color": [
7-
255,
8-
0,
9-
0
10-
],
6+
"color": {
7+
"alpha": 255,
8+
"blue": 0,
9+
"green": 0,
10+
"red": 255
11+
},
1112
"coverage": 0.5,
1213
"duration": 0.25,
1314
"hardness": 0.7
1415
},
1516
"crit_damage": {
1617
"brightness": 1.7,
1718
"brightness_in_light_sensitivity_mode": null,
18-
"color": [
19-
255,
20-
255,
21-
0
22-
],
19+
"color": {
20+
"alpha": 255,
21+
"blue": 0,
22+
"green": 255,
23+
"red": 255
24+
},
2325
"coverage": 0.6,
2426
"duration": 0.33,
2527
"hardness": 0.9
2628
},
2729
"healing_damage": {
2830
"brightness": 1.2,
2931
"brightness_in_light_sensitivity_mode": 0.6,
30-
"color": [
31-
0,
32-
195,
33-
45
34-
],
32+
"color": {
33+
"alpha": 255,
34+
"blue": 45,
35+
"green": 195,
36+
"red": 0
37+
},
3538
"coverage": 0.5,
3639
"duration": 1.0,
3740
"hardness": 0.0
3841
},
3942
"melee_damage": {
4043
"brightness": 3.0,
4144
"brightness_in_light_sensitivity_mode": null,
42-
"color": [
43-
254,
44-
101,
45-
40
46-
],
45+
"color": {
46+
"alpha": 255,
47+
"blue": 40,
48+
"green": 101,
49+
"red": 254
50+
},
4751
"coverage": 0.5,
4852
"duration": 0.275,
4953
"hardness": 0.0
5054
},
5155
"tech_damage": {
5256
"brightness": 1.5,
5357
"brightness_in_light_sensitivity_mode": null,
54-
"color": [
55-
35,
56-
17,
57-
236
58-
],
58+
"color": {
59+
"alpha": 255,
60+
"blue": 236,
61+
"green": 17,
62+
"red": 35
63+
},
5964
"coverage": 0.5,
6065
"duration": 0.25,
6166
"hardness": 0.5

0 commit comments

Comments
 (0)