UNPKG

react-spring

Version:
203 lines (173 loc) 5.51 kB
/* https://github.com/react-community/normalize-css-color BSD 3-Clause License Copyright (c) 2016, React Community All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import colorNames from './colors' export default function normalizeColor(color) { let match if (typeof color === 'number') { return color >>> 0 === color && color >= 0 && color <= 0xffffffff ? color : null } // Ordered based on occurrences on Facebook codebase if ((match = matchers.hex6.exec(color))) return parseInt(match[1] + 'ff', 16) >>> 0 if (colorNames.hasOwnProperty(color)) return colorNames[color] if ((match = matchers.rgb.exec(color))) { return ( ((parse255(match[1]) << 24) | // r (parse255(match[2]) << 16) | // g (parse255(match[3]) << 8) | // b 0x000000ff) >>> // a 0 ) } if ((match = matchers.rgba.exec(color))) { return ( ((parse255(match[1]) << 24) | // r (parse255(match[2]) << 16) | // g (parse255(match[3]) << 8) | // b parse1(match[4])) >>> // a 0 ) } if ((match = matchers.hex3.exec(color))) { return ( parseInt( match[1] + match[1] + // r match[2] + match[2] + // g match[3] + match[3] + // b 'ff', // a 16 ) >>> 0 ) } // https://drafts.csswg.org/css-color-4/#hex-notation if ((match = matchers.hex8.exec(color))) return parseInt(match[1], 16) >>> 0 if ((match = matchers.hex4.exec(color))) { return ( parseInt( match[1] + match[1] + // r match[2] + match[2] + // g match[3] + match[3] + // b match[4] + match[4], // a 16 ) >>> 0 ) } if ((match = matchers.hsl.exec(color))) { return ( (hslToRgb( parse360(match[1]), // h parsePercentage(match[2]), // s parsePercentage(match[3]) // l ) | 0x000000ff) >>> // a 0 ) } if ((match = matchers.hsla.exec(color))) { return ( (hslToRgb( parse360(match[1]), // h parsePercentage(match[2]), // s parsePercentage(match[3]) // l ) | parse1(match[4])) >>> // a 0 ) } return null } function hue2rgb(p, q, t) { if (t < 0) t += 1 if (t > 1) t -= 1 if (t < 1 / 6) return p + (q - p) * 6 * t if (t < 1 / 2) return q if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6 return p } function hslToRgb(h, s, l) { const q = l < 0.5 ? l * (1 + s) : l + s - l * s const p = 2 * l - q const r = hue2rgb(p, q, h + 1 / 3) const g = hue2rgb(p, q, h) const b = hue2rgb(p, q, h - 1 / 3) return ( (Math.round(r * 255) << 24) | (Math.round(g * 255) << 16) | (Math.round(b * 255) << 8) ) } // var INTEGER = '[-+]?\\d+'; const NUMBER = '[-+]?\\d*\\.?\\d+' const PERCENTAGE = NUMBER + '%' function toArray(arrayLike) { return Array.prototype.slice.call(arrayLike, 0) } function call() { return '\\(\\s*(' + toArray(arguments).join(')\\s*,\\s*(') + ')\\s*\\)' } var matchers = { rgb: new RegExp('rgb' + call(NUMBER, NUMBER, NUMBER)), rgba: new RegExp('rgba' + call(NUMBER, NUMBER, NUMBER, NUMBER)), hsl: new RegExp('hsl' + call(NUMBER, PERCENTAGE, PERCENTAGE)), hsla: new RegExp('hsla' + call(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER)), hex3: /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/, hex4: /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/, hex6: /^#([0-9a-fA-F]{6})$/, hex8: /^#([0-9a-fA-F]{8})$/, } function parse255(str) { const int = parseInt(str, 10) if (int < 0) return 0 if (int > 255) return 255 return int } function parse360(str) { const int = parseFloat(str) return ((int % 360 + 360) % 360) / 360 } function parse1(str) { const num = parseFloat(str) if (num < 0) return 0 if (num > 1) return 255 return Math.round(num * 255) } function parsePercentage(str) { // parseFloat conveniently ignores the final % const int = parseFloat(str, 10) if (int < 0) return 0 if (int > 100) return 1 return int / 100 }