UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

48 lines (47 loc) 1.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _tinycolor = require("@ctrl/tinycolor"); function isStableColor(color) { return color >= 0 && color <= 255; } function getAlphaColor(frontColor, backgroundColor) { const { r: fR, g: fG, b: fB, a: originAlpha } = new _tinycolor.TinyColor(frontColor).toRgb(); if (originAlpha < 1) { return frontColor; } const { r: bR, g: bG, b: bB } = new _tinycolor.TinyColor(backgroundColor).toRgb(); for (let fA = 0.01; fA <= 1; fA += 0.01) { const r = Math.round((fR - bR * (1 - fA)) / fA); const g = Math.round((fG - bG * (1 - fA)) / fA); const b = Math.round((fB - bB * (1 - fA)) / fA); if (isStableColor(r) && isStableColor(g) && isStableColor(b)) { return new _tinycolor.TinyColor({ r, g, b, a: Math.round(fA * 100) / 100 }).toRgbString(); } } // fallback /* istanbul ignore next */ return new _tinycolor.TinyColor({ r: fR, g: fG, b: fB, a: 1 }).toRgbString(); } var _default = exports.default = getAlphaColor;