485 lines
13 KiB
TypeScript
485 lines
13 KiB
TypeScript
/** 元素周期表:等级 1~118 对应元素的基态电子排布 */
|
|
|
|
const SUBSHELLS: Array<[number, string, number]> = [
|
|
[1, 's', 2],
|
|
[2, 's', 2],
|
|
[2, 'p', 6],
|
|
[3, 's', 2],
|
|
[3, 'p', 6],
|
|
[4, 's', 2],
|
|
[3, 'd', 10],
|
|
[4, 'p', 6],
|
|
[5, 's', 2],
|
|
[4, 'd', 10],
|
|
[5, 'p', 6],
|
|
[6, 's', 2],
|
|
[4, 'f', 14],
|
|
[5, 'd', 10],
|
|
[6, 'p', 6],
|
|
[7, 's', 2],
|
|
[5, 'f', 14],
|
|
[6, 'd', 10],
|
|
[7, 'p', 6]
|
|
]
|
|
|
|
const NOBLE: Array<[number, string]> = [
|
|
[2, 'He'],
|
|
[10, 'Ne'],
|
|
[18, 'Ar'],
|
|
[36, 'Kr'],
|
|
[54, 'Xe'],
|
|
[86, 'Rn']
|
|
]
|
|
|
|
const SYMBOLS =
|
|
'H He Li Be B C N O F Ne Na Mg Al Si P S Cl Ar K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe Cs Ba La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn Fr Ra Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og'.split(
|
|
' '
|
|
)
|
|
|
|
/** 常见反常排布,直接写死 */
|
|
const EXCEPTIONS: Record<number, string> = {
|
|
24: '[Ar]3d5 4s1',
|
|
29: '[Ar]3d10 4s1',
|
|
41: '[Kr]4d4 5s1',
|
|
42: '[Kr]4d5 5s1',
|
|
44: '[Kr]4d7 5s1',
|
|
45: '[Kr]4d8 5s1',
|
|
46: '[Kr]4d10',
|
|
47: '[Kr]4d10 5s1',
|
|
57: '[Xe]5d1 6s2',
|
|
58: '[Xe]4f1 5d1 6s2',
|
|
64: '[Xe]4f7 5d1 6s2',
|
|
78: '[Xe]4f14 5d9 6s1',
|
|
79: '[Xe]4f14 5d10 6s1',
|
|
89: '[Rn]6d1 7s2',
|
|
90: '[Rn]6d2 7s2',
|
|
91: '[Rn]5f2 6d1 7s2',
|
|
92: '[Rn]5f3 6d1 7s2',
|
|
93: '[Rn]5f4 6d1 7s2',
|
|
96: '[Rn]5f7 6d1 7s2'
|
|
}
|
|
|
|
function fill(z: number): Array<{ n: number; l: string; e: number }> {
|
|
const out: Array<{ n: number; l: string; e: number }> = []
|
|
let left = z
|
|
for (const [n, l, cap] of SUBSHELLS) {
|
|
if (left <= 0) break
|
|
const e = Math.min(cap, left)
|
|
out.push({ n, l, e })
|
|
left -= e
|
|
}
|
|
return out
|
|
}
|
|
|
|
function configOf(z: number): string {
|
|
if (EXCEPTIONS[z]) return EXCEPTIONS[z]
|
|
|
|
let coreZ = 0
|
|
let coreSym = ''
|
|
for (const [zc, sym] of NOBLE) {
|
|
if (zc < z) {
|
|
coreZ = zc
|
|
coreSym = sym
|
|
}
|
|
}
|
|
|
|
const full = fill(z)
|
|
const core = fill(coreZ)
|
|
const parts: string[] = []
|
|
for (let i = 0; i < full.length; i++) {
|
|
const e = full[i].e - (core[i]?.e ?? 0)
|
|
if (e > 0) parts.push(`${full[i].n}${full[i].l}${e}`)
|
|
}
|
|
|
|
return coreSym ? `[${coreSym}]${parts.join(' ')}` : parts.join(' ')
|
|
}
|
|
|
|
const SUP = '⁰¹²³⁴⁵⁶⁷⁸⁹'
|
|
function sup(n: string): string {
|
|
return n
|
|
.split('')
|
|
.map((d) => SUP[Number(d)] ?? d)
|
|
.join('')
|
|
}
|
|
|
|
/** 把 4s1 写成 4s¹(只把电子数变上标) */
|
|
function superscriptConfig(cfg: string): string {
|
|
return cfg.replace(/(\d)([spdf])(\d+)/g, (_m, n: string, l: string, e: string) => `${n}${l}${sup(e)}`)
|
|
}
|
|
|
|
const BLOCK_COLOR: Record<string, string> = {
|
|
s: '#ffd54a',
|
|
p: '#6fc8ff',
|
|
d: '#8ef0b0',
|
|
f: '#cf9bff'
|
|
}
|
|
|
|
function blockOf(cfg: string): 's' | 'p' | 'd' | 'f' {
|
|
const coreOnly = /^\[[A-Za-z]{1,2}\]$/.test(cfg)
|
|
if (coreOnly) return cfg === '[He]' ? 's' : 'p'
|
|
const m = cfg.match(/(\d)([spdf])(\d+)(?![\s\S]*\d[spdf]\d)/)
|
|
const l = m?.[2]
|
|
return (l === 's' || l === 'p' || l === 'd' || l === 'f' ? l : 's') as 's' | 'p' | 'd' | 'f'
|
|
}
|
|
|
|
export interface ElementInfo {
|
|
z: number
|
|
symbol: string
|
|
config: string
|
|
configSuper: string
|
|
block: 's' | 'p' | 'd' | 'f'
|
|
color: string
|
|
}
|
|
|
|
export const ELEMENTS: ElementInfo[] = Array.from({ length: 118 }, (_, i) => {
|
|
const z = i + 1
|
|
const config = configOf(z)
|
|
const block = blockOf(config)
|
|
return {
|
|
z,
|
|
symbol: SYMBOLS[i] ?? '?',
|
|
config,
|
|
configSuper: superscriptConfig(config),
|
|
block,
|
|
color: BLOCK_COLOR[block]
|
|
}
|
|
})
|
|
|
|
export const MAX_LEVEL = ELEMENTS.length
|
|
|
|
export function elementOf(level: number): ElementInfo {
|
|
const idx = Math.min(Math.max(level, 1), MAX_LEVEL) - 1
|
|
return ELEMENTS[idx]
|
|
}
|
|
|
|
/** 卡片里的一行显示:K [Ar]4s¹ */
|
|
export function levelLabel(level: number): string {
|
|
const el = elementOf(level)
|
|
return `${el.symbol} ${el.configSuper}`
|
|
}
|
|
|
|
|
|
/** 元素中文名(按原子序数) */
|
|
export const ELEMENT_NAMES: string[] = [
|
|
'氢','氦','锂','铍','硼','碳','氮','氧','氟','氖',
|
|
'钠','镁','铝','硅','磷','硫','氯','氩','钾','钙',
|
|
'钪','钛','钒','铬','锰','铁','钴','镍','铜','锌',
|
|
'镓','锗','砷','硒','溴','氪','铷','锶','钇','锆',
|
|
'铌','钼','锝','钌','铑','钯','银','镉','铟','锡',
|
|
'锑','碲','碘','氙','铯','钡','镧','铈','镨','钕',
|
|
'钷','钐','铕','钆','铽','镝','钬','铒','铥','镱',
|
|
'镥','铪','钽','钨','铼','锇','铱','铂','金','汞',
|
|
'铊','铅','铋','钋','砹','氡','钫','镭','锕','钍',
|
|
'镤','铀','镎','钚','镅','锔','锫','锎','锿','镄',
|
|
'钔','锘','铹','𬬻','𬭊','𬭳','𬭛','𬭶','鿏','𫟼',
|
|
'𬬭','鿔','鿭','𫓧','镆','𫟷','鿬','鿫'
|
|
]
|
|
|
|
export function elementName(level: number): string {
|
|
return ELEMENT_NAMES[elementOf(level).z - 1] ?? ''
|
|
}
|
|
|
|
export interface WrappedLevelLabel {
|
|
prefix: string
|
|
symbol: string
|
|
name: string
|
|
config: string
|
|
z: number
|
|
}
|
|
|
|
/**
|
|
* 超 118 级后的循环显示:前缀带上上个周期的最大符号(118 Uuo),
|
|
* 再接 1H 1s¹ 这样的循环元素。
|
|
*/
|
|
export function levelLabelWrapped(level: number): WrappedLevelLabel {
|
|
const cycles = Math.floor((Math.max(1, level) - 1) / MAX_LEVEL)
|
|
const z = ((Math.max(1, level) - 1) % MAX_LEVEL) + 1
|
|
const el = elementOf(z)
|
|
return {
|
|
prefix: cycles > 0 ? '118 Uuo' : '',
|
|
symbol: el.symbol,
|
|
name: ELEMENT_NAMES[z - 1] ?? '',
|
|
config: el.configSuper,
|
|
z
|
|
}
|
|
}
|
|
|
|
|
|
export type ElementCategory =
|
|
| 'alkali'
|
|
| 'alkaline'
|
|
| 'transition'
|
|
| 'postmetal'
|
|
| 'metalloid'
|
|
| 'nonmetal'
|
|
| 'halogen'
|
|
| 'noble'
|
|
| 'lanthanide'
|
|
| 'actinide'
|
|
|
|
export const ELEMENT_CATEGORY_LABEL: Record<ElementCategory, string> = {
|
|
alkali: '碱金属',
|
|
alkaline: '碱土金属',
|
|
transition: '过渡金属',
|
|
postmetal: '主族金属',
|
|
metalloid: '类金属',
|
|
nonmetal: '非金属',
|
|
halogen: '卤素',
|
|
noble: '稀有气体',
|
|
lanthanide: '镧系',
|
|
actinide: '锕系'
|
|
}
|
|
|
|
export const CATEGORY_COLORS: Record<ElementCategory, string> = {
|
|
alkali: '#ff9d76',
|
|
alkaline: '#ffcf6b',
|
|
transition: '#8ee6c8',
|
|
postmetal: '#7fc7ff',
|
|
metalloid: '#b9e07a',
|
|
nonmetal: '#8fe1b0',
|
|
halogen: '#ffcf8a',
|
|
noble: '#c9a8ff',
|
|
lanthanide: '#ffb0d0',
|
|
actinide: '#f4a6a6'
|
|
}
|
|
|
|
const CATEGORY_SETS: Array<[ElementCategory, number[]]> = [
|
|
['alkali', [3, 11, 19, 37, 55, 87]],
|
|
['alkaline', [4, 12, 20, 38, 56, 88]],
|
|
['transition', [
|
|
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
|
39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
|
|
72, 73, 74, 75, 76, 77, 78, 79, 80,
|
|
104, 105, 106, 107, 108, 109, 110, 111, 112
|
|
]],
|
|
['postmetal', [13, 31, 49, 50, 81, 82, 83, 84, 113, 114, 115, 116]],
|
|
['metalloid', [5, 14, 32, 33, 51, 52]],
|
|
['nonmetal', [1, 6, 7, 8, 15, 16, 34]],
|
|
['halogen', [9, 17, 35, 53, 85, 117]],
|
|
['noble', [2, 10, 18, 36, 54, 86, 118]],
|
|
['lanthanide', [57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71]],
|
|
['actinide', [89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103]]
|
|
]
|
|
|
|
export function categoryOf(z: number): ElementCategory {
|
|
for (const [cat, list] of CATEGORY_SETS) {
|
|
if (list.includes(z)) return cat
|
|
}
|
|
return 'transition'
|
|
}
|
|
|
|
// ---------------------------------------------------------------- 周期表分区
|
|
// 按教科书五大分区:s / p / d / ds / f(与化学教材一致)
|
|
export type ElementZone = 's' | 'p' | 'd' | 'ds' | 'f'
|
|
|
|
export const ZONE_LABEL: Record<ElementZone, string> = {
|
|
s: 's 区',
|
|
p: 'p 区',
|
|
d: 'd 区',
|
|
ds: 'ds 区',
|
|
f: 'f 区'
|
|
}
|
|
|
|
/** 五大分区的标准配色(参照教材插图) */
|
|
export const ZONE_COLORS: Record<ElementZone, string> = {
|
|
s: '#e8352b',
|
|
p: '#8bd24b',
|
|
d: '#f6d64b',
|
|
ds: '#8fe0ea',
|
|
f: '#f3b9e7'
|
|
}
|
|
|
|
const ZONE_SOFT: Record<ElementZone, string> = {
|
|
s: '#f6b3ae',
|
|
p: '#cfe8ab',
|
|
d: '#fbeab0',
|
|
ds: '#c9f1f5',
|
|
f: '#f9d9f2'
|
|
}
|
|
|
|
const DS_ELEMENTS = [29, 30, 47, 48, 79, 80, 111, 112]
|
|
const S_ELEMENTS = [1, 2, 3, 4, 11, 12, 19, 20, 37, 38, 55, 56, 87, 88]
|
|
|
|
export function defaultZoneOf(z: number): ElementZone {
|
|
if ((z >= 57 && z <= 71) || (z >= 89 && z <= 103)) return 'f'
|
|
if (S_ELEMENTS.includes(z)) return 's'
|
|
if (DS_ELEMENTS.includes(z)) return 'ds'
|
|
// 过渡金属(去掉 ds 的 11、12 族)
|
|
if ((z >= 21 && z <= 28) || (z >= 39 && z <= 46) || (z >= 72 && z <= 78) || (z >= 104 && z <= 110)) {
|
|
return 'd'
|
|
}
|
|
return 'p'
|
|
}
|
|
|
|
export const ZONE_MEMBERS: Record<ElementZone, number[]> = {
|
|
s: S_ELEMENTS,
|
|
ds: DS_ELEMENTS,
|
|
d: ELEMENTS.filter((e) => defaultZoneOf(e.z) === 'd').map((e) => e.z),
|
|
f: ELEMENTS.filter((e) => defaultZoneOf(e.z) === 'f').map((e) => e.z),
|
|
p: ELEMENTS.filter((e) => defaultZoneOf(e.z) === 'p').map((e) => e.z)
|
|
}
|
|
|
|
/** 周期表配色方案:block = 按 s/p/d/ds/f 分区,category = 按元素分类 */
|
|
export type PeriodicScheme = 'block' | 'category'
|
|
|
|
export interface PeriodicStyle {
|
|
scheme: PeriodicScheme
|
|
/** 分区 / 分类颜色:block 用 s/p/d/ds/f(也可自定义分区 id),category 用分类 key */
|
|
colors?: Record<string, string>
|
|
/** 单格覆盖:元素序号 -> 颜色(最高优先级) */
|
|
cellColors?: Record<number, string>
|
|
/** 自定义分区归属:元素序号 -> 分区 id */
|
|
zoneOverrides?: Record<number, string>
|
|
/** 自定义分区名:分区 id -> 名称 */
|
|
zoneNames?: Record<string, string>
|
|
/** 底图背景色 */
|
|
bg?: string
|
|
}
|
|
|
|
export const DEFAULT_PERIODIC_STYLE: PeriodicStyle = {
|
|
scheme: 'block',
|
|
colors: { ...ZONE_COLORS },
|
|
cellColors: {},
|
|
zoneOverrides: {},
|
|
zoneNames: {},
|
|
bg: '#0e1420'
|
|
}
|
|
|
|
/** 取元素所属分区 / 分类 key(自定义归属优先) */
|
|
export function periodicKeyOf(
|
|
el: { z: number },
|
|
style: PeriodicStyle
|
|
): string {
|
|
const over = style.zoneOverrides?.[el.z]
|
|
if (over) return over
|
|
if (style.scheme === 'category') return categoryOf(el.z)
|
|
return defaultZoneOf(el.z)
|
|
}
|
|
|
|
export function periodicCellColor(
|
|
el: { z: number },
|
|
style: PeriodicStyle
|
|
): string {
|
|
const single = style.cellColors?.[el.z]
|
|
if (single) return single
|
|
const key = periodicKeyOf(el, style)
|
|
const colors = style.colors ?? {}
|
|
if (style.scheme === 'category') {
|
|
const cat = categoryOf(el.z)
|
|
return colors[key] ?? CATEGORY_COLORS[cat as ElementCategory] ?? '#8b98ad'
|
|
}
|
|
return colors[key] ?? ZONE_COLORS[key as ElementZone] ?? '#8b98ad'
|
|
}
|
|
|
|
/** 编辑器用:列出当前方案里的所有分区(含自定义分区) */
|
|
export function periodicZoneList(
|
|
style: PeriodicStyle
|
|
): Array<{ id: string; label: string; color: string; count: number }> {
|
|
const counts = new Map<string, number>()
|
|
for (const el of ELEMENTS) {
|
|
const k = periodicKeyOf(el, style)
|
|
counts.set(k, (counts.get(k) ?? 0) + 1)
|
|
}
|
|
const ids = new Set<string>(Object.keys(style.colors ?? {}))
|
|
for (const k of counts.keys()) ids.add(k)
|
|
const order: string[] =
|
|
style.scheme === 'category'
|
|
? (Object.keys(ELEMENT_CATEGORY_LABEL) as string[])
|
|
: ['s', 'p', 'd', 'ds', 'f']
|
|
const sorted = [...ids].sort((a, b) => {
|
|
const ia = order.indexOf(a)
|
|
const ib = order.indexOf(b)
|
|
return (ia < 0 ? 99 : ia) - (ib < 0 ? 99 : ib) || a.localeCompare(b)
|
|
})
|
|
return sorted.map((id) => ({
|
|
id,
|
|
label:
|
|
style.zoneNames?.[id] ??
|
|
(style.scheme === 'category'
|
|
? ELEMENT_CATEGORY_LABEL[id as ElementCategory] ?? id
|
|
: ZONE_LABEL[id as ElementZone] ?? id),
|
|
color:
|
|
style.colors?.[id] ??
|
|
(style.scheme === 'category'
|
|
? CATEGORY_COLORS[id as ElementCategory] ?? '#8b98ad'
|
|
: ZONE_COLORS[id as ElementZone] ?? '#8b98ad'),
|
|
count: counts.get(id) ?? 0
|
|
}))
|
|
}
|
|
|
|
|
|
export interface PeriodicPreset {
|
|
id: string
|
|
name: string
|
|
scheme: PeriodicScheme
|
|
colors: Record<string, string>
|
|
}
|
|
|
|
const SOFT_CATEGORY: Record<string, string> = {
|
|
alkali: '#ffc9b3',
|
|
alkaline: '#ffe3a3',
|
|
transition: '#d9f2e6',
|
|
postmetal: '#cfe7ff',
|
|
metalloid: '#e2f0c4',
|
|
nonmetal: '#d3f0dd',
|
|
halogen: '#ffe3bd',
|
|
noble: '#ead9ff',
|
|
lanthanide: '#ffd6e6',
|
|
actinide: '#f9d2d2'
|
|
}
|
|
|
|
export const PERIODIC_PRESETS: PeriodicPreset[] = [
|
|
{
|
|
id: 'spdf',
|
|
name: '五大分区 · 标准',
|
|
scheme: 'block',
|
|
colors: { ...ZONE_COLORS }
|
|
},
|
|
{
|
|
id: 'spdf-soft',
|
|
name: '五大分区 · 柔和',
|
|
scheme: 'block',
|
|
colors: { ...ZONE_SOFT }
|
|
},
|
|
{
|
|
id: 'category',
|
|
name: '元素分类 · 标准',
|
|
scheme: 'category',
|
|
colors: { ...CATEGORY_COLORS }
|
|
},
|
|
{
|
|
id: 'category-soft',
|
|
name: '元素分类 · 柔和',
|
|
scheme: 'category',
|
|
colors: SOFT_CATEGORY
|
|
}
|
|
]
|
|
|
|
|
|
/** 周期表格子的 (行, 列) 位置:镧系/锕系单独成行 */
|
|
export const ELEMENT_GRID_POS: Array<[number, number]> = (() => {
|
|
const pos: Array<[number, number]> = []
|
|
const set = (z: number, row: number, col: number): void => {
|
|
pos[z - 1] = [row, col]
|
|
}
|
|
set(1, 1, 1)
|
|
set(2, 1, 18)
|
|
for (let i = 0; i < 8; i++) {
|
|
set(3 + i, 2, i < 2 ? i + 1 : i + 11)
|
|
set(11 + i, 3, i < 2 ? i + 1 : i + 11)
|
|
}
|
|
for (let i = 0; i < 18; i++) {
|
|
set(19 + i, 4, i + 1)
|
|
set(37 + i, 5, i + 1)
|
|
}
|
|
set(55, 6, 1)
|
|
set(56, 6, 2)
|
|
for (let i = 0; i < 15; i++) set(57 + i, 8, 3 + i)
|
|
for (let i = 0; i < 15; i++) set(72 + i, 6, 4 + i)
|
|
set(87, 7, 1)
|
|
set(88, 7, 2)
|
|
for (let i = 0; i < 15; i++) set(89 + i, 9, 3 + i)
|
|
for (let i = 0; i < 15; i++) set(104 + i, 7, 4 + i)
|
|
return pos
|
|
})()
|