Skip to content
This repository was archived by the owner on Nov 22, 2019. It is now read-only.

Commit c934b3e

Browse files
author
mfe
committed
Inverted matrices (XYZ to RGB)
1 parent 0d01824 commit c934b3e

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

lutLab/rgb_to_xyz_matrix.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Display RGB colorspaces to XYZ conversion matrixes
1+
""" Display RGB colorspaces to XYZ conversion matrices and their inverses
22
33
.. moduleauthor:: `Marie FETIVEAU <github.com/mfe>`_
44
@@ -92,6 +92,23 @@ def get_RGB_to_XYZ_matrix(xy_red, xy_green, xy_blue, xy_white):
9292
return RGB_to_XYZ
9393

9494

95+
def matrix_to_string(matrix, extra=""):
96+
return ("{0:.10f} {1:.10f} {2:.10f} {9}\n"
97+
"{3:.10f} {4:.10f} {5:.10f} {10}\n"
98+
"{6:.10f} {7:.10f} {8:.10f} {11} \n").format(matrix.item(0, 0),
99+
matrix.item(0, 1),
100+
matrix.item(0, 2),
101+
matrix.item(1, 0),
102+
matrix.item(1, 1),
103+
matrix.item(1, 2),
104+
matrix.item(2, 0),
105+
matrix.item(2, 1),
106+
matrix.item(2, 2),
107+
extra,
108+
extra,
109+
extra)
110+
111+
95112
def display_matrix(colorspace, format):
96113
"""Display RGB to XYZ matrix corresponding to colorspace and formatting
97114
as format
@@ -102,41 +119,27 @@ def display_matrix(colorspace, format):
102119
format (str): output format. simple, matrix, spimtx.
103120
104121
"""
105-
print "{0} to XYZ matrix ({1} output):\n".format(colorspace, format)
106122
try:
107-
colorspace = COLORSPACES[colorspace]
123+
colorspace_obj = COLORSPACES[colorspace]
108124
except KeyError:
109-
colorspace = PRIVATE_COLORSPACES[colorspace]
110-
matrix = get_RGB_to_XYZ_matrix(colorspace.get_red_primaries(),
111-
colorspace.get_green_primaries(),
112-
colorspace.get_blue_primaries(),
113-
colorspace.get_white_point())
125+
colorspace_obj = PRIVATE_COLORSPACES[colorspace]
126+
matrix = get_RGB_to_XYZ_matrix(colorspace_obj.get_red_primaries(),
127+
colorspace_obj.get_green_primaries(),
128+
colorspace_obj.get_blue_primaries(),
129+
colorspace_obj.get_white_point())
114130
if format == 'simple':
115-
print ("{0:.10f} {1:.10f} {2:.10f}\n"
116-
"{3:.10f} {4:.10f} {5:.10f}\n"
117-
"{6:.10f} {7:.10f} {8:.10f}\n").format(matrix.item(0, 0),
118-
matrix.item(0, 1),
119-
matrix.item(0, 2),
120-
matrix.item(1, 0),
121-
matrix.item(1, 1),
122-
matrix.item(1, 2),
123-
matrix.item(2, 0),
124-
matrix.item(2, 1),
125-
matrix.item(2, 2))
131+
matrix_dump = matrix_to_string(matrix)
132+
inv_matrix_dump = matrix_to_string(matrix.I)
126133
elif format == 'spimtx':
127-
print ("{0:.10f} {1:.10f} {2:.10f} 0\n"
128-
"{3:.10f} {4:.10f} {5:.10f} 0\n"
129-
"{6:.10f} {7:.10f} {8:.10f} 0\n").format(matrix.item(0, 0),
130-
matrix.item(0, 1),
131-
matrix.item(0, 2),
132-
matrix.item(1, 0),
133-
matrix.item(1, 1),
134-
matrix.item(1, 2),
135-
matrix.item(2, 0),
136-
matrix.item(2, 1),
137-
matrix.item(2, 2))
134+
matrix_dump = matrix_to_string(matrix, "0")
135+
inv_matrix_dump = matrix_to_string(matrix.I, "0")
138136
else:
139-
print matrix
137+
matrix_dump = "{0}".format(matrix)
138+
inv_matrix_dump = "{0}".format(matrix.I)
139+
print "{0} to XYZ matrix ({1} output):\n".format(colorspace, format)
140+
print matrix_dump
141+
print "XYZ to {0} matrix ({1} output):\n".format(colorspace, format)
142+
print inv_matrix_dump
140143

141144

142145
def __get_options():

0 commit comments

Comments
 (0)