-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorManagement.js
More file actions
133 lines (83 loc) · 3.4 KB
/
Copy pathColorManagement.js
File metadata and controls
133 lines (83 loc) · 3.4 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { SRGBColorSpace, LinearSRGBColorSpace, DisplayP3ColorSpace, } 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,
* and ICC specifications for the D50 connection space. Values in/out
* are _linear_ sRGB and _linear_ Display P3.
*
* Note that both sRGB and Display P3 use the sRGB transfer functions.
*
* Reference:
* - 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 ) {
// Display P3 uses the sRGB transfer functions
return color.convertSRGBToLinear().applyMatrix3( LINEAR_DISPLAY_P3_TO_LINEAR_SRGB );
}
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,
};
// Conversions to <target> from Linear-sRGB reference space.
const FROM_LINEAR = {
[ LinearSRGBColorSpace ]: ( color ) => color,
[ SRGBColorSpace ]: ( color ) => color.convertLinearToSRGB(),
[ DisplayP3ColorSpace ]: LinearSRGBToDisplayP3,
};
export const ColorManagement = {
enabled: true,
get legacyMode() {
console.warn( 'THREE.ColorManagement: .legacyMode=false renamed to .enabled=true in r150.' );
return ! this.enabled;
},
set legacyMode( legacyMode ) {
console.warn( 'THREE.ColorManagement: .legacyMode=false renamed to .enabled=true in r150.' );
this.enabled = ! legacyMode;
},
get workingColorSpace() {
return LinearSRGBColorSpace;
},
set workingColorSpace( colorSpace ) {
console.warn( 'THREE.ColorManagement: .workingColorSpace is readonly.' );
},
convert: function ( color, sourceColorSpace, targetColorSpace ) {
if ( this.enabled === false || sourceColorSpace === targetColorSpace || ! sourceColorSpace || ! targetColorSpace ) {
return color;
}
const sourceToLinear = TO_LINEAR[ sourceColorSpace ];
const targetFromLinear = FROM_LINEAR[ targetColorSpace ];
if ( sourceToLinear === undefined || targetFromLinear === undefined ) {
throw new Error( `Unsupported color space conversion, "${ sourceColorSpace }" to "${ targetColorSpace }".` );
}
return targetFromLinear( sourceToLinear( color ) );
},
fromWorkingColorSpace: function ( color, targetColorSpace ) {
return this.convert( color, this.workingColorSpace, targetColorSpace );
},
toWorkingColorSpace: function ( color, sourceColorSpace ) {
return this.convert( color, sourceColorSpace, this.workingColorSpace );
},
};