1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { SRGBColorSpace, LinearSRGBColorSpace } from '../constants.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;
- }
- // RGB-to-RGB transforms, defined as `FN[InputColorSpace][OutputColorSpace] → conversionFn`.
- const FN = {
- [ SRGBColorSpace ]: { [ LinearSRGBColorSpace ]: SRGBToLinear },
- [ LinearSRGBColorSpace ]: { [ SRGBColorSpace ]: LinearToSRGB },
- };
- export const ColorManagement = {
- enabled: false,
- get legacyMode() {
- console.warn( 'THREE.ColorManagement: .legacyMode=false renamed to .enabled=true in r147.' );
- return ! this.enabled;
- },
- set legacyMode( legacyMode ) {
- console.warn( 'THREE.ColorManagement: .legacyMode=false renamed to .enabled=true in r147.' );
- 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;
- }
- if ( FN[ sourceColorSpace ] && FN[ sourceColorSpace ][ targetColorSpace ] !== undefined ) {
- const fn = FN[ sourceColorSpace ][ targetColorSpace ];
- color.r = fn( color.r );
- color.g = fn( color.g );
- color.b = fn( color.b );
- return color;
- }
- throw new Error( 'Unsupported color space conversion.' );
- },
- fromWorkingColorSpace: function ( color, targetColorSpace ) {
- return this.convert( color, this.workingColorSpace, targetColorSpace );
- },
- toWorkingColorSpace: function ( color, sourceColorSpace ) {
- return this.convert( color, sourceColorSpace, this.workingColorSpace );
- },
- };
|