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

1 Like

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

Hi Mitch,

I’m wondering if it’s possible for this Python script to include an option for Transparency shift?

E.g., I’ve got 100 randomly coloured solid hatches, and I want to change them all from 100% to 23% opacity. Is that easy to amend to your script?

Best,
Jeremy

If the objects are already existing, they have a color, I think I can write a script to just adjust the transparency and keep the same color. Let me look.

Thanks Mitch,

Similarly, Is there a way to set the transparency value in Grasshopper?

Best

Does this work for you?
Edit: please use revised version further down
SetObjTransparency.py (1.1 KB)

The question on practicality I have here is what to set the minimum percentage value at. Right now, I have it set to not quite zero. But is that practical? Should I limit it to some higher minimum percentage like 1%?

TBH, I don’t really do anything in GH that changes the value of existing objects in the Rhino document. I am sure it’s possible with some plug-ins like Elefront, and probably in GH2 natively…

1 Like

I think that perhaps it might be better in 0-255 format? Means there’s no confusion?

Also, if it’s not too difficult, a similar script for batch changing layer transparency would be super helpful.

Perhaps it could be useful to append your previous Layer and Obj colour HSV shift scripts to include transparency 0-255?

Those old ones in this thread had int(h360),int(s100),int(v100). If it also included int(t255) or similar, would be lovely.

Best,
Jeremy

The scripts above are for “shifting” the color - not setting it with fixed values. Is shifting the existing transparency what you want? Or something that simply sets it?

Also, as per my question above, is allowing 100% transparency (alpha=0) useful?

1 Like

Hi Mitch, thanks for the reply.

I was literally just thinking about the fact that a shift is desired; instead of an absolute value; just before jumping on here to see your post commenting the same.

responding to your question; I think that 100% transparency would not be a bad thing, because the object or hatch can still exist without being seen; but can be changed back to full opacity if desired.

Do you think that -100 to +100 shift for transparency would be good? Or do you think that -255 to + 255 as a change in hex could also be valuable?

Best

Sorry for the delay… Try these out:

ObjColorHSVShiftEx.py (3.6 KB)
LayerColorHSVShiftEx.py (3.2 KB)

And just in case, for simply directly setting the transparency level on objects or layers:
SetObjTransparency.py (1.2 KB)
SetLayerTransparency.py (908 Bytes)

Note, none of these are designed to work with block instances…

1 Like

Thank you very much Mitch; Scripts working a charm :slight_smile:

Jeremy

Hi Mitch, I’m frequently using these scripts and am thankful for them

Can these scripts similarly be applied to the Print Color independently from the Display Color?

Best,
Jeremy

Try these out…
ObjPrintColorHSVShiftEx.py (3.7 KB)
LayerPrintColorHSVShiftEx.py (3.3 KB)
I just changed a few lines in the scripts to work on print colors instead of display colors.
Haven’t tested them extensively…

1 Like