From 368c91efc44ab7b3ace595e649d8659514ff4dbe Mon Sep 17 00:00:00 2001 From: Chris Lilley Date: Mon, 16 Jan 2023 09:30:36 +0200 Subject: [PATCH] Correct rec2100-hlg, #190 --- src/spaces/rec2020-linear.js | 5 ++++- src/spaces/rec2100-hlg.js | 25 ++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/spaces/rec2020-linear.js b/src/spaces/rec2020-linear.js index f1069d422..fc2b20de9 100644 --- a/src/spaces/rec2020-linear.js +++ b/src/spaces/rec2020-linear.js @@ -22,5 +22,8 @@ export default new RGBColorSpace({ name: "Linear REC.2020", white: "D65", toXYZ_M, - fromXYZ_M + fromXYZ_M, + formats: { + color: {}, + } }); diff --git a/src/spaces/rec2100-hlg.js b/src/spaces/rec2100-hlg.js index 57eda65dd..22a3f9f5e 100644 --- a/src/spaces/rec2100-hlg.js +++ b/src/spaces/rec2100-hlg.js @@ -7,6 +7,8 @@ const a = 0.17883277; const b = 0.28466892; // 1 - (4 * a) const c = 0.55991073; // 0.5 - a * Math.log(4 *a) +const scale = 3.7743; // Place 18% grey at HLG 0.38, so media white at 0.75 + export default new RGBColorSpace({ id: "rec2100hlg", cssid: "rec2100-hlg", @@ -18,21 +20,30 @@ export default new RGBColorSpace({ // given HLG encoded component in range [0, 1] // return media-white relative linear-light return RGB.map(function (val) { - if (val <= 1/12) { - return Math.sqrt( 3 * val); + // first the HLG EOTF + // ITU-R BT.2390-10 p.30 section + // 6.3 The hybrid log-gamma electro-optical transfer function (EOTF) + // Then scale by 3 so media white is 1.0 + if (val <= 0.5) { + return (val ** 2) / 3 * scale; } - return a * Math.log(12 * val - b) + c; + return Math.exp(((val - c) / a) + b) / 12 * scale; }); }, fromBase (RGB) { // given media-white relative linear-light + // where diffuse white is 1.0, // return HLG encoded component in range [0, 1] - // per ITU Rec BT.2390 return RGB.map(function (val) { - if (val <= 0.5) { - return (val ** 2) / 3; + // first scale to put linear-light media white at 1/3 + val /= scale; + // now the HLG OETF + // ITU-R BT.2390-10 p.23 + // 6.1 The hybrid log-gamma opto-electronic transfer function (OETF) + if (val <= 1/12) { + return Math.sqrt( 3 * val); } - return Math.exp(((val - c) / a) + b) / 12; + return a * Math.log(12 * val - b) + c; }); }, formats: {