Gradient function that returns a list of all colors in order
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.