An HSV / HSB to RGB Color function in OpenSCAD

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

An HSV / HSB to RGB Color function in OpenSCAD

Hypher


I've been using OpenSCAD to generate some mockups that are showing different colors of LEDs. I don't personally like to work with or think in RGB color directly, preferring to use HSB/HSV or HSL. Unfortunately, I couldn't find any conversion functions for OpenSCAD anywhere and the OpenSCAD function syntax makes it a bit annoying to write one.

I'd like to save everyone else the trouble, so here is an HSB/HSV to RGB converter function for OpenSCAD:

function doHsvMatrix(h,s,v,p,q,t,a=1)=[h<1?v:h<2?q:h<3?p:h<4?p:h<5?t:v,h<1?t:h<2?v:h<3?v:h<4?q:h<5?p:p,h<1?p:h<2?p:h<3?t:h<4?v:h<5?v:q,a];
function hsv(h, s=1, v=1,a=1)=doHsvMatrix((h%1)*6,s<0?0:s>1?1:s,v<0?0:v>1?1:v,v*(1-s),v*(1-s*((h%1)*6-floor((h%1)*6))),v*(1-s*(1-((h%1)*6-floor((h%1)*6)))),a);

Call it with hsv(hue, sat, val, alpha) where each value is in the range [0,1]. The function will automatically handle out-of-bounds values by wrapping hue and clipping saturation and value/brightness. Default values are given for everything except hue, so you can implement a simple color wheel with hsv(hue).

You can find a non-obfuscated version along with the code to generate the attached image here: https://gist.github.com/LightAtPlay/24148d8be2e66d26fd11
Reply | Threaded
Open this post in threaded view
|

Re: An HSV / HSB to RGB Color function in OpenSCAD

MichaelAtOz
Administrator
Pretty
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|

Re: An HSV / HSB to RGB Color function in OpenSCAD

jayod
This post has NOT been accepted by the mailing list yet.
In reply to this post by Hypher
Hypher -

I'd like to use your implementation of hsv to rgb in BlocksCAD (https://blockscad.einsteinsworkshop.com, https://github.com/EinsteinsWorkshop/BlocksCAD ).  I will give credit to "Hypher" and link this post on the mailing list, or give whatever attribution you'd prefer. Let me know if you have any problems with this or want to change the attribution.
Reply | Threaded
Open this post in threaded view
|

Re: An HSV / HSB to RGB Color function in OpenSCAD

Hypher
Jayod,

Great to hear you find the function useful. BlocksCAD is great and I
really appreciate it bringing non-coders to parametric modeling.

With this message, I hereby release the original function in this thread
into the public domain, relinquishing any copyright claim I might have,
to the extent legally possible.

So, you're free to use the code anywhere you like. You're welcome to
link back to this thread for credit. You can attribute to "Yona
Appletree (Hypher)" if you like as well.

Best,

Yona

jayod [via OpenSCAD] wrote:

> Jayod,
>
> Great to hear you find the function useful. BlocksCAD is great and I
> really appreciate it bringing non-coders to parametric modeling.
>
> With this message, I hereby release the original function in this
> thread into the public domain, relinquishing any copyright claim I
> might have, to the extent legally possible.
>
> So, you're free to use the code anywhere you like. You're welcome to
> link back to this thread for credit. You can attribute to "Yona
> Appletree (Hypher)" if you like as well.
>
> Best,
>
> Yona