summaryrefslogtreecommitdiff
path: root/test_colorchecker.py
blob: b0c96883ff63854828927d4b36ddbe56992345fe (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
import numpy as np
from colour import *
from colour.plotting import *
from matplotlib import pyplot as plt
from jakob_hanika import jakob_hanika, model_sd



illuminant = "D65"
ill_xy = ILLUMINANTS["CIE 1931 2 Degree Standard Observer"][illuminant]
ill_sd = SpectralDistribution(ILLUMINANT_SDS[illuminant])



# Makes a comparison plot with SDs and swatches
def plot_comparison(target, matched_sd, label, error, ill_sd, show=True):
	if type(target) is SpectralDistribution:
		target_XYZ = sd_to_XYZ(target, illuminant=ill_sd) / 100
	else:
		target_XYZ = target
	target_RGB = np.clip(XYZ_to_sRGB(target_XYZ), 0, 1)
	target_swatch = ColourSwatch(label, target_RGB)
	matched_XYZ = sd_to_XYZ(matched_sd, illuminant=ill_sd) / 100
	matched_RGB = np.clip(XYZ_to_sRGB(matched_XYZ), 0, 1)
	matched_swatch = ColourSwatch("Model", matched_RGB)
      
	axes = plt.subplot(2, 1, 1)
	plt.title(label)
	if type(target) is SpectralDistribution:
		plot_multi_sds([target, matched_sd], axes=axes, standalone=False)
	else:
		plot_single_sd(matched_sd, axes=axes, standalone=False)
      
	axes = plt.subplot(2, 1, 2)
	plt.title("ΔE = %g" % error)
	plot_multi_colour_swatches([target_swatch, matched_swatch],
	                           standalone=show, axes=axes)

if __name__ == "__main__":
	# This demo goes through SDs in a color checker
	for name, sd in COLOURCHECKERS_SDS['ColorChecker N Ohta'].items():
		XYZ = sd_to_XYZ(sd, illuminant=ill_sd) / 100

		print("Color checker: The target is '%s' with X=%g, Y=%g, Z=%g" % (name, *XYZ))
		ccp, error = jakob_hanika(XYZ, ill_sd, ill_xy)
		matched_sd = model_sd(ccp)

		plot_comparison(sd, matched_sd, name, error, ill_sd)