Batch HSV adjustment

Hi there,

I’m wondering if it’s possible to do batch colour changes/shifts of multiple layers or objects in terms of Hue, Saturation and Brightness?
image

Sometimes after doing test prints and seeing the colour managed version, batch adjustments are required.

Cheers,
Jeremy

Perhaps some python multiplier; or shift if x degrees for hue.

-Jeremy

Yes it is. All you need is a Python script. Here is a starting point:

import Eto
import System
import Rhino
import scriptcontext as sc

def test_modify_layer_colors():
    
    # Process each layer
    for layer in sc.doc.Layers:
        
        # Get layer color as System.Drawing.Color
        color = layer.Color
        
        # The .NET Framework does not support other color spaces.
        # But Eto does...
        
        # Convert color to Eto.Drawing.Color
        color_eto = Eto.Drawing.Color.FromArgb(color.ToArgb())
        
        # Convert Eto color to Eto HSB color
        color_hsb = color_eto.ToHSB()
        
        # Get HSB values
        h = color_hsb.H # [0.0, 360.0]
        s = color_hsb.S # [0.0, 1.0]
        b = color_hsb.B # [0.0, 1.0]
        print('[H={0}, S={1}, B={2}]'.format(h, s, b))
        
        #
        # TODO: do something with HSB values here
        #
        
        # Reconstruct Eto HSB color
        color_hsb.H = h
        color_hsb.S = s
        color_hsb.B = b
        
        # Convert Eto HSB color to Eto color
        color_eto = color_hsb.ToColor()
        
        # Convert Eto color to System.Drawing.Color
        color = System.Drawing.Color.FromArgb(color_eto.ToArgb())
        
        # Set layer color
        layer.Color = color

# Call the function defined above
if __name__ == "__main__":
    test_modify_layer_colors()

test_modify_layer_colors.py (1.3 KB)

– Dale

1 Like

Hi Dale, thanks for your script :slight_smile:

If I understand correctly, you’re trying to help me learn basic python scripting to advance my Rhino game. If this is the case, I’m a blank slate. I have no experience in the nuance, hierarchies of programming. If you can direct me on how to approach this/learn, it would be appreciated.

However, to get to the stage of creating a neat functioning script might take time for abstract learning, trial and error - longer than I currently have at my disposal.

Best,
Jeremy

Hi @jdelavaulx,

For learning Python, this is as good as any place to start:

As for the script, the code retrieves the hue (h), saturation (s), and brightness (b) components for every layer color. All you need to do is “tweak” these values to your liking - the code will do the rest.

– Dale

Hi Dale, thanks for the guides.

I had a quick attempt, and got the following

image
made all layers have the one same colour
image

Not sure if the way I put the numbers was correct. But the goal is to treat the HSV shift of each layer individually, not assign them one HSB colour.

E.g., make everything darker, make everything more saturated, make everything shift hue x degrees.

Perhaps the script you provided does do this, but my attempt didnt use correct syntax etc

-Jeremy

@jdelavaulx
Here’s my attempt using the colorsys module in Python.
Below is just one for selected layers to test, if that works for you I can easily make one for selected objects. I hope I got the clamping right (i.e. the H value is circular from 0-360-0 again).

Edit - fixed one typo… sorry.
Edit 2 - fixed the S, V command prompts
LayerColorHSVShift.py (2.6 KB)

And here is the object color version to try:
ObjColorHSVShift.py (2.9 KB)

2 Likes

Wow… wizardry!

Both scripts working a charm. Thank you so much Mitch!
In combination with randomise layer colours, this is also a very quick way to get unique colours for a large amount of layers.

-Jeremy