# ColorTemperature

It is a part of [kch-rgbw-lib](https://github.com/kchinzei/kch-rgbw-lib).
See [README.md](https://github.com/kchinzei/kch-rgbw-lib/blob/master/README.md)
for general information.
Functions in this documents are in CIE 1931 chromaticity for `(x, y)` and in Kelvin for blackbody temperature `k`.

### Code snippet

In TypeScript/ES2015:

```TypeScript
import { CSpace, CSpaceTypes } from 'kch-rgbw-lib';
import { k2x, k2y, xy2k, fadeOutK, fadeInK } from 'kch-rgbw-lib';
```

## API

### Functions

##### k2x(k: number): number

##### k2y(k: number): number

Return (x, y) chromaticity of black body corresponding to the given color temperature in K.

##### xy2k(xy: CSpace): number

##### xy2k(x: number, y: number): number

Return correlated color temperature (CCT) corresponding to (x, y) chromaticity.
`xy` should be in 'xy' or 'xyY' of `CSpaceTypes`.
Other types will throw an exception.
Optimized McCamy's approximation is used. Note that returned CCT is
physically meaningful only when (x, y) is on or near the white light.
In particular, the area between blue and red is very inaccurate as shown
in the color mapping of the returned temperature.

![xy2k](./figs/xy2k.png "Mapping by xy2k()")

Although CCT by McCamy's approximation is known that it's isotherms are
[less accurate than other methods](https://cran.r-project.org/web/packages/spacesXYZ/vignettes/isotherms.pdf),
it gives the smallest error in [1000, 7000] k according to my experiment.

##### fadeOutK(xy0: CSpace, steps: number, fade?: (r: number) => number): CSpace[]

##### fadeInK(xy0: CSpace, steps: number, fade?: (r: number) => number): CSpace[]

Return array of CSpace, filling with interpolated points starting/ending
at xy0.
`xy0` should be in 'xy' or 'xyY' of `CSpaceTypes`.
Other types will throw an exception.
`fadeOutK()` ends / `fadeInK()` starts at the point where
color temperature is 1000 K.
`fade()` is a function used to interpolate between the start and end points.
`fade()` is called repeatedly with changing `r` from 0 to 1, like:

```typescript
const p: CSpace[] = new Array(steps) as CSpace[];
for (let i=0; i<steps; i++) {
  const r = fade(i / steps);
  (interpolate start (x,y) and end (x,y) by r)
  (interpolate colorTemperature of (x,y) and 1000 by r)
  p[i] = { interpolated x, y, k };
}
```

It is intended to mimicking lamp gradually turning off/on.

![fadeOutK](./figs/fadeOutK.png "Example points generated by fadeOutK()")

Note: when (x,y) is far from the black body curve, it behaves odd,
due to the limitation of `xy2k()`.

# License

The MIT License (MIT)
Copyright (c) K. 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.
