diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-10-14 15:20:49 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-10-14 15:20:49 +0800 |
| commit | c5a5384e846f242e901e859fe600d695d8990a9a (patch) | |
| tree | 079d4ccf2ed93dc1f0cae74a6d3b4a42ac387ece /node_modules/rgb-hex/index.js | |
| parent | d261b7aafee0e6e5c7b853cb8dfe609cc0a0ac9e (diff) | |
legacy browser support via plugins from postcss
Diffstat (limited to 'node_modules/rgb-hex/index.js')
| -rw-r--r-- | node_modules/rgb-hex/index.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/node_modules/rgb-hex/index.js b/node_modules/rgb-hex/index.js new file mode 100644 index 00000000..f7b8f4e7 --- /dev/null +++ b/node_modules/rgb-hex/index.js @@ -0,0 +1,38 @@ +'use strict'; +/* eslint-disable no-mixed-operators */ +module.exports = (red, green, blue, alpha) => { + const isPercent = (red + (alpha || '')).toString().includes('%'); + + if (typeof red === 'string') { + [red, green, blue, alpha] = red.match(/(0?\.?\d{1,3})%?\b/g).map(Number); + } else if (alpha !== undefined) { + alpha = parseFloat(alpha); + } + + if (typeof red !== 'number' || + typeof green !== 'number' || + typeof blue !== 'number' || + red > 255 || + green > 255 || + blue > 255 + ) { + throw new TypeError('Expected three numbers below 256'); + } + + if (typeof alpha === 'number') { + if (!isPercent && alpha >= 0 && alpha <= 1) { + alpha = Math.round(255 * alpha); + } else if (isPercent && alpha >= 0 && alpha <= 100) { + alpha = Math.round(255 * alpha / 100); + } else { + throw new TypeError(`Expected alpha value (${alpha}) as a fraction or percentage`); + } + + alpha = (alpha | 1 << 8).toString(16).slice(1); + } else { + alpha = ''; + } + + return ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1) + alpha; +}; +/* eslint-enable no-mixed-operators */ |
