UNPKG

three

Version:

JavaScript 3D library

75 lines (41 loc) 1.65 kB
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; } // JavaScript RGB-to-RGB transforms, defined as // FN[InputColorSpace][OutputColorSpace] callback functions. const FN = { [ SRGBColorSpace ]: { [ LinearSRGBColorSpace ]: SRGBToLinear }, [ LinearSRGBColorSpace ]: { [ SRGBColorSpace ]: LinearToSRGB }, }; export const ColorManagement = { legacyMode: true, get workingColorSpace() { return LinearSRGBColorSpace; }, set workingColorSpace( colorSpace ) { console.warn( 'THREE.ColorManagement: .workingColorSpace is readonly.' ); }, convert: function ( color, sourceColorSpace, targetColorSpace ) { if ( this.legacyMode || 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 ); }, };