Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CIE } from './cie';
  2. import { RGB } from './srgb';
  3.  
  4. /**
  5.  * Wavelength defined by nanometers
  6.  */
  7. export class Wavelength {
  8.     public value: number;
  9.  
  10.     public constructor(wavelength: number) {
  11.         this.value = wavelength;
  12.     }
  13.  
  14.     /**
  15.      * Convert wavelength to RGB color model representation.
  16.      * @returns RGB color model representation
  17.      */
  18.     public toRGB(): RGB {
  19.         let x = this.xCIE();
  20.         let y = this.yCIE();
  21.         let z = this.zCIE();
  22.  
  23.         let r = this.gammaCorr(3.2404542 * x - 1.5371385 * y - 0.4985314 * z);
  24.         let g = this.gammaCorr(-0.969266 * x + 1.8760108 * y + 0.041556 * z);
  25.         let b = this.gammaCorr(0.0556434 * x - 0.2040259 * y + 1.0572252 * z);
  26.  
  27.         return new RGB(r, g, b);
  28.     }
  29.  
  30.     /**
  31.      * Convert wavelength to CIE color model representation.
  32.      * @returns RGB color model representation
  33.      */
  34.     public toCIE(): CIE {
  35.         return new CIE(this.xCIE(), this.yCIE(), this.zCIE());
  36.     }
  37.  
  38.     private xCIE(): number {
  39.         let t1 = (this.value - 442.0) * (this.value < 442.0 ? 0.0624 : 0.0374);
  40.         let t2 = (this.value - 599.8) * (this.value < 599.8 ? 0.0264 : 0.0323);
  41.         let t3 = (this.value - 501.1) * (this.value < 501.1 ? 0.049 : 0.0382);
  42.         return (
  43.             0.362 * Math.exp(-0.5 * t1 * t1) +
  44.             1.056 * Math.exp(-0.5 * t2 * t2) -
  45.             0.065 * Math.exp(-0.5 * t3 * t3)
  46.         );
  47.     }
  48.  
  49.     private yCIE(): number {
  50.         let t1 = (this.value - 568.8) * (this.value < 568.8 ? 0.0213 : 0.0247);
  51.         let t2 = (this.value - 530.9) * (this.value < 530.9 ? 0.0613 : 0.0322);
  52.         return (
  53.             0.821 * Math.exp(-0.5 * t1 * t1) + 0.286 * Math.exp(-0.5 * t2 * t2)
  54.         );
  55.     }
  56.  
  57.     private zCIE(): number {
  58.         let t1 = (this.value - 437.0) * (this.value < 437.0 ? 0.0845 : 0.0278);
  59.         let t2 = (this.value - 459.0) * (this.value < 459.0 ? 0.0385 : 0.0725);
  60.         return (
  61.             1.217 * Math.exp(-0.5 * t1 * t1) + 0.681 * Math.exp(-0.5 * t2 * t2)
  62.         );
  63.     }
  64.  
  65.     /**
  66.      * Gamma correction for RGB, r, g, and b, values.
  67.      * @param u value to be gamma corrected
  68.      * @returns gamma corrected value u
  69.      */
  70.     private gammaCorr(u: number): number {
  71.         if (Math.abs(u) < 0.0031308) return 12.92 * u;
  72.         return 1.055 * Math.pow(u, 0.41666) - 0.055;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement