Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { CIE } from './cie';
- import { RGB } from './srgb';
- /**
- * Wavelength defined by nanometers
- */
- export class Wavelength {
- public value: number;
- public constructor(wavelength: number) {
- this.value = wavelength;
- }
- /**
- * Convert wavelength to RGB color model representation.
- * @returns RGB color model representation
- */
- public toRGB(): RGB {
- let x = this.xCIE();
- let y = this.yCIE();
- let z = this.zCIE();
- let r = this.gammaCorr(3.2404542 * x - 1.5371385 * y - 0.4985314 * z);
- let g = this.gammaCorr(-0.969266 * x + 1.8760108 * y + 0.041556 * z);
- let b = this.gammaCorr(0.0556434 * x - 0.2040259 * y + 1.0572252 * z);
- return new RGB(r, g, b);
- }
- /**
- * Convert wavelength to CIE color model representation.
- * @returns RGB color model representation
- */
- public toCIE(): CIE {
- return new CIE(this.xCIE(), this.yCIE(), this.zCIE());
- }
- private xCIE(): number {
- let t1 = (this.value - 442.0) * (this.value < 442.0 ? 0.0624 : 0.0374);
- let t2 = (this.value - 599.8) * (this.value < 599.8 ? 0.0264 : 0.0323);
- let t3 = (this.value - 501.1) * (this.value < 501.1 ? 0.049 : 0.0382);
- return (
- 0.362 * Math.exp(-0.5 * t1 * t1) +
- 1.056 * Math.exp(-0.5 * t2 * t2) -
- 0.065 * Math.exp(-0.5 * t3 * t3)
- );
- }
- private yCIE(): number {
- let t1 = (this.value - 568.8) * (this.value < 568.8 ? 0.0213 : 0.0247);
- let t2 = (this.value - 530.9) * (this.value < 530.9 ? 0.0613 : 0.0322);
- return (
- 0.821 * Math.exp(-0.5 * t1 * t1) + 0.286 * Math.exp(-0.5 * t2 * t2)
- );
- }
- private zCIE(): number {
- let t1 = (this.value - 437.0) * (this.value < 437.0 ? 0.0845 : 0.0278);
- let t2 = (this.value - 459.0) * (this.value < 459.0 ? 0.0385 : 0.0725);
- return (
- 1.217 * Math.exp(-0.5 * t1 * t1) + 0.681 * Math.exp(-0.5 * t2 * t2)
- );
- }
- /**
- * Gamma correction for RGB, r, g, and b, values.
- * @param u value to be gamma corrected
- * @returns gamma corrected value u
- */
- private gammaCorr(u: number): number {
- if (Math.abs(u) < 0.0031308) return 12.92 * u;
- return 1.055 * Math.pow(u, 0.41666) - 0.055;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement