Skip to content

Commit

Permalink
KaraTemplater: util.multi_gbc
Browse files Browse the repository at this point in the history
  • Loading branch information
The0x539 committed Jul 21, 2021
1 parent e531bd2 commit 2ae95b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 4 additions & 1 deletion doc/0x.KaraTemplater.md
Expand Up @@ -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.
Expand All @@ -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.

Expand Down
15 changes: 13 additions & 2 deletions src/0x.KaraTemplater.moon
Expand Up @@ -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}"
Expand All @@ -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.
Expand Down

0 comments on commit 2ae95b0

Please sign in to comment.