gumyr / cq_warehouse

A cadquery parametric part collection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

jdegenstein opened this issue · comments

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),
                     )}