Skip to content

Commit

Permalink
[CATs] recalc Bradford with consistent D65 D50 values
Browse files Browse the repository at this point in the history
  • Loading branch information
svgeesus committed May 4, 2021
1 parent a96e740 commit aa5a15d
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 6 deletions.
124 changes: 124 additions & 0 deletions scripts/RGB_matrix_maker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"""Calculate XYZ conversion matrices."""
"""Derived from https://gist.githubusercontent.com/facelessuser/7d3707734fa9bcf208ab4dabea830cdb/raw/52a6effaec9b8b3662d28cd1378799639d1b4ef7/matrix_calc.py """
import numpy as np

np.set_printoptions(precision=17, suppress='true', sign='-', floatmode='fixed')

"""From ASTM E308-01"""
""" white_d65 = [0.95047, 1.00000, 1.08883]"""

""" white_d50 = [0.96422, 1.00000, 0.82521]"""

""" From CIE 15:2004 table T.3 chromaticities"""

"""white_d65 = [0.31272 / 0.32903, 1.00000, (1.0 - 0.31272 - 0.32903) / 0.32903]"""

"""white_d50 = [0.34567 / 0.35851, 1.00000, (1.0 - 0.34567 - 0.35851) / 0.35851]"""

""" From CIE 15:2004 table T.3 XYZ"""

"""white_d65 = [0.9504, 1.0000, 1.0888]"""

"""white_d50 = [0.9642, 1.0000, 0.8251]"""

"""The four-digit chromaticity ones that everyone uses, including the sRGB standard, for compatibility"""

white_d65 = [0.3127 / 0.3290, 1.00000, (1.0 - 0.3127 - 0.3290) / 0.3290]
white_d50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585]


def get_matrix(wp, space):
"""Get the matrices for the specified space."""

if space == 'srgb':
xr = 0.64
yr = 0.33
xg = 0.30
yg = 0.60
xb = 0.15
yb = 0.06
elif space == 'display-p3':
xr = 0.68
yr = 0.32
xg = 0.265
yg = 0.69
xb = 0.150
yb = 0.060
elif space == 'rec2020':
xr = 0.708
yr = 0.292
xg = 0.17
yg = 0.797
xb = 0.131
yb = 0.046
elif space == 'a98-rgb':
xr = 0.64
yr = 0.33
xg = 0.21
yg = 0.71
xb = 0.15
yb = 0.06
elif space == 'prophoto-rgb':
xr = 0.734699
yr = 0.265301
xg = 0.159597
yg = 0.840403
xb = 0.036598
yb = 0.000105
else:
raise ValueError

m = [
[xr / yr, 1.0, (1.0 - xr - yr) / yr],
[xg / yg, 1.0, (1.0 - xg - yg) / yg],
[xb / yb, 1.0, (1.0 - xb - yb) / yb]
]
mi = np.linalg.inv(m)

r, g, b = np.dot(wp, mi)
rgb = [
[r],
[g],
[b]
]
rgb2xyz = np.multiply(rgb, m).transpose()
xyz2rgb = np.linalg.inv(rgb2xyz)

return rgb2xyz, xyz2rgb


if __name__ == "__main__":
print('===== sRGB =====')
to_xyz, from_xyz = get_matrix(white_d65, 'srgb')
print('--- rgb -> xyz ---')
print(to_xyz)
print('--- xyz -> rgb ---')
print(from_xyz)

print('===== Display P3 =====')
to_xyz, from_xyz = get_matrix(white_d65, 'display-p3')
print('--- rgb -> xyz ---')
print(to_xyz)
print('--- xyz -> rgb ---')
print(from_xyz)

print('===== Adobe 98 =====')
to_xyz, from_xyz = get_matrix(white_d65, 'a98-rgb')
print('--- rgb -> xyz ---')
print(to_xyz)
print('--- xyz -> rgb ---')
print(from_xyz)

print('===== Rec.2020 =====')
to_xyz, from_xyz = get_matrix(white_d65, 'rec2020')
print('--- rgb -> xyz ---')
print(to_xyz)
print('--- xyz -> rgb ---')
print(from_xyz)

print('===== ProPhoto =====')
to_xyz, from_xyz = get_matrix(white_d50, 'prophoto-rgb')
print('--- rgb -> xyz ---')
print(to_xyz)
print('--- xyz -> rgb ---')
print(from_xyz)
24 changes: 18 additions & 6 deletions src/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,17 +537,29 @@ export default class Color {
if (!env.M) {
if (env.W1 === Color.whites.D65 && env.W2 === Color.whites.D50) {
// Linear Bradford CAT
// env.M = [
// [ 1.0478112, 0.0228866, -0.0501270],
// [ 0.0295424, 0.9904844, -0.0170491],
// [-0.0092345, 0.0150436, 0.7521316]
// ];

env.M = [
[ 1.0478112, 0.0228866, -0.0501270],
[ 0.0295424, 0.9904844, -0.0170491],
[-0.0092345, 0.0150436, 0.7521316]
[ 1.0479298208405488, 0.022946793341019088, -0.05019222954313557 ],
[ 0.029627815688159344, 0.990434484573249, -0.01707382502938514 ],
[ -0.009243058152591178, 0.015055144896577895, 0.7518742899580008 ]
];
}
else if (env.W1 === Color.whites.D50 && env.W2 === Color.whites.D65) {
// env.M = [
// [ 0.9555766, -0.0230393, 0.0631636],
// [-0.0282895, 1.0099416, 0.0210077],
// [ 0.0122982, -0.0204830, 1.3299098]
// ];

env.M = [
[ 0.9555766, -0.0230393, 0.0631636],
[-0.0282895, 1.0099416, 0.0210077],
[ 0.0122982, -0.0204830, 1.3299098]
[ 0.9554734527042182, -0.023098536874261423, 0.0632593086610217 ],
[ -0.028369706963208136, 1.0099954580058226, 0.021041398966943008 ],
[ 0.012314001688319899, -0.020507696433477912, 1.3303659366080753 ]
];
}
}
Expand Down

0 comments on commit aa5a15d

Please sign in to comment.