ColorManagement.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // JavaScript RGB-to-RGB transforms, defined as
  9. // FN[InputColorSpace][OutputColorSpace] callback functions.
  10. const FN = {
  11. [ SRGBColorSpace ]: { [ LinearSRGBColorSpace ]: SRGBToLinear },
  12. [ LinearSRGBColorSpace ]: { [ SRGBColorSpace ]: LinearToSRGB },
  13. };
  14. export const ColorManagement = {
  15. legacyMode: true,
  16. get workingColorSpace() {
  17. return LinearSRGBColorSpace;
  18. },
  19. set workingColorSpace( colorSpace ) {
  20. console.warn( 'THREE.ColorManagement: .workingColorSpace is readonly.' );
  21. },
  22. convert: function ( color, sourceColorSpace, targetColorSpace ) {
  23. if ( this.legacyMode || sourceColorSpace === targetColorSpace || ! sourceColorSpace || ! targetColorSpace ) {
  24. return color;
  25. }
  26. if ( FN[ sourceColorSpace ] && FN[ sourceColorSpace ][ targetColorSpace ] !== undefined ) {
  27. const fn = FN[ sourceColorSpace ][ targetColorSpace ];
  28. color.r = fn( color.r );
  29. color.g = fn( color.g );
  30. color.b = fn( color.b );
  31. return color;
  32. }
  33. throw new Error( 'Unsupported color space conversion.' );
  34. },
  35. fromWorkingColorSpace: function ( color, targetColorSpace ) {
  36. return this.convert( color, this.workingColorSpace, targetColorSpace );
  37. },
  38. toWorkingColorSpace: function ( color, sourceColorSpace ) {
  39. return this.convert( color, sourceColorSpace, this.workingColorSpace );
  40. },
  41. };