def hex_to_rgb(hex_color: str) -> list:
"""Generate rgb color from hex.
Args:
hex_color: str - color for parsing.
Returns:
list: r, g, and b shades.
"""
hex_color = hex_color.lstrip('#')
bits = 16
return [int(hex_color[bit:bit+2], bits) for bit in (0, 2, 4)]
def linear_gradient(start_hex: str, finish_hex: str, steps: int = 10) -> dict:
"""Generate linear gradient.
Args:
start_hex: str - gradient start color.
finish_hex: str - gradient end color.
steps: int, optional - number of colors in the gradient. Defaults to 10.
Returns:
dict: three lists under the keys r, g and b, storing the number of values \
specified in steps for each color.
"""
start = hex_to_rgb(start_hex)
finish = hex_to_rgb(finish_hex)
factor = steps-1
if factor <= 0:
return {'r': [finish[0]], 'g': [finish[1]], 'b': [finish[2]]}
lin_grad = []
for shade in range(3):
component_values = []
for step in range(steps):
coefficient = (finish[shade] - start[shade]) * step / factor
component_values.append(int(start[shade] + coefficient))
lin_grad.append(component_values)
return {'r': lin_grad[0], 'g': lin_grad[1], 'b': lin_grad[2]}
start_color = '#2E8B57'
finish_color = '#C71585'
gradient_result = linear_gradient(start_color, finish_color, 100)
Результат:
Код можно сократить, но я делал под flake8.