tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
fix: more cleanup
thecoded.prof
5 months ago
c0182355
0e19555c
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+17
-9
1 changed file
expand all
collapse all
unified
split
python
sep30
level4
blendColors.py
+17
-9
python/sep30/level4/blendColors.py
···
1
1
-
def blendComponent(c1: int, c2: int) -> int:
1
1
+
COMPONENT_LENGTH = 3
2
2
+
3
3
+
4
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
9
+
def splitColors(rgb: int) -> tuple[int, int, int]:
10
10
+
"""Split an RGB value into its components."""
11
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
12
+
return (r, g, b)
13
13
+
14
14
+
6
15
def blendColors(rgb1: int, rgb2: int) -> int:
7
16
"""Blend two colors represented as RGB values."""
8
8
-
# Fill values and split into individual components
9
9
-
(r1, g1, b1) = [int(str(rgb1).zfill(9)[i : i + 3]) for i in range(0, 9, 3)]
10
10
-
(r2, g2, b2) = [int(str(rgb2).zfill(9)[i : i + 3]) for i in range(0, 9, 3)]
11
11
-
# Calculate the average of each color component
17
17
+
(r1, g1, b1) = splitColors(rgb1)
18
18
+
(r2, g2, b2) = splitColors(rgb2)
12
19
(r3, g3, b3) = [
13
13
-
blendComponent(r1, r2),
14
14
-
blendComponent(g1, g2),
15
15
-
blendComponent(b1, b2),
20
20
+
blend(r1, r2),
21
21
+
blend(g1, g2),
22
22
+
blend(b1, b2),
16
23
]
17
17
-
(final_r, final_g, final_b) = [str(x).zfill(3) for x in (r3, g3, b3)]
24
24
+
# Pad with leading zeros
25
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