I've made this function gradient(iteration)
which returns the list of HSL values in order. Now the problem is I can't figure out how to calculate the step difference to keep the length of the list returned by the function = no of iteration. (len(gradient(1000)) == 1000)
Here is the gradient()
function:
def gradient(iteration):
"""This function returns a list of HSL values
of all the colors in an order."""
ops = { '+': lambda c, step: min(1.0, c + step),
'-': lambda c, step: max(0.0, c - step)}
index = 0
operation = '+'
rgb, _list = [1.0, 0.0, 0.0], []
combo = ((2,1,0), (2,0,1), (0,2,1), (0,1,2), (1,0,2), (1,2,0))
step = 1 if iteration > get_len(1/255) else int(get_len(1/255)/iteration)
step /= 200
for i in range(iteration):
if (rgb[combo[index][1]] == 1.0 and operation == '+') or \
(rgb[combo[index][1]] == 0.0 and operation == '-'):
operation = '-' if operation == '+' else '+'
index += 1
if len(combo)-1 <= index: break#index = 0
rgb[combo[index][1]] = ops[operation](rgb[combo[index][1]], step)
_list.append(rgb)
return _list
What I've tried so far:
I made a
get_len
function which will give the total length of the list given according to the step parameter. The plan is to try to find the exact step value by the value returned byget_len
.def get_len(step): c = 1 v = 1.0 while True: v = max(0.0, v - step) if v == 0.0: break c += 1 return c*5
I further tried to correct the calculation for
step
and got very close with this.step = 1 if iteration > get_len(1/255) else int(get_len(1/255)/iteration) step /= 200
To understand better what I'm trying to deal with, run this code
# i and len(gradient(i)) should be equal.
for i in range(1,100):
print(i, len(gradient(i)))
For example:-
Thie image is of 159 iterations.
This image is of 509 iterations.
Basically I'm trying to alter the length of the gradient bar but with any integer value given to the gradient(iteration)
function.