/* The MIT License (MIT) Copyright (c) Kiyo Chinzei (kchinzei@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* RGB/RGBW LED class library. Make Asayake to Wake Project. Kiyo Chinzei https://github.com/kchinzei/kch-rgbw-lib */ import { checkWaveLength, nmMin } from './const'; import { CSpace } from './CSpace'; const nmStep = 5; // step of temperature in waveLengthTable // Obtain index to access waveLengthTable. const nmIndex = (nm: number) => Math.floor((nm - nmMin) / nmStep); export function xyIsInGamut(xy: CSpace, xyList?: CSpace[]): boolean { if (xy.type !== 'xy' && xy.type !== 'xyY') throw new Error('xyIsInGamut() requires a color in xy or xyY'); const x = xy.x; const y = xy.y; if (typeof(xyList) === 'undefined') { xyList = waveLengthTable; } // If (x,y) is inside of poligon formed by xyList. // the crossing number algorithm. // Point near the border of xyList may incorrectly assessed. // Translated from https://www.nttpc.co.jp/technology/number_algorithm.html let crossNum = 0; for (let i=0; i y)) || // Rule 2: Downward vertex. ((xyList[i].y > y) && (xyList[i+1].y <= y))) { // Rule 3: When rule 1 & 2 examined, rule 3 is also examined. // Rule 4: If vertex is rightside of the point. const vt = (y - xyList[i].y) / (xyList[i+1].y - xyList[i].y); // Find the vertex at the same y, and check if x of the vertex and the point. if (x < (xyList[i].x + (vt * (xyList[i+1].x - xyList[i].x)))) { crossNum++; } } } return ((crossNum % 2) === 1); } const waveLengthTable: CSpace[] = makeWaveLengthTable(); function makeWaveLengthTable(): CSpace[] { /* Wave length to CIE(x,y) Source: JIS Z8701:1999 */ const w: number[][] = [ [ 0.173134328, 0.004477612, 405 ], [ 0.172550575, 0.004760016, 410 ], [ 0.172023941, 0.004876967, 415 ], [ 0.171428571, 0.005102041, 420 ], [ 0.170313987, 0.005788138, 425 ], [ 0.168877521, 0.006900244, 430 ], [ 0.166895290, 0.008535284, 435 ], [ 0.164416541, 0.010857251, 440 ], [ 0.161120111, 0.013793103, 445 ], [ 0.156641662, 0.017704887, 450 ], [ 0.150985408, 0.022740193, 455 ], [ 0.143960396, 0.029702970, 460 ], [ 0.135502671, 0.039879121, 465 ], [ 0.124142313, 0.057814485, 470 ], [ 0.109594324, 0.086842511, 475 ], [ 0.091256205, 0.132684231, 480 ], [ 0.068761114, 0.200711322, 485 ], [ 0.045377198, 0.294951787, 490 ], [ 0.023459943, 0.412703479, 495 ], [ 0.008168028, 0.538423071, 500 ], [ 0.003858521, 0.654823151, 505 ], [ 0.013870246, 0.750186428, 510 ], [ 0.038851802, 0.812016021, 515 ], [ 0.074339401, 0.833822666, 520 ], [ 0.114154776, 0.826163941, 525 ], [ 0.154716276, 0.805833411, 530 ], [ 0.192840055, 0.781698565, 535 ], [ 0.229619673, 0.754329090, 540 ], [ 0.265775085, 0.724323925, 545 ], [ 0.301579570, 0.692366572, 550 ], [ 0.337396231, 0.658848333, 555 ], [ 0.373101544, 0.624450860, 560 ], [ 0.408748569, 0.589624631, 565 ], [ 0.444062464, 0.554713903, 570 ], [ 0.478774791, 0.520202307, 575 ], [ 0.512472036, 0.486577181, 580 ], [ 0.544786506, 0.454434115, 585 ], [ 0.575151311, 0.424232235, 590 ], [ 0.602932786, 0.396496634, 595 ], [ 0.627036600, 0.372491145, 600 ], [ 0.648233106, 0.351394916, 605 ], [ 0.665781260, 0.334019523, 610 ], [ 0.680098565, 0.319756486, 615 ], [ 0.691485918, 0.308352218, 620 ], [ 0.700606061, 0.299300699, 625 ], [ 0.707956800, 0.292043200, 630 ], [ 0.714059823, 0.285940177, 635 ], [ 0.719056028, 0.280943972, 640 ], [ 0.723046092, 0.276953908, 645 ], [ 0.725992318, 0.274007682, 650 ], [ 0.728271728, 0.271728272, 655 ], [ 0.729969013, 0.270030987, 660 ], [ 0.731001206, 0.268998794, 665 ], [ 0.731993300, 0.268006700, 670 ], [ 0.732718894, 0.267281106, 675 ], [ 0.733542320, 0.266457680, 680 ], [ 0.734375000, 0.265625000, 685 ], [ 0.734627832, 0.265372168, 690 ], [ 0.734883721, 0.265116279, 695 ], [ 0.735483871, 0.264516129, 700 ], [ 0.173134328, 0.004477612, 405 ] // Dummy to avoid error when nm=700 ]; const t: CSpace[] = new Array(w.length) as CSpace[]; for (let i=0; i xyProjection2Gamut(xy, false, xyList); export const xyMap2Gamut = (xy: CSpace, xyList: CSpace[] = waveLengthTable): CSpace => xyProjection2Gamut(xy, true, xyList); /* Project (x, y) to the polygon made of points in xyList, return the projected (= interpolated) (x, y) and the wavelength. If xyList is omitted, waveLengthTable[] is used, which is the gamut of the chromaticity space. xyIsInGamut(interpotaled point) should be true, but numerical error may exist. */ function xyProjection2Gamut(xy: CSpace, projectWhenInside: boolean, xyList: CSpace[]): CSpace { if (xy.type !== 'xy' && xy.type !== 'xyY') throw new Error('xyFit2Gamut() requires a start color in xy or xyY'); const x = xy.x; const y = xy.y; let isOpen = false; if (xyList === waveLengthTable) { // When waveLengthTable is used, we don't interpolate between UV and NIR. isOpen = true; } const isInside = xyIsInGamut(xy, xyList); // Not smart but working solution... check every distance! // Find the nearest point. let dMin = 100; // enough large let iMin = 0; for (let i=0; i