diff --git a/doc/0x.KaraTemplater.md b/doc/0x.KaraTemplater.md index 23eeebe..6c84615 100644 --- a/doc/0x.KaraTemplater.md +++ b/doc/0x.KaraTemplater.md @@ -347,7 +347,7 @@ It became an annoying piece of boilerplate, though, so now I offer this function ### `util.lerp(t, v0, v1)` Straightforward linear interpolation of two numbers: `(v1 * t) + (v0 * (1 - t))`. -### `util.gbc(c1, c2, interp)` +### `util.gbc(c1, c2, interp, t=util.cx())` A somewhat flexible function for gradient-by-character effects. Typical usage would go in a `mixin char` component and look something like `{\3c!util.gbc('&H0000FF&', '&HFF0000&')!}`. If no `interp` function is provided, I will try to guess how to interpolate the two values. @@ -357,6 +357,9 @@ At the moment, this means: If this is unsatisfactory, well, that's why I expose this function's third argument: specify your own interpolation function with a signature analagous to `util.lerp`'s. +### `util.multi_gbc(cs, interp, t=util.cx())` +Like `util.gbc`, but accepts a *list* of colors, for multi-stop gradients. Nifty. + ### `util.rand.sign()` Either -1 or 1, randomly. diff --git a/src/0x.KaraTemplater.moon b/src/0x.KaraTemplater.moon index e6a99b6..1d4bff3 100644 --- a/src/0x.KaraTemplater.moon +++ b/src/0x.KaraTemplater.moon @@ -31,7 +31,7 @@ util = (tenv) -> { lerp: (t, v0, v1) -> (v1 * t) + (v0 * (1 - t)) - gbc: (c1, c2, interp) -> + gbc: (c1, c2, interp, t=tenv.util.cx!) -> ctype = type c1 if ctype != type c2 error "gbc type mismatch: #{ctype} / #{type c2}" @@ -41,7 +41,18 @@ util = (tenv) -> { when 'string' then tenv.colorlib.interp_lch --a fallible assumption. revisit. else error "unknown gbc type: #{ctype}. please pass a custom interpolation function." - interp tenv.util.cx!, c1, c2 + interp t, c1, c2 + + multi_gbc: (cs, interp, t=tenv.util.cx!) -> + if t == 0 then return cs[1] + if t == 1 then return cs[#cs] + + -- Without these parens, moonc outputs incorrect Lua. + -- This is deeply disturbing. + t *= (#cs - 1) + + c1, c2 = cs[math.ceil t], cs[1 + math.ceil t] + tenv.util.gbc c1, c2, interp, t % 1 rand: { -- Either -1 or 1, randomly.