Skip to content

Commit

Permalink
Add color_temp & kelvin support
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Feb 21, 2020
1 parent e979009 commit 4b451af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ node_js:
- 'node'

script:
- npm run check-lint
- npm test
- npm run check-lint
after_success: npm run coverage

deploy:
Expand Down
11 changes: 11 additions & 0 deletions card.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ class RGBLightCard extends HTMLElement {
if (color['color_name']) {
return color['color_name'];
}
if (color['color_temp'] || color['kelvin']) {
// https://git.io/JvRKs
let temp = parseInt(color['color_temp'], 10) || 1000000 / parseInt(color['kelvin'], 10);
temp = Math.max(154, Math.min(500, temp));
const lin = (tr, cr, val) => cr.map(c => ((val - tr[0]) * (c[1] - c[0])) / (tr[1] - tr[0]) + c[0]);
const center = (500 + 154) / 2;
const miredRange = temp < center ? [154, center] : [center, 500]; // https://git.io/JvRKR
// prettier-ignore
const rgbRange = temp < center ? [[166, 255], [209, 255], [255, 255]] : [[255, 255], [255, 160], [255, 0]];
return `rgb(${lin(miredRange, rgbRange, temp).join(',')})`;
}
if (Array.isArray(color['rgb_color']) && color['rgb_color'].length === 3) {
return `rgb(${color['rgb_color'].join(',')})`;
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"repository": "bokub/rgb-light-card",
"scripts": {
"dev": "serve .",
"lint": "prettier --write **/*.{js,json,md,yml,html}",
"check-lint": "prettier --check **/*.{js,json,md,yml,html}",
"lint": "prettier --write **/*.{js,md,yml,html} *.json",
"check-lint": "prettier --check **/*.{js,md,yml,html} *.json",
"test": "nyc ava",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
},
Expand Down

0 comments on commit 4b451af

Please sign in to comment.