summaryrefslogtreecommitdiff
path: root/src/tests.py
blob: 9f20b3b41efc6ba151db88bf06b7164ac911f29d (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from crl import *
import random

def pg(name):
	pt()
	print("\t%s" % name.upper())
first = True
def pt(name=None):
	global first
	if first:
		first = False
	else:
		print("OK")

	if name:
		print("%s%s" % (name, "." * (40 - len(name))), end="")
	else:
		first = True
def fail():
	print("FAILED")

def relerr(x, y):
	return np.abs(x - y) / y


pg("D series (SPD synthesis)")


for T, ref in [[5000, Illuminants.D50],
               [5500, Illuminants.D55],
               [6500, Illuminants.D65]]:
	pt(f"D{T//100}")
	spec = CIEDaylightSpectrum(T)

	wvl = np.linspace(380, 780, 1000)
	Yspec = spec.Yi(wvl)
	Yref = ref.Yi(wvl)
	error = np.abs(Yspec - Yref) / Yref
	if np.max(error) > 0.01:
		fail()
		exit(1)

pg("Color space inversion tests")

spaces = [sRGB, xyY, UVW, uvY, UVWstarD65, UVstarNormedD65, uvY76, LabstarD65, LuvstarD65]

for CS in spaces:
	pt(CS.full_name)

	for i in range(150):
		X0 = XYZ(random.random(), random.random(), random.random())
		X = list(CS.from_XYZ(X0).to_XYZ())

		for i in range(3):
			if abs(X[i] - X0[i]) > 1e-9:
				fail()
				print(f" Input: {[*X0]}")
				print(f"Output: {[*X]}")
				exit(1)

pg("F series (CCT, CRI Ra)")

F_xs = [0.3131, 0.3721, 0.4091, 0.4402, 0.3138, 0.3779, 0.3129, 0.3458, 0.3741, 0.3458, 0.3805, 0.437]
F_ys = [0.3371, 0.3751, 0.3941, 0.4031, 0.3452, 0.3882, 0.3292, 0.3586, 0.3727, 0.3588, 0.3769, 0.4042]
F_CCTs = [6430, 4230, 3450, 2940, 6350, 4150, 6500, 5000, 4150, 5000, 4000, 3000]
F_Ras = [76, 64, 57, 51, 72, 59, 90, 95, 90, 81, 83, 83]

for i in range(12):
	pt(f"F{i + 1}")

	spec = Spectrum(tables.illuminants_F[:, 0], tables.illuminants_F[:, 1 + i])

	x, y, _ = xyY(spec)
	if relerr(x, F_xs[i]) > 0.01 or relerr(y, F_ys[i]) > 0.01:
		fail()
		print(f"Wrong chromaticity: {x}, {y} != {F_xs[i]}, {F_ys[i]}")
		#exit(1)
	
	CCT, _ = cct(spec)
	if relerr(CCT, F_CCTs[i]) > 0.01:
		fail()
		print(f"Wrong CCT: {CCT} != {F_CCTs[i]}")
		#exit(1)

	res = cri_Ra(spec)
	if round(res.CRI) != F_Ras[i]:
		fail()
		print(f"Wrong CRI Ra: {res.CRI} != {F_Ras[i]}")
		#exit(1)

pt()