Checking if a value is in the RGB range and then increment/decrement Is there a better way to check if the variable x is in the 0-255 range and:

if x is less than 255, add colVel to it
if x equals 255 minus colVel from it.

My implementation, in Python 3.8.
if 0 &lt;= x &lt; colVel:
    flag = True
if (255-colVel) &lt; x &lt; 255:
    x = 255        
if (255-colVel) &lt;= x &lt;= 255: 
    flag = False
if flag == True:
    x += colVel
if flag == False:
    x -= colVel

This section of code is a function that creates iridescence while an object moves. This is all of my code, and I'm happy for it to be reviewed.
import pygame
import random
pygame.init()

win = pygame.display.set_mode((800,600))

pygame.display.set_caption('Chachi')
x = 50
y = 50
width = 2
height = 2
vel = 5
colVel = 10
rf = True
gf = True
bf = True
running = True

def irid(r, g, b, rFlag=True, gFlag=True, bFlag=True):
        def rgbRange(x, flag):                
                if 0&lt;= x&lt; colVel:
                        x = random.randint(1, 100) 
                        flag = True
                if (255-colVel) &lt; x &lt; 255:
                        x = 255        
                if (255-colVel) &lt;= x &lt;= 255: 
                        flag = False
                if flag==True:
                        x += colVel
                if flag==False:
                        x -= colVel   
                return x, flag        
        r, rFlag = rgbRange(r, rFlag)
        g, gFlag = rgbRange(g, gFlag)
        b, bFlag = rgbRange(b, bFlag)          
        return r, g, b, rFlag, gFlag, bFlag
r = irid(0, 255, 0)[0]
g = irid(0, 255, 0)[1]
b = irid(0, 255, 0)[2]
          
while running:
    pygame.time.delay(1)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT]:
        x -= vel
        r, g, b, rf, gf, bf = irid(r, g, b, rf, gf, bf)
            
    if keys[pygame.K_RIGHT]: 
        x += vel
        r, g, b, rf, gf, bf = irid(r, g, b, rf, gf, bf)
    if keys[pygame.K_UP]:
        y -= vel
        r, g, b, rf, gf, bf = irid(r, g, b, rf, gf, bf)
    if keys[pygame.K_DOWN]:
        y += vel
        r, g, b, rf, gf, bf = irid(r, g, b, rf, gf, bf)
            
    pygame.draw.rect(win, (r, g, b), (x, y, width, height) )
    pygame.display.update()
    
pygame.quit()
Avoid repeating yourself
First, avoid having separate variables for r, g and b. It means you have to write all code three times. Just store the three components in a list, like so:
color = [0, 255, 0]

Then you can pass color to other functions, and have those functions iterate over the elements of the list.
Better way to create a triangle wave
What you are doing is basically creating a triangle wave shape for the intensity of each color component. Instead of keeping the current color value and a flag to store whether you are on the increasing slope or decreasing slope, just store a variable that holds the phase in a full cycle of the triangle wave.
In this case, I would store an integer between 0 and 511 for each color component. Initialize the phase like you did the color vector before:
phase = [0, 255, 0]

When updating the phase, do:
for i in range(3):
    phase[i] += colorVel
    phase[i] %= 512

Of course, this gives a sawtooth wave, and the values you get will be too high, but now you can map it onto a triangle wave with the correct amplitude:
color = list(map(lambda x: x if x &lt; 256 else 511 - x, phase))

Create a class to store the state of the iridescence
Even better would be to have a class manage all the internal state of an iridescence color, and allow it to be called as a function that generates the next color:
class Iridecence:
    def __init__(self, r, g, b, colVel=10):
        self.phase = [r, g, b]
        self.colVel = colVel

    def __call__(self):
        for i in range(3):
            if self.phase[i] &lt; self.colVel:
                self.phase[i] = random.randint(1, 100)
            self.phase[i] += self.colVel
            self.phase[i] %= 512;
        return list(map(lambda x: x if x &lt; 256 else 511 - x, self.phase))

Now you can use this as follow in your code:
irid = Iridescence(0, 255, 0)
color = irid()

while running:
    ...
    if keys[pygame.K_LEFT]:
        x -= vel
        color = irid()
    ...
    pygame.draw.rect(win, color, (x, y, width, height) )
    pygame.display.update()