UNPKG

@platform/css

Version:

Helpers for working with inline CSS.

85 lines (84 loc) 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.darken = exports.lighten = exports.alpha = exports.toGrayHex = exports.toGrayAlpha = exports.format = exports.mix = exports.white = exports.black = exports.create = exports.RED = void 0; var tinycolor = require("tinycolor2"); exports.RED = "rgba(255, 0, 0, 0.1)"; function create(value) { return tinycolor(value); } exports.create = create; var black = function () { return create('black'); }; exports.black = black; var white = function () { return create('white'); }; exports.white = white; function mix(color1, color2, amount) { return tinycolor.mix(color1, color2, amount); } exports.mix = mix; function format(value) { if (value === undefined) { return undefined; } if (value === true) { return exports.RED; } if (typeof value === 'number') { return toGrayAlpha(value); } if (typeof value === 'string') { if (value.includes('url(')) return value; if (!value.includes('#') && !value.includes('rgb')) return "#".concat(value); } return value; } exports.format = format; function toGrayAlpha(value) { if (value < -1) { value = -1; } if (value > 1) { value = 1; } if (value < 0) { return "rgba(0, 0, 0, ".concat(Math.abs(value), ")"); } if (value > 0) { return "rgba(255, 255, 255, ".concat(value, ")"); } return "rgba(0, 0, 0, 0.0)"; } exports.toGrayAlpha = toGrayAlpha; function toGrayHex(value) { if (value < -1) { value = -1; } if (value > 1) { value = 1; } if (value < 0) { return (0, exports.white)() .darken(Math.abs(value) * 100) .toHexString(); } if (value > 0) { return (0, exports.black)() .lighten(value * 100) .toHexString(); } return (0, exports.white)().toHexString(); } exports.toGrayHex = toGrayHex; function alpha(color, alpha) { return create(color).setAlpha(alpha).toRgbString(); } exports.alpha = alpha; function lighten(color, amount) { return create(color).lighten(amount).toRgbString(); } exports.lighten = lighten; function darken(color, amount) { return create(color).darken(amount).toRgbString(); } exports.darken = darken;