From e2fe77b680ca67dad34018ed191c75bc7112f823 Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Thu, 23 May 2024 18:10:34 +0300 Subject: [PATCH] Fix coloring in rank2 with size hints Improve parallelotope coloring semantics. ref #329 --- src/parser/__tests__/stdlib.spec.ts | 64 +++++++++++++++++++---------- src/stdlib/prelude.ts | 10 +++-- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/parser/__tests__/stdlib.spec.ts b/src/parser/__tests__/stdlib.spec.ts index 19eb7f3e..6a0821f5 100644 --- a/src/parser/__tests__/stdlib.spec.ts +++ b/src/parser/__tests__/stdlib.spec.ts @@ -1541,41 +1541,61 @@ describe('SonicWeave standard library', () => { it('supports size hints for rank-2 preimages', () => { const scale = expand(` - rank2(10/9, 7, 0, 2/1, 1, 2\\15) + rank2(10/9 red, 7, 0, 2/1 green, 1, 2\\15) i => i str(i) 15@ `); expect(scale).toEqual([ - '2\\15 "10/9"', - '4\\15 "100/81"', - '6\\15 "1000/729"', - '8\\15 "10000/6561"', - '10\\15 "100000/59049"', - '12\\15 "1000000/531441"', - '14\\15 "10000000/4782969"', - '15\\15 "2/1"', + '2\\15 "10/9" red', + '4\\15 "100/81" red', + '6\\15 "1000/729" red', + '8\\15 "10000/6561" red', + '10\\15 "100000/59049" red', + '12\\15 "1000000/531441" red', + '14\\15 "10000000/4782969" red', + '15\\15 "2/1" green', ]); }); it('supports size hints for rank-3 preimages', () => { const scale = expand(` - parallelotope([3, 5], [2, 3], niente, 2, [1892.986, 2795.938], 1196.430) + parallelotope([3 red, 5 green], [2, 3], niente, 2 blue, [1892.986, 2795.938], 1196.430) i => i str(i) PrimeMapping(1196.430, 1892.986, 2795.938, 3382.895) `); expect(scale).toEqual([ - '12.804 "125/128"', - '196.682 "9/8"', - '209.486 "1125/1024"', - '306.282 "75/64"', - '403.078 "5/4"', - '599.76 "45/32"', - '696.556 "3/2"', - '709.36 "375/256"', - '806.156 "25/16"', - '1002.838 "225/128"', - '1099.634 "15/8"', - '1196.43 "2"', + '12.804 "125/128" green', + '196.682 "9/8" red', + '209.486 "1125/1024" red', + '306.282 "75/64" red', + '403.078 "5/4" green', + '599.76 "45/32" red', + '696.556 "3/2" red', + '709.36 "375/256" red', + '806.156 "25/16" green', + '1002.838 "225/128" red', + '1099.634 "15/8" red', + '1196.43 "2" blue', + ]); + }); + + it('has sensible parallelotope coloring (without size hints)', () => { + const scale = expand( + 'parallelotope([3 red, 5 green], [2, 3], niente, 2 blue)' + ); + expect(scale).toEqual([ + '1125/1024 red', + '9/8 red', + '75/64 red', + '5/4 green', + '45/32 red', + '375/256 red', + '3/2 red', + '25/16 green', + '225/128 red', + '15/8 red', + '125/64 green', + '2 blue', ]); }); }); diff --git a/src/stdlib/prelude.ts b/src/stdlib/prelude.ts index 26deb82f..d167384b 100644 --- a/src/stdlib/prelude.ts +++ b/src/stdlib/prelude.ts @@ -506,7 +506,7 @@ riff rank2(generator, up, down = 0, period = 2, numPeriods = 1, generatorSizeHin const scales = zip(scale, hint ~rdc periodSizeHint); sort(scales, (a, b) => compare(a[1], b[1])); [a for [a, b] of scales]; - pop() vor period; + period vor pop(); repeat(numPeriods); } @@ -551,7 +551,11 @@ riff parallelotope(basis, ups = niente, downs = niente, equave = 2, basisSizeHin const up = pop(ups); const down = pop(downs); - popAll($$) tns~ generator ~^ [-down..up]; + // Don't impose color on unity. + const axis = generator ~^ [-down..up]; + axis[down] = bleach(axis[down]); + + axis ~tns~ popAll($$); } if (basisSizeHints == niente and equaveSizeHint == niente) { @@ -578,7 +582,7 @@ riff parallelotope(basis, ups = niente, downs = niente, equave = 2, basisSizeHin const up = pop(ups); const down = pop(downs); - popAll($$) tns~ generator ~^ [-down..up]; + generator ~^ [-down..up] ~tns~ popAll($$); } const hint = popAll();