# 詰まり具合: 煤(体積割合f)と水(n=1.33)の Maxwell-Garnett 有効媒質の球、直径 D。煤の量(体積)あたりで比べる
import numpy as np, miepython, colour
msoot = 1.95 + 0.79j; mw = 1.33
lam = np.arange(400, 701, 10).astype(float); Rg = 0.85
def mg(f):
    es, ew = msoot ** 2, mw ** 2
    e = ew * (es + 2 * ew + 2 * f * (es - ew)) / (es + 2 * ew - f * (es - ew))
    return np.sqrt(e)
def KS(D, f):
    m = mg(f) / mw                       # 水の中の粒として相対屈折率
    x = np.pi * D * mw / lam
    qe, qs, qb, g = miepython.mie(np.conj(m), x)
    qa = qe - qs
    vs = f * D                           # 煤の体積 ∝ f D^3、断面積 ∝ D^2 → 煤の体積あたり Q/(f D)
    return 2 * qa / vs, 0.75 * qs * (1 - g) / vs
def R_layer(K, S, X):
    S = np.maximum(S, 1e-12); a = 1 + K / S; b = np.sqrt(a * a - 1)
    sh, ch = np.sinh(b * S * X), np.cosh(b * S * X)
    return (1 - Rg * (a - b * ch / sh)) / (a - Rg + b * ch / sh)
def color(D, f):
    K, S = KS(D, f); i = 15; lo, hi = 1e-6, 1e6
    for _ in range(200):
        X = np.sqrt(lo * hi); r = R_layer(K, S, X)[i]
        if r > 0.45: lo = X
        else: hi = X
    sd = colour.SpectralDistribution(dict(zip(lam, R_layer(K, S, X))))
    return colour.XYZ_to_Lab(colour.sd_to_XYZ(sd, illuminant=colour.SDS_ILLUMINANTS['D65']) / 100)
for D in [300, 400]:
    print(f"直径{D}nm   f   a*     b*")
    for f in [0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 1.0]:
        L, A, B = color(D, f); print(f"         {f:4.2f}  {A:5.2f}  {B:5.2f}")
print("参照: 直径40nm f=1 ->", np.round(color(40, 1.0)[1:], 2))
