Random QColor Generator with Range Limit

  • Share this:

Code introduction


This function takes two integer arguments and generates a random QColor object. The first and second arguments are used to limit the range of the red, green, and blue components of the random color.


Technology Stack : PyQt6, QColor, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
def generate_random_color(arg1, arg2):
    from PyQt6.QtGui import QColor
    import random

    if not isinstance(arg1, int) or not isinstance(arg2, int):
        raise ValueError("Both arguments must be integers")

    return QColor(random.randint(arg1, arg2), random.randint(arg1, arg2), random.randint(arg1, arg2))