|
@@ -1,18 +1,6 @@
|
|
|
-import { SRGBColorSpace, LinearSRGBColorSpace, DisplayP3ColorSpace, } from '../constants.js';
|
|
|
+import { SRGBColorSpace, LinearSRGBColorSpace, DisplayP3ColorSpace, LinearDisplayP3ColorSpace, Rec709Primaries, P3Primaries, SRGBTransfer, LinearTransfer, NoColorSpace, } from '../constants.js';
|
|
|
import { Matrix3 } from './Matrix3.js';
|
|
|
|
|
|
-export function SRGBToLinear( c ) {
|
|
|
-
|
|
|
- return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-export function LinearToSRGB( c ) {
|
|
|
-
|
|
|
- return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055;
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* Matrices converting P3 <-> Rec. 709 primaries, without gamut mapping
|
|
|
* or clipping. Based on W3C specifications for sRGB and Display P3,
|
|
@@ -25,50 +13,57 @@ export function LinearToSRGB( c ) {
|
|
|
* - http://www.russellcottrell.com/photo/matrixCalculator.htm
|
|
|
*/
|
|
|
|
|
|
-const LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 = /*@__PURE__*/ new Matrix3().fromArray( [
|
|
|
- 0.8224621, 0.0331941, 0.0170827,
|
|
|
- 0.1775380, 0.9668058, 0.0723974,
|
|
|
- - 0.0000001, 0.0000001, 0.9105199
|
|
|
-] );
|
|
|
-
|
|
|
-const LINEAR_DISPLAY_P3_TO_LINEAR_SRGB = /*@__PURE__*/ new Matrix3().fromArray( [
|
|
|
- 1.2249401, - 0.0420569, - 0.0196376,
|
|
|
- - 0.2249404, 1.0420571, - 0.0786361,
|
|
|
- 0.0000001, 0.0000000, 1.0982735
|
|
|
-] );
|
|
|
-
|
|
|
-function DisplayP3ToLinearSRGB( color ) {
|
|
|
+const LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 = /*@__PURE__*/ new Matrix3().set(
|
|
|
+ 0.8224621, 0.177538, 0.0,
|
|
|
+ 0.0331941, 0.9668058, 0.0,
|
|
|
+ 0.0170827, 0.0723974, 0.9105199,
|
|
|
+);
|
|
|
|
|
|
- // Display P3 uses the sRGB transfer functions
|
|
|
- return color.convertSRGBToLinear().applyMatrix3( LINEAR_DISPLAY_P3_TO_LINEAR_SRGB );
|
|
|
+const LINEAR_DISPLAY_P3_TO_LINEAR_SRGB = /*@__PURE__*/ new Matrix3().set(
|
|
|
+ 1.2249401, - 0.2249404, 0.0,
|
|
|
+ - 0.0420569, 1.0420571, 0.0,
|
|
|
+ - 0.0196376, - 0.0786361, 1.0982735
|
|
|
+);
|
|
|
|
|
|
-}
|
|
|
-
|
|
|
-function LinearSRGBToDisplayP3( color ) {
|
|
|
-
|
|
|
- // Display P3 uses the sRGB transfer functions
|
|
|
- return color.applyMatrix3( LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 ).convertLinearToSRGB();
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-// Conversions from <source> to Linear-sRGB reference space.
|
|
|
-const TO_LINEAR = {
|
|
|
- [ LinearSRGBColorSpace ]: ( color ) => color,
|
|
|
- [ SRGBColorSpace ]: ( color ) => color.convertSRGBToLinear(),
|
|
|
- [ DisplayP3ColorSpace ]: DisplayP3ToLinearSRGB,
|
|
|
+/**
|
|
|
+ * Defines supported color spaces by transfer function and primaries,
|
|
|
+ * and provides conversions to/from the Linear-sRGB reference space.
|
|
|
+ */
|
|
|
+const COLOR_SPACES = {
|
|
|
+ [ LinearSRGBColorSpace ]: {
|
|
|
+ transfer: LinearTransfer,
|
|
|
+ primaries: Rec709Primaries,
|
|
|
+ toReference: ( color ) => color,
|
|
|
+ fromReference: ( color ) => color,
|
|
|
+ },
|
|
|
+ [ SRGBColorSpace ]: {
|
|
|
+ transfer: SRGBTransfer,
|
|
|
+ primaries: Rec709Primaries,
|
|
|
+ toReference: ( color ) => color.convertSRGBToLinear(),
|
|
|
+ fromReference: ( color ) => color.convertLinearToSRGB(),
|
|
|
+ },
|
|
|
+ [ LinearDisplayP3ColorSpace ]: {
|
|
|
+ transfer: LinearTransfer,
|
|
|
+ primaries: P3Primaries,
|
|
|
+ toReference: ( color ) => color.applyMatrix3( LINEAR_DISPLAY_P3_TO_LINEAR_SRGB ),
|
|
|
+ fromReference: ( color ) => color.applyMatrix3( LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 ),
|
|
|
+ },
|
|
|
+ [ DisplayP3ColorSpace ]: {
|
|
|
+ transfer: SRGBTransfer,
|
|
|
+ primaries: P3Primaries,
|
|
|
+ toReference: ( color ) => color.convertSRGBToLinear().applyMatrix3( LINEAR_DISPLAY_P3_TO_LINEAR_SRGB ),
|
|
|
+ fromReference: ( color ) => color.applyMatrix3( LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 ).convertLinearToSRGB(),
|
|
|
+ },
|
|
|
};
|
|
|
|
|
|
-// Conversions to <target> from Linear-sRGB reference space.
|
|
|
-const FROM_LINEAR = {
|
|
|
- [ LinearSRGBColorSpace ]: ( color ) => color,
|
|
|
- [ SRGBColorSpace ]: ( color ) => color.convertLinearToSRGB(),
|
|
|
- [ DisplayP3ColorSpace ]: LinearSRGBToDisplayP3,
|
|
|
-};
|
|
|
+const SUPPORTED_WORKING_COLOR_SPACES = new Set( [ LinearSRGBColorSpace, LinearDisplayP3ColorSpace ] );
|
|
|
|
|
|
export const ColorManagement = {
|
|
|
|
|
|
enabled: true,
|
|
|
|
|
|
+ _workingColorSpace: LinearSRGBColorSpace,
|
|
|
+
|
|
|
get legacyMode() {
|
|
|
|
|
|
console.warn( 'THREE.ColorManagement: .legacyMode=false renamed to .enabled=true in r150.' );
|
|
@@ -87,13 +82,19 @@ export const ColorManagement = {
|
|
|
|
|
|
get workingColorSpace() {
|
|
|
|
|
|
- return LinearSRGBColorSpace;
|
|
|
+ return this._workingColorSpace;
|
|
|
|
|
|
},
|
|
|
|
|
|
set workingColorSpace( colorSpace ) {
|
|
|
|
|
|
- console.warn( 'THREE.ColorManagement: .workingColorSpace is readonly.' );
|
|
|
+ if ( ! SUPPORTED_WORKING_COLOR_SPACES.has( colorSpace ) ) {
|
|
|
+
|
|
|
+ throw new Error( `Unsupported working color space, "${ colorSpace }".` );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ this._workingColorSpace = colorSpace;
|
|
|
|
|
|
},
|
|
|
|
|
@@ -105,29 +106,50 @@ export const ColorManagement = {
|
|
|
|
|
|
}
|
|
|
|
|
|
- const sourceToLinear = TO_LINEAR[ sourceColorSpace ];
|
|
|
- const targetFromLinear = FROM_LINEAR[ targetColorSpace ];
|
|
|
+ const sourceToReference = COLOR_SPACES[ sourceColorSpace ].toReference;
|
|
|
+ const targetFromReference = COLOR_SPACES[ targetColorSpace ].fromReference;
|
|
|
|
|
|
- if ( sourceToLinear === undefined || targetFromLinear === undefined ) {
|
|
|
+ return targetFromReference( sourceToReference( color ) );
|
|
|
|
|
|
- throw new Error( `Unsupported color space conversion, "${ sourceColorSpace }" to "${ targetColorSpace }".` );
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ fromWorkingColorSpace: function ( color, targetColorSpace ) {
|
|
|
|
|
|
- return targetFromLinear( sourceToLinear( color ) );
|
|
|
+ return this.convert( color, this._workingColorSpace, targetColorSpace );
|
|
|
|
|
|
},
|
|
|
|
|
|
- fromWorkingColorSpace: function ( color, targetColorSpace ) {
|
|
|
+ toWorkingColorSpace: function ( color, sourceColorSpace ) {
|
|
|
|
|
|
- return this.convert( color, this.workingColorSpace, targetColorSpace );
|
|
|
+ return this.convert( color, sourceColorSpace, this._workingColorSpace );
|
|
|
|
|
|
},
|
|
|
|
|
|
- toWorkingColorSpace: function ( color, sourceColorSpace ) {
|
|
|
+ getPrimaries: function ( colorSpace ) {
|
|
|
+
|
|
|
+ return COLOR_SPACES[ colorSpace ].primaries;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ getTransfer: function ( colorSpace ) {
|
|
|
|
|
|
- return this.convert( color, sourceColorSpace, this.workingColorSpace );
|
|
|
+ if ( colorSpace === NoColorSpace ) return LinearTransfer;
|
|
|
+
|
|
|
+ return COLOR_SPACES[ colorSpace ].transfer;
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
+
|
|
|
+
|
|
|
+export function SRGBToLinear( c ) {
|
|
|
+
|
|
|
+ return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+export function LinearToSRGB( c ) {
|
|
|
+
|
|
|
+ return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055;
|
|
|
+
|
|
|
+}
|