+17
-9
python/sep30/level4/blendColors.py
+17
-9
python/sep30/level4/blendColors.py
···
1
-
def blendComponent(c1: int, c2: int) -> int:
1
+
COMPONENT_LENGTH = 3
2
+
3
+
4
+
def blend(c1: int, c2: int) -> int:
2
5
"""Get the median of 2 numbers."""
3
6
return round((c1 + c2) / 2)
4
7
5
8
9
+
def splitColors(rgb: int) -> tuple[int, int, int]:
10
+
"""Split an RGB value into its components."""
11
+
(r, g, b) = [int(str(rgb).zfill(3*COMPONENT_LENGTH)[i : i + COMPONENT_LENGTH]) for i in range(0, 3*COMPONENT_LENGTH, COMPONENT_LENGTH)]
12
+
return (r, g, b)
13
+
14
+
6
15
def blendColors(rgb1: int, rgb2: int) -> int:
7
16
"""Blend two colors represented as RGB values."""
8
-
# Fill values and split into individual components
9
-
(r1, g1, b1) = [int(str(rgb1).zfill(9)[i : i + 3]) for i in range(0, 9, 3)]
10
-
(r2, g2, b2) = [int(str(rgb2).zfill(9)[i : i + 3]) for i in range(0, 9, 3)]
11
-
# Calculate the average of each color component
17
+
(r1, g1, b1) = splitColors(rgb1)
18
+
(r2, g2, b2) = splitColors(rgb2)
12
19
(r3, g3, b3) = [
13
-
blendComponent(r1, r2),
14
-
blendComponent(g1, g2),
15
-
blendComponent(b1, b2),
20
+
blend(r1, r2),
21
+
blend(g1, g2),
22
+
blend(b1, b2),
16
23
]
17
-
(final_r, final_g, final_b) = [str(x).zfill(3) for x in (r3, g3, b3)]
24
+
# Pad with leading zeros
25
+
(final_r, final_g, final_b) = [str(x).zfill(COMPONENT_LENGTH) for x in (r3, g3, b3)]
18
26
# Compose components and cast to integer
19
27
return int(final_r + final_g + final_b)
20
28