clusterInk.test.ts
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { describe, expect, it } from 'vitest';
import { inkLevel } from './ClusterLayer';
import { inkAt } from './palette';
/**
* 数字浓淡这条编码线只有两条不变量,出问题都是满屏级的,所以值得钉住:
* 1) 深浅必须是案量的单调函数,且低段分得开 —— 曾经先按整屏一刀切(同一个 1 起
* 一会儿红点一会儿写着「1」),又改成过「不满 3 起只画点」(满屏没有数字)。
* 2) 墨色必须一直在靛蓝家族里 —— 散点模式用过 turbo 高温段的红,等于用「最热」
* 的颜色标案量最低的格子,颜色和数值说反话。
* 观感部分(淡到什么程度还算可读)只能在浏览器里判,这里只保证不越界。
*/
function rgbOf(css: string): [number, number, number] {
const m = css.match(/\d+/g);
if (!m || m.length < 3) throw new Error(`不是合法的 rgb:${css}`);
return [Number(m[0]), Number(m[1]), Number(m[2])];
}
describe('inkLevel 案量 → 浓淡档', () => {
it('随案量单调不减,并封顶在 1', () => {
for (const ceiling of [4, 12, 372, 5000]) {
let prev = -1;
for (let n = 0; n <= ceiling * 2; n += 1) {
const u = inkLevel(n, ceiling);
expect(u).toBeGreaterThanOrEqual(prev);
expect(u).toBeLessThanOrEqual(1);
prev = u;
}
expect(inkLevel(ceiling, ceiling)).toBeCloseTo(1, 6);
}
});
it('低案量之间仍分得出档:1 起与 2 起不能挤在同一档', () => {
// 直接按 count / 上限算的话,全市视图(上限三百多)里 1 和 2 差 0.003,
// 满屏低密度数字会糊成同一种淡 —— 用户看到的就是「深浅没意义」。
expect(inkLevel(2, 372) - inkLevel(1, 372)).toBeGreaterThan(0.05);
expect(inkAt(inkLevel(1, 372))).not.toBe(inkAt(inkLevel(2, 372)));
});
it('脏数据不把档算崩', () => {
expect(inkLevel(0, 0)).toBe(0);
expect(inkLevel(5, 0)).toBeLessThanOrEqual(1);
expect(inkLevel(-3, 10)).toBe(0);
});
});
describe('inkAt 墨色', () => {
it('全程是靛蓝家族,绝不出现热力渐变的红端', () => {
for (let i = 0; i <= 20; i += 1) {
const [r, g, b] = rgbOf(inkAt(i / 20));
// 靛墨:蓝 > 绿 > 红;turbo 的高温段是红远大于蓝
expect(b).toBeGreaterThan(g);
expect(g).toBeGreaterThanOrEqual(r);
}
const dark = rgbOf(inkAt(1));
expect(dark).toEqual([34, 43, 95]); // 深端 = 品牌 primaryDeep,与黑点数字同一支墨
});
it('越大的档越深(亮度单调下降)', () => {
const lum = (c: [number, number, number]) => c[0] + c[1] + c[2];
let prev = Number.POSITIVE_INFINITY;
for (let i = 0; i <= 20; i += 1) {
const v = lum(rgbOf(inkAt(i / 20)));
expect(v).toBeLessThanOrEqual(prev);
prev = v;
}
});
});