Gradient function that returns a list of all colors in order I've made this Function gradient() that returns a list of rbg of all the colors in an order.
Issues:-

What should be the condition for while loop to stop the loop. Though b == 0 in the last elif block works fine.

How can I reduce the code of the function keeping the functionality the same. Like if there is any better approach to doing this.

Also, How can I calculate the length of the rbg_list just with the value of gap parameter without running the function. Let's say the length of the rbg_list will be 1529 if gap=1 so I can iterate it with range(1529).



gradient() function:-
def gradient(gap=1):
    r, g, b = 255, 0, 0

    if gap&lt;=0: 
        return (r, g, b)

    rbg_list = []
    while True:
        if  r == 255 and g &gt;= 0 and g &lt; 255 and b == 0:           # 1
            g += gap
            if g &gt; 255:
                g = 255

        elif r &lt;= 255 and g == 255 and r &gt; 0 and b == 0:          # 2
            r -= gap
            if r &lt; 0:
                r = 0
        
        elif r == 0 and g == 255 and b &lt; 255 and b &gt;= 0:          # 3
            b += gap
            if b &gt; 255:
                b = 255
        
        elif r == 0 and g &lt;= 255 and g &gt; 0 and b == 255:          # 4
            g -= gap
            if g &lt; 0:
                g = 0
        
        elif r &gt;= 0 and g == 0 and r &lt; 255 and b == 255:          # 5
            r += gap
            if r &gt; 255:
                r = 255
        
        elif r == 255 and g == 0 and b &gt; 0 and b &lt;= 255:          # 6
            b -= gap
            if b &lt; 0:
                b = 0
                
            if b == 0: break
        
        # print(r, g, b)
        rbg_list.append((r, g, b))
    return rbg_list

print(gradient(5))

Note:- I want the function to be compatible with both Python 2 and Python 3
I rarely do python nowadays, but cleanup of the conditions would be:
def add(c, gap):
    return min(255, c + gap)
   
def sub(c, gap):
    return max(0, c - gap)

def gradient(gap=1):
    r, g, b = 255, 0, 0

    if gap&lt;=0: 
        return (r, g, b)

    rbg_list = []
    while True:
        if  r == 255 and g &lt; 255 and b == 0:         # 1
            g = add(g, gap)

        elif r &gt; 0 and g == 255 and b == 0:          # 2
            r = sub(r, gap)
        
        elif r == 0 and g == 255 and b &lt; 255:        # 3
            b = add(b, gap)
        
        elif r == 0 and g &gt; 0 and b == 255:          # 4
            g = sub(g, gap)
        
        elif r &lt; 255 and g == 0 and b == 255:        # 5
            r = add(r, gap)
        
        elif r == 255 and g == 0 and b &gt; 0:          # 6
            b = sub(b, gap)
                
            if b == 0: break
        
        # print(r, g, b)
        rbg_list.append((r, g, b))
    return rbg_list
    

Now let me understand what happens. Let us on paper name the 3 states for a color component x (r, g or b):
    a = x == 0
    b = 0 &lt; x &lt; 255
    c = x == 255 
    
    a :   == 0
    bc :  &gt; 0
    c :   == 255
    ab :  &lt; 255
    
    start: c a a
    
    c   ab  a    .+.  --&gt;&gt;  c c a
    bc  c   a    -..  --&gt;&gt;  a c a
    a   c   ab   ..+  --&gt;&gt;  a c c
    a   bc  c    .-.  --&gt;&gt;  a a c
    ab  a   c    +..  --&gt;&gt;  c a c
    c   a   bc   ..-  --&gt;&gt;  c a a
    

A very limited kind of gradient, restricted to one color component.
This  could be written as 6 loops:
        while g &lt; 255:         # 1
            g += gap
            rbg_list.append((r, g, b))

        g = 255
        rbg_list.append((r, g, b))
            
        while r &gt; 0:           # 2
            r -= gap
            
        r = 0
        rbg_list.append((r, g, b))

        ...

However you have just increasing from 0 by gap, and decreasing from 255 by gap.
These would give two component value arrays which can then be used with the two other components 0 or 255. That should be exploitable in Python.
You might also calculate how many gap steps are needed for 255, times 6, and have a function that gives for the ith step the (r, g, b). That would be the simplest.
I admit I might be mistaken in what gradient calculation you want to achieve.