跳到正文

休闲·娱乐

信标光柱的颜色

Eznibuil约 21 分钟
原文
文章信息与版本

文章信息

发布
2026年4月14日
收录
2026年5月15日
更新
2026年11月12日
篇幅
约 21 分钟

版本

  1. v2当前
  2. v1

本文讨论的是 Java 版。

很久以前就想写这么一篇文章了,最近恰好又翻到中文维基上的一句话:

修改了信标光柱的着色算法。在此之前,光柱颜色以浮点数计算,总数为 500 万左右。

很有意思,我没想到 Mojang 还有心情改这个,虽然已经是快两年前的事了。

考虑到光柱的颜色用浮点数表示,其实是一个很简单的公式,不考虑浮点误差的话正如维基所写(当然,一个维度上):

2−kc0+∑i=1k2i−k−1ci.2^{-k}c_0+\sum_{i=1}^k2^{i-k-1}c_i.

用整数表示的话加个向下取整就行了。容易证明每一步都向下取整和只在最后一步向下取整没有区别。

很容易证明若允许存在无限多玻璃调色,则任何能调出的颜色都落在所有 16 种颜色的三维凸包中。(注意:这不意味着凸包中每个点都能取到,见下文。)

记 16 种颜色分别为:(按顺序“white”“orange”“magenta”“light blue”“yellow”“lime”“pink”“gray”“light gray”“cyan”“purple”“blue”“brown”“green”“red”“black”)

A(249,255,254)B(249,128,29)C(199,78,189)D(58,179,218)E(254,216,61)F(128,199,31)G(243,139,170)H(71,79,82)I(157,157,151)J(22,156,156)K(137,50,184)L(60,68,170)M(131,84,50)N(94,124,22)O(176,46,38)P(29,29,33)\begin{aligned} A(249,255,254)\\ B(249,128,29)\\ C(199,78,189)\\ D(58,179,218)\\ E(254,216,61)\\ F(128,199,31)\\ G(243,139,170)\\ H(71,79,82)\\ I(157,157,151)\\ J(22,156,156)\\ K(137,50,184)\\ L(60,68,170)\\ M(131,84,50)\\ N(94,124,22)\\ O(176,46,38)\\ P(29,29,33) \end{aligned}

接下来无耻地去洛谷的三维凸包模板偷一下代码,可以得其所有外表面:

(A,B,E)(A,B,G)(A,C,G)(A,C,K)(A,D,F)(A,D,K)(A,E,F)(B,C,G)(B,C,O)(B,E,F)(B,F,N)(B,N,P)(B,O,P)(C,K,O)(D,F,J)(D,J,L)(D,K,L)(F,J,P)(F,N,P)(J,L,P)(K,L,P)(K,O,P)\begin{aligned} (A,B,E)\\ (A,B,G)\\ (A,C,G)\\ (A,C,K)\\ (A,D,F)\\ (A,D,K)\\ (A,E,F)\\ (B,C,G)\\ (B,C,O)\\ (B,E,F)\\ (B,F,N)\\ (B,N,P)\\ (B,O,P)\\ (C,K,O)\\ (D,F,J)\\ (D,J,L)\\ (D,K,L)\\ (F,J,P)\\ (F,N,P)\\ (J,L,P)\\ (K,L,P)\\ (K,O,P) \end{aligned}

可怜的 HH(gray)、II(light gray)、MM(brown)三点被裹在里面了。余下的都在凸包表面上,且不存在四点共面的情况。

现在来考虑颜色的逼近问题。

规则的八个点是不存在的。何谓规则呢?想象一个平行六面体的八个顶点,对它们组合就可以保证不存在任何空隙。为了不掉 Hausdorff 维数就需要更多的点。

幸好这里有十六个点,不过还是可能存在掉维的问题,不管了。

在点均为格点的情况下,只需要跑一遍 BFS 就可以求出最少需要的玻璃数目。代码如下。

C++
#include<iostream>
constexpr int c[]{16383998,16351261,13061821,3847130,16701501,8439583,15961002,4673362,10329495,1481884,8991416,3949738,8606770,6192150,11546150,1908001};
constexpr char n[][11]{"white","orange","magenta","light_blue","yellow","lime","pink","gray","light_gray","cyan","purple","blue","brown","green","red","black"};
int m[1<<24|1],q[1<<24|1];
int main()
{
	int t=0;
	for(int i:c)
		m[q[t++]=i]=1;
	for(int h=0;h<t;h++)
		for(int i:c)
		{
			int x=((i&0xff0000)+(q[h]&0xff0000)&0x1fe0000|(i&0xff00)+(q[h]&0xff00)&0x1fe00|(i&0xff)+(q[h]&0xff))>>1;
			if(!m[x])
				m[q[t++]=x]=m[q[h]]+1;
		}
	std::cout<<t<<' '<<m[q[t-1]],t=0;
	for(int i=0;i<1<<24;i++)
		if(!m[i]&&((i&0xff0000)&&m[i-0x10000]>0||(i&0xff00)&&m[i-0x100]>0||(i&0xff)&&m[i-1]>0||(i>>16)<0xff&&m[i+0x10000]||(i&0xff00)<0xff00&&m[i+0x100]||(i&0xff)<0xff&&m[i+1]))
			m[q[t++]=i]=-1;
	for(int h=0;h<t;h++)
		for(int i:{0,8,16})
		{
			if((q[h]>>i&0xff)&&!m[q[h]-(1<<i)])
				m[q[t++]=q[h]-(1<<i)]=m[q[h]]-1;
			if((q[h]>>i&0xff)<0xff&&!m[q[h]+(1<<i)])
				m[q[t++]=q[h]+(1<<i)]=m[q[h]]-1;
		}
	std::cout<<' '<<-m[q[t-1]]<<'\n';
	for(int x;std::cin>>x;)
	{
		if(m[x]<0)
		{
			std::cout<<-m[x]<<' ';
			while(m[x]<0)
				for(int i:{0,8,16})
				{
					if(m[x]<0&&(x>>i&0xff)&&m[x-(1<<i)]>m[x])
						x-=1<<i;
					if(m[x]<0&&(x>>i&0xff)<0xff&&m[x+(1<<i)]>m[x])
						x+=1<<i;
				}
			std::cout<<x<<' ';
		}
		while(m[x]>1)
		{
			bool f=0;
			for(int i=0;i<16;i++)
			{
				for(int j:{0,1,0x101,0x100,0x10100,0x10101,0x10001,0x10000})
				{
					int y=j+(x<<1&0x1fe0000)-(c[i]&0xff0000)+(x<<1&0x1fe00)-(c[i]&0xff00)+(x<<1&0x1fe)-(c[i]&0xff);
					if(!(y&0xff000000)&&x==(((c[i]&0xff0000)+(y&0xff0000)&0x1fe0000|(c[i]&0xff00)+(y&0xff00)&0x1fe00|(c[i]&0xff)+(y&0xff))>>1)&&m[x]==m[y]+1)
					{
						std::cout<<n[i]<<' ',x=y,f=1;
						break;
					}
				}
				if(f)
					break;
			}
		}
		for(int i=0;i<16;i++)
			if(x==c[i])
			{
				std::cout<<n[i]<<'\n';
				break;
			}
	}
	return 0;
}

该代码和维基计算出的结果一致,为 4928458,同时也可以帮助你确定某个颜色的最短玻璃序列(最长的最短序列长达 21)。具体而言是交互式的,输入颜色的十进制表示,就会以倒序输出染色玻璃序列。如果这个颜色无法达到,则转而计算曼哈顿距离的最近颜色。

如果你亲自测试了代码就会发现,维基那个破东西根本没法给出某些颜色的准确序列。

现在可以回到浮点数的讨论上:其实这差别不大,容易证明使用整数向下取整,最后累积的误差也不会超过 1,这是肉眼识别不了的,无需在意。

但令人真正在意的却是,如果可以用无限多的玻璃,究竟哪些颜色是可达的。这我确实解决不了,希望其他人有所研究。

(顺带一提,台版翻译“烽火台”是什么鬼啊喂?)

Upd:请欣赏让我当场脑溢血的维基所使用的代码。充分诠释了什么叫做全是情感,毫无技巧。它预先存储了 74612 个玻璃序列,然后检查哪个序列最好。

所以我新写了一份代码,采用跟维基一样的 CIE94 公式计算色差,如下:

C++
#include<cmath>
#include<algorithm>
#include<array>
#include<iostream>
#include<vector>
constexpr int c[]{16383998,16351261,13061821,3847130,16701501,8439583,15961002,4673362,10329495,1481884,8991416,3949738,8606770,6192150,11546150,1908001};
constexpr char n[][11]{"white","orange","magenta","light_blue","yellow","lime","pink","gray","light_gray","cyan","purple","blue","brown","green","red","black"};
int m[1<<24|1];
std::array<double,3>lab[1<<24|1];
constexpr std::array<double,3>rgb2lab(int rgb)
{
	double r=(rgb>>16)/255.,g=(rgb>>8&0xff)/255.,b=(rgb&0xff)/255.;
	if(r>.04045)
		r=std::pow((r+.055)/1.055,2.4);
	else
		r/=12.92;
	if(g>.04045)
		g=std::pow((g+.055)/1.055,2.4);
	else
		g/=12.92;
	if(b>.04045)
		b=std::pow((b+.055)/1.055,2.4);
	else
		b/=12.92;
	double x=(r*.4124+g*.3576+b*.1805)/.95047,y=r*.2126+g*.7152+b*.0722,z=(r*.0193+g*.1192+b*.9505)/1.08883;
	if(x>.008856)
		x=std::cbrt(x);
	else
		x=x*7.787+16./116;
	if(y>.008856)
		y=std::cbrt(y);
	else
		y=y*7.787+16./116;
	if(z>.008856)
		z=std::cbrt(z);
	else
		z=z*7.787+16./116;
	return{y*116-16,(x-y)*500,(y-z)*200};
}
constexpr double deltaE94(std::array<double,3>lab1,std::array<double,3>lab2)
{
	double c1=std::hypot(lab1[1],lab1[2]),dl=lab2[0]-lab1[0],dc=std::hypot(lab2[1],lab2[2])-c1,de=std::hypot(lab1[0]-lab2[0],lab1[1]-lab2[1],lab1[2]-lab2[2]),dh=de*de-dl*dl-dc*dc;
	if(dh>0)
		dh=std::sqrt(dh);
	else
		dh=0;
	return std::hypot(dl,dc/(1+c1*.045),dh/(1+c1*.015));
}
std::vector<int>rgb2glass(int x)
{
	std::vector<int>v;
	while(m[x]>1)
	{
		bool f=0;
		for(int i=0;i<16;i++)
		{
			for(int j:{0,1,0x101,0x100,0x10100,0x10101,0x10001,0x10000})
			{
				int y=j+(x<<1)-c[i];
				if(!(y&0xff000000)&&x==(((c[i]&0xff0000)+(y&0xff0000)&0x1fe0000|(c[i]&0xff00)+(y&0xff00)&0x1fe00|(c[i]&0xff)+(y&0xff))>>1)&&m[x]==m[y]+1)
				{
					v.push_back(i),x=y,f=1;
					break;
				}
			}
			if(f)
				break;
		}
	}
	for(int i=0;i<16;i++)
		if(x==c[i])
		{
			v.push_back(i);
			break;
		}
	std::reverse(v.begin(),v.end());
	return v;
}
void display(int x)
{
	std::array<double,3>labx=rgb2lab(x);
	double dE[22];
	int rgb[22];
	for(int i=0;i<22;i++)
		dE[i]=1e308;
	for(int i=0;i<1<<24;i++)
		if(m[i])
		{
			double _=deltaE94(labx,lab[i]);
			if(dE[m[i]]>_)
				dE[m[i]]=_,rgb[m[i]]=i;
		}
	std::cout<<"0 "<<(dE[0]=deltaE94(labx,lab[16383998]))<<" 16383998\n";
	for(int i=1;i<22;i++)
		if(dE[0]>dE[i])
		{
			std::cout<<i<<' '<<(dE[0]=dE[i]);
			for(int j:rgb2glass(rgb[i]))
				std::cout<<' '<<n[j];
			std::cout<<' '<<rgb[i]<<'\n';
		}
	return;
}
int main()
{
	static int q[1<<24|1];
	int t=0;
	for(int i:c)
		m[q[t++]=i]=1,lab[i]=rgb2lab(i);
	for(int h=0;h<t;h++)
		for(int i:c)
		{
			int x=((i&0xff0000)+(q[h]&0xff0000)&0x1fe0000|(i&0xff00)+(q[h]&0xff00)&0x1fe00|(i&0xff)+(q[h]&0xff))>>1;
			if(!m[x])
				m[q[t++]=x]=m[q[h]]+1,lab[x]=rgb2lab(x);
		}
	for(int x;std::cin>>x;)
		display(x);
	return 0;
}

让 AI 帮我翻译成 Typescript,然后我稍作修改:

TypeScript
const colors = [16383998, 16351261, 13061821, 3847130, 16701501, 8439583, 15961002, 4673362, 10329495, 1481884, 8991416, 3949738, 8606770, 6192150, 11546150, 1908001];
const names = ["white", "orange", "magenta", "light_blue", "yellow", "lime", "pink", "gray", "light_gray", "cyan", "purple", "blue", "brown", "green", "red", "black"];

const [minlen, lab] = init();

function rgb2lab(rgb: number): [number, number, number] {
	let r = ((rgb >> 16) & 0xff) / 255;
	let g = ((rgb >> 8) & 0xff) / 255;
	let b = (rgb & 0xff) / 255;

	if (r > 0.04045) {
		r = Math.pow((r + 0.055) / 1.055, 2.4);
	} else {
		r /= 12.92;
	}
	if (g > 0.04045) {
		g = Math.pow((g + 0.055) / 1.055, 2.4);
	} else {
		g /= 12.92;
	}
	if (b > 0.04045) {
		b = Math.pow((b + 0.055) / 1.055, 2.4);
	} else {
		b /= 12.92;
	}

	const x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
	const y = r * 0.2126 + g * 0.7152 + b * 0.0722;
	const z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;

	const cx = x > 0.008856 ? Math.cbrt(x) : x * 7.787 + 16 / 116;
	const cy = y > 0.008856 ? Math.cbrt(y) : y * 7.787 + 16 / 116;
	const cz = z > 0.008856 ? Math.cbrt(z) : z * 7.787 + 16 / 116;

	return [cy * 116 - 16, (cx - cy) * 500, (cy - cz) * 200];
}

function deltaE94(lab1: [number, number, number], lab2: [number, number, number]): number {
	const c1 = Math.hypot(lab1[1], lab1[2]);
	const dl = lab2[0] - lab1[0];
	const dc = Math.hypot(lab2[1], lab2[2]) - c1;
	const de = Math.hypot(lab1[0] - lab2[0], lab1[1] - lab2[1], lab1[2] - lab2[2]);
	let dh = de * de - dl * dl - dc * dc;
	if (dh > 0) {
		dh = Math.sqrt(dh);
	} else {
		dh = 0;
	}
	return Math.hypot(dl, dc / (1 + c1 * 0.045), dh / (1 + c1 * 0.015));
}

function rgb2glass(x: number): number[] {
	const v: number[] = [];
	while (minlen[x] > 1) {
		let f = false;
		for (let i = 0; i < 16; i++) {
			for (const j of [0, 1, 0x101, 0x100, 0x10100, 0x10101, 0x10001, 0x10000]) {
				const y = j + (x << 1) - colors[i];
				if (!(y & 0xff000000) &&
					x === (((colors[i] & 0xff0000) + (y & 0xff0000)) & 0x1fe0000
						| ((colors[i] & 0xff00) + (y & 0xff00)) & 0x1fe00
						| ((colors[i] & 0xff) + (y & 0xff))) >> 1
					&& minlen[x] === minlen[y] + 1) {
					v.push(i);
					x = y;
					f = true;
					break;
				}
			}
			if (f) break;
		}
	}
	for (let i = 0; i < 16; i++) {
		if (x === colors[i]) {
			v.push(i);
			break;
		}
	}
	v.reverse();
	return v;
}

function display(x: number): void {
	const labx = rgb2lab(x);
	const dE: number[] = [];
	const rgb: number[] = [];

	for (let i = 0; i < 22; i++) {
		dE.push(1e308);
		rgb.push(0);
	}

	for (let i = 0; i < 1 << 24; i++) {
		if (minlen[i]) {
			const d = deltaE94(labx, lab[i]);
			if (dE[minlen[i]] > d) {
				dE[minlen[i]] = d;
				rgb[minlen[i]] = i;
			}
		}
	}

	dE[0] = deltaE94(labx, lab[16383998]);// no glass is equivalent to a white glass
	console.log(`0 ${dE[0]} 16383998`);
	for (let i = 1; i < 22; i++) {
		if (dE[0] > dE[i]) {
			dE[0] = dE[i];
			let output = `${i} ${dE[0]}`;// the total amount of glasses, the minimum deltaE
			for (const j of rgb2glass(rgb[i])) {
				output += ` ${names[j]}`;// glass sequence
			}
			output += ` ${rgb[i]}`;// the final color produced by glasses
			console.log(output);
		}
	}
}

function init(): [Int8Array, [number, number, number][]] {
	const m = new Int8Array(1 << 24);
	const q = new Int32Array(1 << 24);
	const lab: [number, number, number][] = Array(1 << 24);
	let t = 0;
	for (const i of colors) {
		m[q[t++] = i] = 1;
		lab[i] = rgb2lab(i);
	}
	for (let h = 0; h < t; h++) {
		for (const i of colors) {
			const x = (((i & 0xff0000) + (q[h] & 0xff0000)) & 0x1fe0000
				| ((i & 0xff00) + (q[h] & 0xff00)) & 0x1fe00
				| ((i & 0xff) + (q[h] & 0xff))) >> 1;
			if (!m[x]) {
				m[q[t++] = x] = m[q[h]] + 1;
				lab[x] = rgb2lab(x);
			}
		}
	}
	return [m, lab];
}

display(14199677);

Upd on 7.7:由于光柱接触到第一个玻璃前的颜色即为白色染色玻璃的颜色,所以代码还可以加入无玻璃的情况。

原文