hyper/app/utils/to-electron-background-color.ts

21 lines
526 B
TypeScript
Raw Normal View History

// Packages
2019-12-25 00:52:32 -09:00
import Color from 'color';
// returns a background color that's in hex
// format including the alpha channel (e.g.: `#00000050`)
// input can be any css value (rgb, hsl, string…)
2019-12-25 00:52:32 -09:00
export default (bgColor: string) => {
const color = Color(bgColor);
if (color.alpha() === 1) {
return color.hex().toString();
}
// http://stackoverflow.com/a/11019879/1202488
const alphaHex = Math.round(color.alpha() * 255).toString(16);
2019-11-28 05:17:01 -09:00
return `#${alphaHex}${color
.hex()
.toString()
.substr(1)}`;
};