pax_global_header00006660000000000000000000000064142305637700014521gustar00rootroot0000000000000052 comment=d9b04bb2989b2279079ed1b48ac2e9d61291ef64 color-string-1.9.1/000077500000000000000000000000001423056377000141535ustar00rootroot00000000000000color-string-1.9.1/.editorconfig000066400000000000000000000013331423056377000166300ustar00rootroot00000000000000root = true [*] indent_style = tab indent_size = 4 tab_width = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{*.json,*.json.example,*.gyp,*.yml,*.yaml}] indent_style = space indent_size = 2 [{*.py,*.asm}] indent_style = space [*.py] indent_size = 4 [{*.asm,*.s}] indent_style = space indent_size = 8 [*.md] trim_trailing_whitespace = false # Ideal settings - some plugins might support these. [*.js] quote_type = single [{*.c,*.cc,*.h,*.hh,*.cpp,*.hpp,*.m,*.mm,*.mpp,*.js,*.java,*.go,*.rs,*.php,*.ng,*.jsx,*.ts,*.d,*.cs,*.swift}] curly_bracket_next_line = false spaces_around_operators = true spaces_around_brackets = outside # close enough to 1TB indent_brace_style = K&R color-string-1.9.1/.github/000077500000000000000000000000001423056377000155135ustar00rootroot00000000000000color-string-1.9.1/.github/workflows/000077500000000000000000000000001423056377000175505ustar00rootroot00000000000000color-string-1.9.1/.github/workflows/main.yml000066400000000000000000000007031423056377000212170ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 17 - 16 - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test color-string-1.9.1/.gitignore000066400000000000000000000000161423056377000161400ustar00rootroot00000000000000node_modules/ color-string-1.9.1/.npmrc000066400000000000000000000000771423056377000152770ustar00rootroot00000000000000save-exact = true package-lock = false update-notifier = false color-string-1.9.1/CHANGELOG.md000066400000000000000000000012051423056377000157620ustar00rootroot00000000000000# 0.4.0 - Changed: Invalid conversions now return `null` instead of `undefined` - Changed: Moved to XO standard - Fixed: a few details in package.json - Fixed: readme output regarding wrapped hue values ([#21](https://github.com/MoOx/color-string/pull/21)) # 0.3.0 - Fixed: HSL alpha channel ([#16](https://github.com/harthur/color-string/pull/16)) - Fixed: ability to parse signed number ([#15](https://github.com/harthur/color-string/pull/15)) - Removed: component.json - Removed: browser build - Added: license field to package.json ([#17](https://github.com/harthur/color-string/pull/17)) --- Check out commit logs for earlier releases color-string-1.9.1/LICENSE000066400000000000000000000020721423056377000151610ustar00rootroot00000000000000Copyright (c) 2011 Heather Arthur Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. color-string-1.9.1/README.md000066400000000000000000000050311423056377000154310ustar00rootroot00000000000000# color-string > library for parsing and generating CSS color strings. ## Install With [npm](http://npmjs.org/): ```console $ npm install color-string ``` ## Usage ### Parsing ```js colorString.get('#FFF') // {model: 'rgb', value: [255, 255, 255, 1]} colorString.get('#FFFA') // {model: 'rgb', value: [255, 255, 255, 0.67]} colorString.get('#FFFFFFAA') // {model: 'rgb', value: [255, 255, 255, 0.67]} colorString.get('hsl(360, 100%, 50%)') // {model: 'hsl', value: [0, 100, 50, 1]} colorString.get('hsl(360 100% 50%)') // {model: 'hsl', value: [0, 100, 50, 1]} colorString.get('hwb(60, 3%, 60%)') // {model: 'hwb', value: [60, 3, 60, 1]} colorString.get.rgb('#FFF') // [255, 255, 255, 1] colorString.get.rgb('blue') // [0, 0, 255, 1] colorString.get.rgb('rgba(200, 60, 60, 0.3)') // [200, 60, 60, 0.3] colorString.get.rgb('rgba(200 60 60 / 0.3)') // [200, 60, 60, 0.3] colorString.get.rgb('rgba(200 60 60 / 30%)') // [200, 60, 60, 0.3] colorString.get.rgb('rgb(200, 200, 200)') // [200, 200, 200, 1] colorString.get.rgb('rgb(200 200 200)') // [200, 200, 200, 1] colorString.get.hsl('hsl(360, 100%, 50%)') // [0, 100, 50, 1] colorString.get.hsl('hsl(360 100% 50%)') // [0, 100, 50, 1] colorString.get.hsl('hsla(360, 60%, 50%, 0.4)') // [0, 60, 50, 0.4] colorString.get.hsl('hsl(360 60% 50% / 0.4)') // [0, 60, 50, 0.4] colorString.get.hwb('hwb(60, 3%, 60%)') // [60, 3, 60, 1] colorString.get.hwb('hwb(60, 3%, 60%, 0.6)') // [60, 3, 60, 0.6] colorString.get.rgb('invalid color string') // null ``` ### Generation ```js colorString.to.hex([255, 255, 255]) // "#FFFFFF" colorString.to.hex([0, 0, 255, 0.4]) // "#0000FF66" colorString.to.hex([0, 0, 255], 0.4) // "#0000FF66" colorString.to.rgb([255, 255, 255]) // "rgb(255, 255, 255)" colorString.to.rgb([0, 0, 255, 0.4]) // "rgba(0, 0, 255, 0.4)" colorString.to.rgb([0, 0, 255], 0.4) // "rgba(0, 0, 255, 0.4)" colorString.to.rgb.percent([0, 0, 255]) // "rgb(0%, 0%, 100%)" colorString.to.keyword([255, 255, 0]) // "yellow" colorString.to.hsl([360, 100, 100]) // "hsl(360, 100%, 100%)" colorString.to.hwb([50, 3, 15]) // "hwb(50, 3%, 15%)" // all functions also support swizzling colorString.to.rgb(0, [0, 255], 0.4) // "rgba(0, 0, 255, 0.4)" colorString.to.rgb([0, 0], [255], 0.4) // "rgba(0, 0, 255, 0.4)" colorString.to.rgb([0], 0, [255, 0.4]) // "rgba(0, 0, 255, 0.4)" ``` color-string-1.9.1/index.js000066400000000000000000000131241423056377000156210ustar00rootroot00000000000000/* MIT license */ var colorNames = require('color-name'); var swizzle = require('simple-swizzle'); var hasOwnProperty = Object.hasOwnProperty; var reverseNames = Object.create(null); // create a list of reverse color names for (var name in colorNames) { if (hasOwnProperty.call(colorNames, name)) { reverseNames[colorNames[name]] = name; } } var cs = module.exports = { to: {}, get: {} }; cs.get = function (string) { var prefix = string.substring(0, 3).toLowerCase(); var val; var model; switch (prefix) { case 'hsl': val = cs.get.hsl(string); model = 'hsl'; break; case 'hwb': val = cs.get.hwb(string); model = 'hwb'; break; default: val = cs.get.rgb(string); model = 'rgb'; break; } if (!val) { return null; } return {model: model, value: val}; }; cs.get.rgb = function (string) { if (!string) { return null; } var abbr = /^#([a-f0-9]{3,4})$/i; var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i; var rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; var keyword = /^(\w+)$/; var rgb = [0, 0, 0, 1]; var match; var i; var hexAlpha; if (match = string.match(hex)) { hexAlpha = match[2]; match = match[1]; for (i = 0; i < 3; i++) { // https://jsperf.com/slice-vs-substr-vs-substring-methods-long-string/19 var i2 = i * 2; rgb[i] = parseInt(match.slice(i2, i2 + 2), 16); } if (hexAlpha) { rgb[3] = parseInt(hexAlpha, 16) / 255; } } else if (match = string.match(abbr)) { match = match[1]; hexAlpha = match[3]; for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i] + match[i], 16); } if (hexAlpha) { rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255; } } else if (match = string.match(rgba)) { for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i + 1], 0); } if (match[4]) { if (match[5]) { rgb[3] = parseFloat(match[4]) * 0.01; } else { rgb[3] = parseFloat(match[4]); } } } else if (match = string.match(per)) { for (i = 0; i < 3; i++) { rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55); } if (match[4]) { if (match[5]) { rgb[3] = parseFloat(match[4]) * 0.01; } else { rgb[3] = parseFloat(match[4]); } } } else if (match = string.match(keyword)) { if (match[1] === 'transparent') { return [0, 0, 0, 0]; } if (!hasOwnProperty.call(colorNames, match[1])) { return null; } rgb = colorNames[match[1]]; rgb[3] = 1; return rgb; } else { return null; } for (i = 0; i < 3; i++) { rgb[i] = clamp(rgb[i], 0, 255); } rgb[3] = clamp(rgb[3], 0, 1); return rgb; }; cs.get.hsl = function (string) { if (!string) { return null; } var hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d\.]+)%\s*,?\s*([+-]?[\d\.]+)%\s*(?:[,|\/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; var match = string.match(hsl); if (match) { var alpha = parseFloat(match[4]); var h = ((parseFloat(match[1]) % 360) + 360) % 360; var s = clamp(parseFloat(match[2]), 0, 100); var l = clamp(parseFloat(match[3]), 0, 100); var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1); return [h, s, l, a]; } return null; }; cs.get.hwb = function (string) { if (!string) { return null; } var hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; var match = string.match(hwb); if (match) { var alpha = parseFloat(match[4]); var h = ((parseFloat(match[1]) % 360) + 360) % 360; var w = clamp(parseFloat(match[2]), 0, 100); var b = clamp(parseFloat(match[3]), 0, 100); var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1); return [h, w, b, a]; } return null; }; cs.to.hex = function () { var rgba = swizzle(arguments); return ( '#' + hexDouble(rgba[0]) + hexDouble(rgba[1]) + hexDouble(rgba[2]) + (rgba[3] < 1 ? (hexDouble(Math.round(rgba[3] * 255))) : '') ); }; cs.to.rgb = function () { var rgba = swizzle(arguments); return rgba.length < 4 || rgba[3] === 1 ? 'rgb(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ')' : 'rgba(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ', ' + rgba[3] + ')'; }; cs.to.rgb.percent = function () { var rgba = swizzle(arguments); var r = Math.round(rgba[0] / 255 * 100); var g = Math.round(rgba[1] / 255 * 100); var b = Math.round(rgba[2] / 255 * 100); return rgba.length < 4 || rgba[3] === 1 ? 'rgb(' + r + '%, ' + g + '%, ' + b + '%)' : 'rgba(' + r + '%, ' + g + '%, ' + b + '%, ' + rgba[3] + ')'; }; cs.to.hsl = function () { var hsla = swizzle(arguments); return hsla.length < 4 || hsla[3] === 1 ? 'hsl(' + hsla[0] + ', ' + hsla[1] + '%, ' + hsla[2] + '%)' : 'hsla(' + hsla[0] + ', ' + hsla[1] + '%, ' + hsla[2] + '%, ' + hsla[3] + ')'; }; // hwb is a bit different than rgb(a) & hsl(a) since there is no alpha specific syntax // (hwb have alpha optional & 1 is default value) cs.to.hwb = function () { var hwba = swizzle(arguments); var a = ''; if (hwba.length >= 4 && hwba[3] !== 1) { a = ', ' + hwba[3]; } return 'hwb(' + hwba[0] + ', ' + hwba[1] + '%, ' + hwba[2] + '%' + a + ')'; }; cs.to.keyword = function (rgb) { return reverseNames[rgb.slice(0, 3)]; }; // helpers function clamp(num, min, max) { return Math.min(Math.max(min, num), max); } function hexDouble(num) { var str = Math.round(num).toString(16).toUpperCase(); return (str.length < 2) ? '0' + str : str; } color-string-1.9.1/package.json000066400000000000000000000013341423056377000164420ustar00rootroot00000000000000{ "name": "color-string", "description": "Parser and generator for CSS color strings", "version": "1.9.1", "author": "Heather Arthur ", "contributors": [ "Maxime Thirouin", "Dyma Ywanov ", "Josh Junon" ], "repository": "Qix-/color-string", "scripts": { "pretest": "xo", "test": "node test/basic.js" }, "license": "MIT", "files": [ "index.js" ], "xo": { "rules": { "no-cond-assign": 0, "operator-linebreak": 0 } }, "dependencies": { "color-name": "^1.0.0", "simple-swizzle": "^0.2.2" }, "devDependencies": { "xo": "^0.12.1" }, "keywords": [ "color", "colour", "rgb", "css" ] } color-string-1.9.1/test/000077500000000000000000000000001423056377000151325ustar00rootroot00000000000000color-string-1.9.1/test/basic.js000066400000000000000000000340631423056377000165570ustar00rootroot00000000000000var assert = require('assert'); var string = require('../'); function normalizeAlpha(res) { if (res.model === 'rgb' && res.value.length >= 4) { res.value[3] = res.value[3].toFixed(2); } else if (res.length >= 4) { res[3] = res[3].toFixed(2); } return res; } assert.deepEqual(string.get.rgb('#fef'), [255, 238, 255, 1]); assert.deepEqual(string.get.rgb('#fffFEF'), [255, 255, 239, 1]); assert.deepEqual(string.get.rgb('rgb(244, 233, 100)'), [244, 233, 100, 1]); assert.deepEqual(string.get.rgb('rgb(244 233 100)'), [244, 233, 100, 1]); assert.deepEqual(string.get.rgb('rgb(100%, 30%, 90%)'), [255, 77, 229, 1]); assert.deepEqual(string.get.rgb('rgb(100% 30% 90%)'), [255, 77, 229, 1]); assert.deepEqual(string.get.rgb('transparent'), [0, 0, 0, 0]); assert.deepEqual(string.get.hsl('hsl(240, 100%, 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hsl('hsl(240 100% 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hsl('hsl(240deg, 100%, 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hsl('hsl(240deg 100% 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(240, 100%, 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(240deg, 100%, 50.5%)'), [240, 100, 50.5, 1]); // generic .get() assert.deepEqual(string.get('#fef'), {model: 'rgb', value: [255, 238, 255, 1]}); assert.deepEqual(string.get('#fffFEF'), {model: 'rgb', value: [255, 255, 239, 1]}); assert.deepEqual(string.get('#fffFEFff'), {model: 'rgb', value: [255, 255, 239, 1]}); assert.deepEqual(string.get('#fffFEF00'), {model: 'rgb', value: [255, 255, 239, 0]}); assert.deepEqual(normalizeAlpha(string.get('#fffFEFa9')), {model: 'rgb', value: [255, 255, 239, '0.66']}); assert.deepEqual(string.get('rgb(244, 233, 100)'), {model: 'rgb', value: [244, 233, 100, 1]}); assert.deepEqual(string.get('rgb(244 233 100)'), {model: 'rgb', value: [244, 233, 100, 1]}); assert.deepEqual(string.get('rgb(100%, 30%, 90%)'), {model: 'rgb', value: [255, 77, 229, 1]}); assert.deepEqual(string.get('rgb(100% 30% 90%)'), {model: 'rgb', value: [255, 77, 229, 1]}); assert.deepEqual(string.get('transparent'), {model: 'rgb', value: [0, 0, 0, 0]}); assert.deepEqual(string.get('hsl(240, 100%, 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]}); assert.deepEqual(string.get('hsl(-480, 100%, 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]}); assert.deepEqual(string.get('hsl(240 100% 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]}); assert.deepEqual(string.get('hsl(240deg, 100%, 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]}); assert.deepEqual(string.get('hsl(240deg 100% 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]}); assert.deepEqual(string.get('hwb(240, 100%, 50.5%)'), {model: 'hwb', value: [240, 100, 50.5, 1]}); assert.deepEqual(string.get('hwb(240deg, 100%, 50.5%)'), {model: 'hwb', value: [240, 100, 50.5, 1]}); // invalid generic .get() calls assert.deepEqual(string.get('hsla(250, 100%, 50%, 50%)'), null); assert.deepEqual(string.get('hsl(250 100% 50% / 50%)'), null); assert.deepEqual(string.get('rgba(250, 100%, 50%, 50%)'), null); assert.deepEqual(string.get('333333'), null); assert.strictEqual(string.get('#1'), null); assert.strictEqual(string.get('#f'), null); assert.strictEqual(string.get('#4f'), null); assert.strictEqual(string.get('#45ab4'), null); assert.strictEqual(string.get('#45ab45e'), null); assert.strictEqual(string.get('rgb()'), null); assert.strictEqual(string.get('rgb(10)'), null); assert.strictEqual(string.get('rgb(10, 2)'), null); assert.strictEqual(string.get('rgb(10, 2, 2348723dskjfs)'), null); assert.strictEqual(string.get('rgb(10%)'), null); assert.strictEqual(string.get('rgb(10%, 2%)'), null); assert.strictEqual(string.get('rgb(10%, 2%, 2348723%dskjfs)'), null); assert.strictEqual(string.get('rgb(10%, 2%, 2348723dskjfs%)'), null); assert.strictEqual(string.get('rgb(10$,3)'), null); assert.strictEqual(string.get('rgba(10, 3)'), null); // with sign assert.deepEqual(string.get.rgb('rgb(-244, +233, -100)'), [0, 233, 0, 1]); assert.deepEqual(string.get.rgb('rgb(-244 +233 -100)'), [0, 233, 0, 1]); assert.deepEqual(string.get.hsl('hsl(+240, 100%, 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hsl('hsl(+240 100% 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.rgb('rgba(200, +20, -233, -0.0)'), [200, 20, 0, 0]); assert.deepEqual(string.get.rgb('rgba(200 +20 -233 / -0.0)'), [200, 20, 0, 0]); assert.deepEqual(string.get.rgb('rgba(200, +20, -233, -0.0)'), [200, 20, 0, 0]); assert.deepEqual(string.get.rgb('rgba(200 +20 -233 / -0.0)'), [200, 20, 0, 0]); assert.deepEqual(string.get.hsl('hsla(+200, 100%, 50%, -0.2)'), [200, 100, 50, 0]); assert.deepEqual(string.get.hsl('hsla(+200, 100%, 50%, -1e-7)'), [200, 100, 50, 0]); assert.deepEqual(string.get.hsl('hsl(+200 100% 50% / -0.2)'), [200, 100, 50, 0]); assert.deepEqual(string.get.hsl('hsl(+200 100% 50% / -1e-7)'), [200, 100, 50, 0]); assert.deepEqual(string.get.hsl('hsl(+200 100% 50% / -2.e7)'), [200, 100, 50, 0]); assert.deepEqual(string.get.hsl('hsl(+200 100% 50% / +1e7)'), [200, 100, 50, 1]); assert.deepEqual(string.get.hsl('hsl(+200 100% 50% / 127.88e4)'), [200, 100, 50, 1]); assert.deepEqual(string.get.hsl('hsl(+200 100% 50% / 0.2e3)'), [200, 100, 50, 1]); assert.deepEqual(string.get.hsl('hsl(+200 100% 50% / .1e-4)'), [200, 100, 50, 1e-5]); assert.deepEqual(string.get.hsl('hsla(-10.0, 100%, 50%, -0.2)'), [350, 100, 50, 0]); assert.deepEqual(string.get.hsl('hsl(-10.0 100% 50% / -0.2)'), [350, 100, 50, 0]); assert.deepEqual(string.get.hsl('hsla(.5, 100%, 50%, -0.2)'), [0.5, 100, 50, 0]); assert.deepEqual(string.get.hsl('hsl(.5 100% 50% / -0.2)'), [0.5, 100, 50, 0]); assert.deepEqual(string.get.hwb('hwb(+240, 100%, 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%)'), [120, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%, +0.6)'), [120, 100, 50.5, 0.6]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%, +1e-7)'), [120, 100, 50.5, 1e-7]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%, -2.e7)'), [120, 100, 50.5, 0]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%, +1e7)'), [120, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%, +1e7)'), [120, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%, 127.88e4)'), [120, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%, 0.2e3)'), [120, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(-240deg, 100%, 50.5%, .1e-4)'), [120, 100, 50.5, 1e-5]); assert.deepEqual(string.get.hwb('hwb(10.0deg, 100%, 50.5%)'), [10, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(-.5, 100%, 50.5%)'), [359.5, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(-10.0deg, 100%, 50.5%, +0.6)'), [350, 100, 50.5, 0.6]); // subsequent return values should not change array assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]); assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]); // alpha assert.deepEqual(normalizeAlpha(string.get.rgb('#fffa')), [255, 255, 255, '0.67']); assert.deepEqual(string.get.rgb('#c814e933'), [200, 20, 233, 0.2]); assert.deepEqual(string.get.rgb('#c814e900'), [200, 20, 233, 0]); assert.deepEqual(string.get.rgb('#c814e9ff'), [200, 20, 233, 1]); assert.deepEqual(string.get.rgb('rgba(200, 20, 233, 0.2)'), [200, 20, 233, 0.2]); assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0.2)'), [200, 20, 233, 0.2]); assert.deepEqual(string.get.rgb('rgba(200 20 233 / 20%)'), [200, 20, 233, 0.2]); assert.deepEqual(string.get.rgb('rgba(200, 20, 233, 0)'), [200, 20, 233, 0]); assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0)'), [200, 20, 233, 0]); assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0%)'), [200, 20, 233, 0]); assert.deepEqual(string.get.rgb('rgba(100%, 30%, 90%, 0.2)'), [255, 77, 229, 0.2]); assert.deepEqual(string.get.rgb('rgba(100% 30% 90% / 0.2)'), [255, 77, 229, 0.2]); assert.deepEqual(string.get.rgb('rgba(100% 30% 90% / 20%)'), [255, 77, 229, 0.2]); assert.deepEqual(string.get.hsl('hsla(200, 20%, 33%, 0.2)'), [200, 20, 33, 0.2]); assert.deepEqual(string.get.hsl('hsla(200, 20%, 33%, 1e-7)'), [200, 20, 33, 1e-7]); assert.deepEqual(string.get.hsl('hsl(200 20% 33% / 0.2)'), [200, 20, 33, 0.2]); assert.deepEqual(string.get.hsl('hsl(200 20% 33% / 1e-7)'), [200, 20, 33, 1e-7]); assert.deepEqual(string.get.hwb('hwb(200, 20%, 33%, 0.2)'), [200, 20, 33, 0.2]); assert.deepEqual(string.get.hwb('hwb(200, 20%, 33%, 1e-7)'), [200, 20, 33, 1e-7]); // no alpha assert.deepEqual(string.get.rgb('#fef'), [255, 238, 255, 1]); assert.deepEqual(string.get.rgb('rgba(200, 20, 233)'), [200, 20, 233, 1]); assert.deepEqual(string.get.rgb('rgba(200 20 233)'), [200, 20, 233, 1]); assert.deepEqual(string.get.hsl('hsl(240, 100%, 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hsl('hsl(240 100% 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.rgb('rgba(0, 0, 0, 0)'), [0, 0, 0, 0]); assert.deepEqual(string.get.rgb('rgba(0 0 0 / 0)'), [0, 0, 0, 0]); assert.deepEqual(string.get.hsl('hsla(0, 0%, 0%, 0)'), [0, 0, 0, 0]); assert.deepEqual(string.get.hsl('hsl(0 0% 0% / 0)'), [0, 0, 0, 0]); assert.deepEqual(string.get.hsl('hsl(0deg 0% 0% / 0)'), [0, 0, 0, 0]); assert.deepEqual(string.get.hwb('hwb(400, 10%, 200%, 0)'), [40, 10, 100, 0]); // range assert.deepEqual(string.get.rgb('rgba(300, 600, 100, 3)'), [255, 255, 100, 1]); assert.deepEqual(string.get.rgb('rgba(300 600 100 / 3)'), [255, 255, 100, 1]); assert.deepEqual(string.get.rgb('rgba(8000%, 100%, 333%, 88)'), [255, 255, 255, 1]); assert.deepEqual(string.get.rgb('rgba(8000% 100% 333% / 88)'), [255, 255, 255, 1]); assert.deepEqual(string.get.hsl('hsla(400, 10%, 200%, 10)'), [40, 10, 100, 1]); assert.deepEqual(string.get.hsl('hsl(400 10% 200% / 10)'), [40, 10, 100, 1]); assert.deepEqual(string.get.hwb('hwb(400, 10%, 200%, 10)'), [40, 10, 100, 1]); // invalid assert.strictEqual(string.get.rgb('yellowblue'), null); assert.strictEqual(string.get.rgb('hsl(100, 10%, 10%)'), null); assert.strictEqual(string.get.rgb('hsl(100 10% 10%)'), null); assert.strictEqual(string.get.rgb('hwb(100, 10%, 10%)'), null); assert.strictEqual(string.get.rgb('rgb(123, 255, 9)1234'), null); assert.strictEqual(string.get.rgb('rgb(123 255 9)1234'), null); assert.strictEqual(string.get.rgb('333333'), null); assert.strictEqual(string.get.rgb('1'), null); assert.strictEqual(string.get.rgb('1892371923879'), null); assert.strictEqual(string.get.rgb('444'), null); assert.strictEqual(string.get.rgb('#1'), null); assert.strictEqual(string.get.rgb('#f'), null); assert.strictEqual(string.get.rgb('#4f'), null); assert.strictEqual(string.get.rgb('#45ab4'), null); assert.strictEqual(string.get.rgb('#45ab45e'), null); assert.strictEqual(string.get.hsl('hsl(41, 50%, 45%)1234'), null); assert.strictEqual(string.get.hsl('hsl(41 50% 45%)1234'), null); assert.strictEqual(string.get.hsl('hsl(41 50% 45% / 3)1234'), null); assert.strictEqual(string.get.hsl('hsl(41 50% 45% / 1e)'), null); assert.strictEqual(string.get.hsl('hsl(41 50% 45% / e)'), null); assert.strictEqual(string.get.hsl('hsl(41 50% 45% / 0e-)'), null); assert.strictEqual(string.get.hsl('hsl(41 50% 45% / 0e+)'), null); assert.strictEqual(string.get.hsl('hsl(41 50% 45% / +000e33)'), null); assert.strictEqual(string.get.hwb('hwb(240, 100%, 1e'), null); assert.strictEqual(string.get.hwb('hwb(240, 100%, e'), null); assert.strictEqual(string.get.hwb('hwb(240, 100%, 0e-'), null); assert.strictEqual(string.get.hwb('hwb(240, 100%, 0e+'), null); assert.strictEqual(string.get.hwb('hwb(240, 100%, +000e33'), null); // generators assert.equal(string.to.hex([255, 10, 35]), '#FF0A23'); assert.equal(string.to.hex([255, 10, 35, 1]), '#FF0A23'); assert.equal(string.to.hex([255, 10, 35], 1), '#FF0A23'); assert.equal(string.to.hex([255, 10, 35, 0.3]), '#FF0A234D'); assert.equal(string.to.hex([255, 10, 35], 0.3), '#FF0A234D'); assert.equal(string.to.hex([255, 10, 35, 0]), '#FF0A2300'); assert.equal(string.to.hex([255, 10, 35], 0), '#FF0A2300'); assert.equal(string.to.rgb([255, 10, 35]), 'rgb(255, 10, 35)'); assert.equal(string.to.rgb([255, 10, 35, 0.3]), 'rgba(255, 10, 35, 0.3)'); assert.equal(string.to.rgb([255, 10, 35], 0.3), 'rgba(255, 10, 35, 0.3)'); assert.equal(string.to.rgb([255, 10, 35, 0.3]), 'rgba(255, 10, 35, 0.3)'); assert.equal(string.to.rgb([255, 10, 35], 0.3), 'rgba(255, 10, 35, 0.3)'); assert.equal(string.to.rgb([255, 10, 35]), 'rgb(255, 10, 35)'); assert.equal(string.to.rgb([255, 10, 35, 0]), 'rgba(255, 10, 35, 0)'); assert.equal(string.to.rgb.percent([255, 10, 35]), 'rgb(100%, 4%, 14%)'); assert.equal(string.to.rgb.percent([255, 10, 35, 0.3]), 'rgba(100%, 4%, 14%, 0.3)'); assert.equal(string.to.rgb.percent([255, 10, 35], 0.3), 'rgba(100%, 4%, 14%, 0.3)'); assert.equal(string.to.rgb.percent([255, 10, 35, 0.3]), 'rgba(100%, 4%, 14%, 0.3)'); assert.equal(string.to.rgb.percent([255, 10, 35], 0.3), 'rgba(100%, 4%, 14%, 0.3)'); assert.equal(string.to.rgb.percent([255, 10, 35]), 'rgb(100%, 4%, 14%)'); assert.equal(string.to.hsl([280, 40, 60]), 'hsl(280, 40%, 60%)'); assert.equal(string.to.hsl([280, 40, 60, 0.3]), 'hsla(280, 40%, 60%, 0.3)'); assert.equal(string.to.hsl([280, 40, 60], 0.3), 'hsla(280, 40%, 60%, 0.3)'); assert.equal(string.to.hsl([280, 40, 60, 0.3]), 'hsla(280, 40%, 60%, 0.3)'); assert.equal(string.to.hsl([280, 40, 60], 0.3), 'hsla(280, 40%, 60%, 0.3)'); assert.equal(string.to.hsl([280, 40, 60], 0), 'hsla(280, 40%, 60%, 0)'); assert.equal(string.to.hsl([280, 40, 60]), 'hsl(280, 40%, 60%)'); assert.equal(string.to.hwb([280, 40, 60]), 'hwb(280, 40%, 60%)'); assert.equal(string.to.hwb([280, 40, 60, 0.3]), 'hwb(280, 40%, 60%, 0.3)'); assert.equal(string.to.hwb([280, 40, 60], 0.3), 'hwb(280, 40%, 60%, 0.3)'); assert.equal(string.to.hwb([280, 40, 60], 0), 'hwb(280, 40%, 60%, 0)'); assert.equal(string.to.keyword([255, 255, 0]), 'yellow'); assert.equal(string.to.keyword(['constructor']), undefined); assert.equal(string.to.keyword([100, 255, 0]), undefined); // Make sure .get() doesn't return object prototype values (regression test, #44) Object.keys(Object.getOwnPropertyDescriptors(Object.prototype)) .map(property => assert.deepStrictEqual([property, string.get(property)], [property, null])); // Make sure writing decimal values as hex doesn't cause bizarre output (regression test, #25) assert.equal(string.to.hex([44.2, 83.8, 44]), '#2C542C');