Data Visualization: Build a sequential palette

A sequential palette blends evenly from a light colour to a dark one. Interpolate it channel by channel.

What to build

A program that prints n colours evenly spaced between two hex colours.

Requirements

  • For i from 0 to n − 1, interpolate each of the red, green and blue channels linearly from the first colour to the second and round to the nearest whole number, halves up.
  • Print the n colours as upper-case #RRGGBB values on one line, separated by spaces.

Input

One line: the first colour, the second colour and n.

Output

One line.

Constraints

2 ≤ n ≤ 20; colours are valid six-digit hex values in upper or lower case.

Example

Input:

#000000 #FFFFFF 3

Output:

#000000 #808080 #FFFFFF

Why it works

Moving every channel by the same fraction at each step gives colours spaced evenly between the two ends, and rounding returns each channel to a whole byte that hex can write.

Examples

Example 1 · Input
#000000 #FFFFFF 3
Expected output
#000000 #808080 #FFFFFF
Example 2 · Input
#1C3028 #EDE9DF 2
Expected output
#1C3028 #EDE9DF

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

For step i of n, each channel is a + (b − a) × i ÷ (n − 1).

Hint 2

Round halves up with int(x + 0.5).

Hint 3

f"{value:02X}" writes a channel as two upper-case hex digits.

Solution

Show one correct solution (try it yourself first)
a, b, n = input().split()
n = int(n)
ca = [int(a[i:i + 2], 16) for i in (1, 3, 5)]
cb = [int(b[i:i + 2], 16) for i in (1, 3, 5)]
out = []
for i in range(n):
    rgb = [int(x + (y - x) * i / (n - 1) + 0.5) for x, y in zip(ca, cb)]
    out.append("#" + "".join(f"{v:02X}" for v in rgb))
print(" ".join(out))

Lesson: Colour and accessibility →