@@ -66,27 +66,22 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]:
66
66
nColors where the colors are linearly interpolated between the
67
67
(r, g, b) tuples that are given.
68
68
"""
69
- allColors : List [str ] = []
70
- # Separate (R, G, B) pairs.
71
- for start , end in zip (hexList [:- 1 ], hexList [1 :]):
72
- # Linearly interpolate between pair of hex ###### values and
73
- # add to list.
74
- start = [int (start [i :i + 2 ], 16 ) for i in (1 , 3 , 5 )]
75
- end = [int (end [i :i + 2 ], 16 ) for i in (1 , 3 , 5 )]
76
-
77
- n_interpolate = 765
78
- for i in range (n_interpolate ):
79
- frac = i / (n_interpolate - 1 )
80
- byte_ints = [int (x + (y - x ) * frac ) for x , y in zip (start , end )]
81
- hexs = [hex (x )[2 :].zfill (2 ) for x in byte_ints ]
82
- allColors .append ("#" + "" .join (hexs ))
83
-
84
- # Pick only nColors colors from the total list.
69
+ input_color_bytes = [[int (_hex [i :i + 2 ], 16 ) for i in (1 , 3 , 5 )] for _hex in hexList ]
85
70
result : List [str ] = []
86
- for counter in range (nColors ):
87
- fraction = float (counter ) / (nColors - 1 )
88
- index = int (fraction * (len (allColors ) - 1 ))
89
- result .append (allColors [index ])
71
+ step_size = (len (hexList ) - 1 ) / (nColors - 1 )
72
+ step = 0
73
+ idx = 0
74
+ while len (result ) < nColors - 1 :
75
+ start , end = input_color_bytes [idx ], input_color_bytes [idx + 1 ]
76
+ new_color_bytes = [int (x + step * (y - x )) for x , y in zip (start , end )]
77
+ new_color_hexs = [hex (x )[2 :].zfill (2 ) for x in new_color_bytes ]
78
+ result .append ("#" + "" .join (new_color_hexs ))
79
+ step += step_size
80
+ if step > 1 :
81
+ step -= 1
82
+ idx += 1
83
+
84
+ result .append (hexList [- 1 ].lower ())
90
85
return result
91
86
92
87
0 commit comments