@@ -386,6 +386,25 @@ static const char* hsltorgb_docstring =
386386 " hsl value (except for negative s values), the conversion is\n "
387387 " well-defined and reversible." ;
388388
389+ static Vec3d saturate (const Vec3d& Cin, double amt) {
390+ const Vec3d lum (.2126 ,.7152 ,.0722 ); // rec709 luminance
391+ Vec3d result = Vec3d (Cin.dot (lum) * (1 -amt)) + Cin * amt;
392+ if (result[0 ] < 0 ) result[0 ] = 0 ;
393+ if (result[1 ] < 0 ) result[1 ] = 0 ;
394+ if (result[2 ] < 0 ) result[2 ] = 0 ;
395+ return result;
396+ }
397+
398+ Vec3d saturate (int n, const Vec3d* args) {
399+ if (n < 2 ) return 0.0 ;
400+ return saturate (args[0 ], args[1 ][0 ]);
401+ }
402+ static const char * saturate_docstring =
403+ " color saturate(color val, float amt)\n "
404+ " Scale saturation of color by amt.\n "
405+ " The color is scaled around the rec709 luminance value,\n "
406+ " and negative results are clamped at zero.\n " ;
407+
389408double hash (int n, double * args) {
390409 // combine args into a single seed
391410 uint32_t seed = 0 ;
@@ -729,7 +748,9 @@ static const char* ccellnoise_docstring =
729748double pnoise (const Vec3d& p, const Vec3d& period) {
730749 double result;
731750 double args[3 ] = {p[0 ], p[1 ], p[2 ]};
732- int pargs[3 ] = {(int )period[0 ], (int )period[1 ], (int )period[2 ]};
751+ int pargs[3 ] = {max (1 ,(int )period[0 ]),
752+ max (1 ,(int )period[1 ]),
753+ max (1 ,(int )period[2 ])};
733754 PNoise<3 , 1 >(args, pargs, &result);
734755 return result;
735756}
@@ -1177,6 +1198,13 @@ static const char* pick_docstring =
11771198 " to the supplied weights. Any weights not supplied are assumed to\n "
11781199 " be 1.0." ;
11791200
1201+ double swatch (int n, double * params) {
1202+ return choose (n, params);
1203+ }
1204+ static const char *swatch_docstring=
1205+ " color swatch(float index, color choice0, color choice1, color choice2, [...])\n "
1206+ " Chooses one of the supplied color choices based on the index (assumed to be in range [0..1])." ;
1207+
11801208double choose (int n, double * params) {
11811209 if (n < 3 ) return 0 ;
11821210 double key = params[0 ];
@@ -1644,6 +1672,7 @@ void defineBuiltins(ExprFunc::Define define, ExprFunc::Define3 define3) {
16441672 FUNCNDOC (midhsi, 5 , 7 );
16451673 FUNCDOC (hsltorgb);
16461674 FUNCDOC (rgbtohsl);
1675+ FUNCNDOC (saturate, 2 , 2 );
16471676
16481677 // noise
16491678 FUNCNDOC (hash, 1 , -1 );
@@ -1684,6 +1713,7 @@ void defineBuiltins(ExprFunc::Define define, ExprFunc::Define3 define3) {
16841713 FUNCNDOC (pick, 3 , -1 );
16851714 FUNCNDOC (choose, 3 , -1 );
16861715 FUNCNDOC (wchoose, 4 , -1 );
1716+ FUNCNDOC (swatch, 3 , -1 );
16871717 FUNCNDOC (spline, 5 , -1 );
16881718
16891719 // FuncX interface
0 commit comments