ColorManagement.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { SRGBColorSpace, LinearSRGBColorSpace } from '../constants.js';
  2. export function SRGBToLinear( c ) {
  3. return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
  4. }
  5. export function LinearToSRGB( c ) {
  6. return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055;
  7. }
  8. // RGB-to-RGB transforms, defined as `FN[InputColorSpace][OutputColorSpace] → conversionFn`.
  9. const FN = {
  10. [ SRGBColorSpace ]: { [ LinearSRGBColorSpace ]: SRGBToLinear },
  11. [ LinearSRGBColorSpace ]: { [ SRGBColorSpace ]: LinearToSRGB },
  12. };
  13. export const ColorManagement = {
  14. enabled: false,
  15. get legacyMode() {
  16. console.warn( 'THREE.ColorManagement: .legacyMode=false renamed to .enabled=true in r147.' );
  17. return ! this.enabled;
  18. },
  19. set legacyMode( legacyMode ) {
  20. console.warn( 'THREE.ColorManagement: .legacyMode=false renamed to .enabled=true in r147.' );
  21. this.enabled = ! legacyMode;
  22. },
  23. get workingColorSpace() {
  24. return LinearSRGBColorSpace;
  25. },
  26. set workingColorSpace( colorSpace ) {
  27. console.warn( 'THREE.ColorManagement: .workingColorSpace is readonly.' );
  28. },
  29. convert: function ( color, sourceColorSpace, targetColorSpace ) {
  30. if ( this.enabled === false || sourceColorSpace === targetColorSpace || ! sourceColorSpace || ! targetColorSpace ) {
  31. return color;
  32. }
  33. if ( FN[ sourceColorSpace ] && FN[ sourceColorSpace ][ targetColorSpace ] !== undefined ) {
  34. const fn = FN[ sourceColorSpace ][ targetColorSpace ];
  35. color.r = fn( color.r );
  36. color.g = fn( color.g );
  37. color.b = fn( color.b );
  38. return color;
  39. }
  40. throw new Error( 'Unsupported color space conversion.' );
  41. },
  42. fromWorkingColorSpace: function ( color, targetColorSpace ) {
  43. return this.convert( color, this.workingColorSpace, targetColorSpace );
  44. },
  45. toWorkingColorSpace: function ( color, sourceColorSpace ) {
  46. return this.convert( color, sourceColorSpace, this.workingColorSpace );
  47. },
  48. };