temp-color
Version:
Convert any quantity to RGB color
78 lines (53 loc) • 2.25 kB
Markdown




[](https://codecov.io/gh/szymslo/temp-color)

`npm i temp-color`
`yarn add temp-color`
```js
import { tempToColor } from 'temp-color';
const { r, g, b } = tempToColor(10, -30, 30);
const { r, g, b } = tempToColor(10, 0, 100, 'half');
const { r, g, b } = tempToColor(10, -30, 30, 'extended');
```
```
tempToColor = (t: number, min: number, max: number, mode?: string) : {r: number, g: number, b: number}
```
* _t_ - value that will be scaled into RGB
* _min_ - lowest possible value (scale begins there)
* _max_ - highest possible value (scale ends there)
* _mode_ - OPTIONAL
### Mode
* **default** - scaling from blue to red -> no need to provide any additional parameters
* **extended** - scaling from violet-blue to violet-red -> add _'extended'_ parameter at the end
* **half** - scaling from green to red (from good to bad); no blue colors -> add _'half'_ parameter at the end
```js
import {tempToColor} from 'temp-color';
let isHeld = false;
const box = document.querySelector("#box");
const slider = document.querySelector("#slider");
slider.addEventListener('mousedown', () => {
isHeld = true;
});
slider.addEventListener('mouseup', () => {
isHeld = false;
});
slider.addEventListener('mousemove', () => {
if (isHeld) {
const {r,g,b} = tempToColor(parseFloat(slider.value), parseInt(slider.min), parseInt(slider.max));
box.style.background = `rgb(${r},${g},${b})`;
}
});
```