UNPKG

@chayns/colors

Version:

JavaScript utility functions for the calculation of colors for chayns

27 lines (26 loc) 800 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = rgb255ToHex; var _is = require("../../utils/is"); function componentToHex(c) { var hex = Math.round(c).toString(16); return hex.length === 1 ? "0".concat(hex) : hex; } function rgb255ToHex(rgb) { if (!(0, _is.isRgb255)(rgb)) { return null; } var r = Math.round(rgb.r); var g = Math.round(rgb.g); var b = Math.round(rgb.b); if ('a' in rgb) { var a = Math.round(rgb.a * 255); if ((0, _is.isNumber)(rgb.a)) { return "#".concat(componentToHex(r)).concat(componentToHex(g)).concat(componentToHex(b)).concat(componentToHex(a)); } } return "#".concat(componentToHex(r)).concat(componentToHex(g)).concat(componentToHex(b)); } //# sourceMappingURL=rgb255ToHex.js.map