gumyr / cq_warehouse

A cadquery parametric part collection
Apache License 2.0
107 stars 23 forks source link

[feature request] add random color generator for CQ-editor show_object #77

Open jdegenstein opened 1 year ago

jdegenstein commented 1 year ago

image

Add a simple random color function that helps generate properly formatted dict or optionally a color tuple. Not submitting as PR because not sure where this would/should go. My guess is extensions.py but not sure...

import random
random.seed(371353) #preserves colors run to run, needs to be run once globally
def rand_color(alpha = 0., cfloat=False):
    #helper function to generate a random color dict
    #for CQ-editor's show_object function
    randrange = random.randrange
    lower = 10
    upper = 100 #not too high to keep color brightness in check
    if cfloat: #for two output types depending on need
        return (
                (randrange(lower,upper)/255),
                (randrange(lower,upper)/255),
                (randrange(lower,upper)/255),
                .9,
                )
    return {"alpha": alpha,
            "color": (
                      randrange(lower,upper),
                      randrange(lower,upper),
                      randrange(lower,upper),
                     )}