From 8551976e7269e21f4f1f7c09a45b27b499198d57 Mon Sep 17 00:00:00 2001 From: wessberg Date: Tue, 19 Jun 2018 23:38:18 +0200 Subject: [PATCH] Refactored and moved to Typescript --- .gitignore | 45 +- .npmignore | 49 +- .flowconfig => CHANGELOG.md | 0 README.md | 96 +- mochahook.js | 28 - package.json | 92 +- rollup.dist.config.js | 44 - rollup.native.config.js | 41 - src/{colorutilities.js => colorutilities.ts} | 891 +++--- src/test/colorutilities.test.js | 2737 ------------------ src/test/colorutilities.test.ts | 1817 ++++++++++++ tsconfig.dist.json | 9 + tsconfig.json | 3 + tslint.json | 3 + 14 files changed, 2462 insertions(+), 3393 deletions(-) rename .flowconfig => CHANGELOG.md (100%) delete mode 100644 mochahook.js delete mode 100644 rollup.dist.config.js delete mode 100644 rollup.native.config.js rename src/{colorutilities.js => colorutilities.ts} (54%) delete mode 100644 src/test/colorutilities.test.js create mode 100644 src/test/colorutilities.test.ts create mode 100644 tsconfig.dist.json create mode 100644 tsconfig.json create mode 100644 tslint.json diff --git a/.gitignore b/.gitignore index 9c7497b..d027f31 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,36 @@ -node_modules -npm-debug.log -flow-typed +/node_modules/ +/compiled/ +/dist/ +/typings/ +package-lock.json +/.idea/ +/.cache/ +/.vscode/ +*.log +/logs/ +npm-debug.log* +/lib-cov/ +/coverage/ +/.nyc_output/ +/.grunt/ +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip +.tgz +.env .DS_Store -.babelrc -.eslintrc.js -jsconfig.json -.idea -/typed.js -/native.js -/colorutilities.js +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db +*.pem +*.p12 +*.crt +*.csr \ No newline at end of file diff --git a/.npmignore b/.npmignore index 93d844e..d2b1538 100644 --- a/.npmignore +++ b/.npmignore @@ -1,10 +1,41 @@ -.babelrc -.eslintrc.js -rollup.dist.config.js -rollup.native.config.js +/src/ +tsconfig.* +.gitignore +/test/ +/compiled/ +/typings/ +.cache +tslint.json +/.idea/ +/.cache/ +/.vscode/ +*.log +/logs/ +npm-debug.log* +/lib-cov/ +/coverage/ +/.nyc_output/ +/.grunt/ +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip +.tgz +.env .DS_Store -.eslintrc.js -.idea -src -mochahook.js -flow-typed \ No newline at end of file +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db +*.pem +*.p12 +*.crt +*.csr +/tsconfig.* +/tslint.json \ No newline at end of file diff --git a/.flowconfig b/CHANGELOG.md similarity index 100% rename from .flowconfig rename to CHANGELOG.md diff --git a/README.md b/README.md index 38e5369..66dddd1 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,20 @@ -# colorutilities [![NPM version][npm-image]][npm-url] -> A library of well-tested helper methods for working with colors. Comes with flow typings and tree-shakeable modules. +# @wessberg/color +[![NPM version][npm-version-image]][npm-version-url] +[![License-mit][license-mit-image]][license-mit-url] -## Installation -Simply do: `npm install colorutilities`. + + +[license-mit-url]: https://opensource.org/licenses/MIT +[license-mit-image]: https://img.shields.io/badge/License-MIT-yellow.svg +[npm-version-url]: https://www.npmjs.com/package/@wessberg/color +[npm-version-image]: https://badge.fury.io/js/%40wessberg%2Fcolor.svg -## What is it? +## Installation +Simply do: `npm install @wessberg/color`. -This is a collection of helper methods for working with colors and converting between color systems. Simple methods are provided such as `toHex()`, `toHsl()`, `toHsv()`, `toRgb()`, `saturate()`, `lighten()` and much more. +## Description +This is a collection of helper functions for working with colors and converting between color systems. Simple functions are provided such as `toHex()`, `toHsl()`, `toHsv()`, `toRgb()`, `saturate()`, `lighten()` and much more. The library takes care of figuring out which color system your input string comes from, so you can just use the library and not care about the rest. Supported color systems: @@ -17,16 +24,14 @@ Supported color systems: - HSL/HSLa - HTML color names. -Beyond that, everything is typed with [Flow](https://flowtype.org/) and every method is exported as separate modules. This makes this library very easy to tree-shake and incorporate in your rollup or webpack build. - -At the time of writing, 388 tests has been written for the library methods. I'd say you can use it in production today. +At the time of writing, 389 tests has been written for the library methods. I'd say you can use it in production today. Have fun! ## Examples -```javascript -import {toHsl, lighten, saturate, isLight, randomHexColor} from "colorutilities"; +```typescript +import {toHsl, lighten, saturate, isLight, randomHexColor} from "@wessberg/color"; toHsl("#DC143C") // returns 'hsl(348, 83%, 47%)' toHsl("DC143C") // returns 'hsl(348, 83%, 47%)' @@ -40,28 +45,16 @@ isLight("rgb(234, 205, 244)") // returns true, that's a pretty light color. randomHexColor(); // returns a random hex color. ``` -## Changelog: - -**v0.02**: - -- Fixed a bug where RGB colors could be returned non-rounded even though 'rounding' was true. - -**v0.01**: - -- First release! 388 tests has been written and bidirectional conversions between HEX, RGB/RGBa, HSL/HSLa, HSV/HSB and HTML color names are supported in this initial release. - ## Usage Import it in your project like this: -```javascript -import {saturate, toRgb, ...} from "colorutilities"; // (standard) Transpiled to ES5. -// or +```typescript +import {saturate, toRgb, ...} from "@wessberg/color"; +``` -import {saturate, toRgb, ...} from "colorutilities/native"; // Transpiled to ES5 except for native ES-modules. -// or +## Backers -import {saturate, toRgb, ...} from "colorutilities/typed"; // Flow typings, ES-modules, pretranspile. -``` +[Become a backer](https://www.patreon.com/bePatron?c=1770586) and get your name, logo, and link to your site listed here. Your help is greatly appreciated! ## Documentation @@ -71,7 +64,7 @@ import {saturate, toRgb, ...} from "colorutilities/typed"; // Flow typings, ES-m **Signature:** -```javascript +```typescript isLight (color: string): boolean ``` @@ -84,7 +77,7 @@ isLight (color: string): boolean `boolean` - Returns true if the color is light, false otherwise. #### Example -```javascript +```typescript isLight("rgb(234, 205, 244)") // returns true, that's a pretty light color. ``` @@ -94,7 +87,7 @@ isLight("rgb(234, 205, 244)") // returns true, that's a pretty light color. **Signature:** -```javascript +```typescript lighten (color: string, percentage: number = 10): string ``` @@ -109,7 +102,7 @@ lighten (color: string, percentage: number = 10): string `string` - The lightened/darkened color. #### Example -```javascript +```typescript lighten("#DDACED", 10) // lightens the color 10% and returns the new value: #EACDF4. lighten("#DDACED", -90) // darkens the color 90% and returns the new value: #1B0721. ``` @@ -120,7 +113,7 @@ lighten("#DDACED", -90) // darkens the color 90% and returns the new value: #1B0 **Signature:** -```javascript +```typescript saturate (color: string, percentage: number = 10): string ``` @@ -135,7 +128,7 @@ saturate (color: string, percentage: number = 10): string `string` - The saturated/desaturated color. #### Example -```javascript +```typescript saturate("brown", 100) // saturates the HTML color 'brown' an additional 100%. saturate("brown", -100) // desaturates the HTML color 'brown' 100%. ``` @@ -146,7 +139,7 @@ saturate("brown", -100) // desaturates the HTML color 'brown' 100%. **Signature:** -```javascript +```typescript toHex (color: string): string ``` @@ -165,7 +158,7 @@ toHex (color: string): string - `TypeError` - If the method didn't succeed in normalizing the given argument into a hex color. #### Example -```javascript +```typescript toHex("#DC143C") // returns '#DC143C' toHex("DC143C") // returns '#DC143C' toHex("rgb(220, 20, 60)") // returns '#DC143C' @@ -179,7 +172,7 @@ toHex("hsb(348, 91, 86)") // returns '#DC143C' **Signature:** -```javascript +```typescript toRgb (color: string): string ``` @@ -198,7 +191,7 @@ toRgb (color: string): string - `TypeError` - If the method didn't succeed in normalizing the given argument into an RGB color. #### Example -```javascript +```typescript toRgb("#DC143C") // returns 'rgb(220, 20, 60)' toRgb("DC143C") // returns 'rgb(220, 20, 60)' toRgb("rgb(220, 20, 60)") // returns 'rgb(220, 20, 60)' @@ -212,7 +205,7 @@ toRgb("hsb(348, 91, 86)") // returns 'rgb(220, 20, 60)' **Signature:** -```javascript +```typescript toRgb (color: string): string ``` @@ -231,7 +224,7 @@ toRgb (color: string): string - `TypeError` - If the method didn't succeed in normalizing the given argument into an HSL color. #### Example -```javascript +```typescript toHsl("hsl(348, 83%, 47%)") // returns 'hsl(348, 83%, 47%)' toHsl("#DC143C") // returns 'hsl(348, 83%, 47%)' toHsl("DC143C") // returns 'hsl(348, 83%, 47%)' @@ -246,7 +239,7 @@ toHsl("hsb(348, 91, 86)") // returns 'hsl(348, 83%, 47%)' **Signature:** -```javascript +```typescript toRgb (color: string): string ``` @@ -265,7 +258,7 @@ toRgb (color: string): string - `TypeError` - If the method didn't succeed in normalizing the given argument into an HSVB/HSB color. #### Example -```javascript +```typescript toHsv("hsb(348, 91, 86)") // returns 'hsb(348, 91, 86)' toHsv("hsv(348, 91, 86)") // returns 'hsv(348, 91, 86)' toHsv("hsl(348, 83%, 47%)") // returns 'hsb(348, 91, 86)' @@ -281,7 +274,7 @@ toHsv("crimson") // returns 'hsb(348, 91, 86)' **Signature:** -```javascript +```typescript randomHexColor (): string ``` @@ -290,7 +283,7 @@ randomHexColor (): string `string` - A random hex color. Always starts with '#'. #### Example -```javascript +```typescript setInterval(() => document.body.style.backgroundColor = randomHexColor(), 100); // Makes the background of the body element change to a new random color every 100ms. Welcome back to web 1.0! @@ -302,7 +295,7 @@ setInterval(() => document.body.style.backgroundColor = randomHexColor(), 100); **Signature:** -```javascript +```typescript randomRgbColor (): string ``` @@ -311,7 +304,7 @@ randomRgbColor (): string `string` - A random RGB color. #### Example -```javascript +```typescript setInterval(() => document.body.style.backgroundColor = randomRgbColor(), 100); // Makes the background of the body element change to a new random color every 100ms. Welcome back to web 1.0! @@ -323,7 +316,7 @@ setInterval(() => document.body.style.backgroundColor = randomRgbColor(), 100); **Signature:** -```javascript +```typescript randomHslColor (): string ``` @@ -332,7 +325,7 @@ randomHslColor (): string `string` - A random HSL color. #### Example -```javascript +```typescript setInterval(() => document.body.style.backgroundColor = randomHslColor(), 100); // Makes the background of the body element change to a new random color every 100ms. Welcome back to web 1.0! @@ -344,7 +337,7 @@ setInterval(() => document.body.style.backgroundColor = randomHslColor(), 100); **Signature:** -```javascript +```typescript randomHsvColor (): string ``` @@ -353,7 +346,7 @@ randomHsvColor (): string `string` - A random HSV color. #### Example -```javascript +```typescript setInterval(() => document.body.style.backgroundColor = randomHsvColor(), 100); // Makes the background of the body element change to a new random color every 100ms. Welcome back to web 1.0! @@ -446,6 +439,3 @@ There's a lot of them. The above was the highlights. Everything is well-document - **`randomHsvColor`:** - Generates a random HSV/HSB color and returns it. - -[npm-url]: https://npmjs.org/package/colorutilities -[npm-image]: https://badge.fury.io/js/colorutilities.svg diff --git a/mochahook.js b/mochahook.js deleted file mode 100644 index 5627005..0000000 --- a/mochahook.js +++ /dev/null @@ -1,28 +0,0 @@ -"use strict"; - -// Mocha will by default use the .babelrc config. However, we also need to transform modules with mocha. This hooks is therefore injected. -require("babel-register")({ - presets: ["es2015", "stage-1"] -}); - -require("babel-polyfill"); - -// Prepare the global objects to mimic a browser environment. - -// Add a 'window' object. -global.window = {}; -// Add a 'navigator' object. -global.navigator = {userAgent: ""}; - -// Add a 'document' object. -global.document = { - body: { - style: "" - }, - createElement () {return {};}, - getElementsByTagName() {return [{style: {}}];} -}; - - -// Make 'assert' a global object. -global.assert = require("chai").assert; diff --git a/package.json b/package.json index 9ee7ea4..4a208e7 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,40 @@ { - "name": "colorutilities", - "version": "0.0.2", - "description": "A library of helper methods for working with colors. Comes with flow typings and tree-shakeable modules.", - "main": "colorutilities.js", - "jsnext:main": "native.js", - "author": "Frederik Wessberg", - "license": "ISC", + "name": "@wessberg/color", + "version": "0.0.0", + "description": "A library of helper methods for working with colors.", + "repository": { + "type": "git", + "url": "https://github.com/wessberg/color.git" + }, + "bugs": { + "url": "https://github.com/wessberg/color/issues" + }, "scripts": { - "test": "mocha --require mochahook.js --compilers js:babel-register src/test/**/*.*", - "build:es5": "NODE_ENV=production rollup -c rollup.dist.config.js -o colorutilities.js", - "build:native": "NODE_ENV=production rollup -c rollup.native.config.js -o native.js", - "build:typed": "cp src/colorutilities.js typed.js && cp src/colorutilities.js colorutilities.js.flow && cp src/colorutilities.js native.js.flow", - "build:all": "npm run build:es5 & npm run build:native & npm run build:typed & wait" + "test": "NODE_ENV=TEST ava -v", + "changelog:generate": "conventional-changelog --outfile CHANGELOG.md --release-count 0", + "readme:badges": "node node_modules/@wessberg/ts-config/readme/badge/helper/add-badges.js", + "readme:refresh": "npm run changelog:generate && npm run readme:badges", + "commit:readme": "npm run readme:refresh && git commit -am \"Bumped version\" --no-verify || true", + "clean:dist": "rm -r -f dist", + "clean:compiled": "rm -r -f compiled", + "clean": "npm run clean:dist && npm run clean:compiled", + "tsc:dist:cjs": "tsc --module commonjs --outDir dist/cjs -p tsconfig.dist.json", + "tsc:dist:es2015": "tsc --module es2015 --outDir dist/es2015 -p tsconfig.dist.json", + "tsc:test": "tsc --module commonjs --target es2017 --sourceMap", + "build:cjs": "npm run tsc:dist:cjs", + "build:es2015": "npm run tsc:dist:es2015", + "prebuild": "npm run clean", + "build": "npm run build:cjs & npm run build:es2015", + "tslint": "tslint -c tslint.json -p tsconfig.json", + "validate": "npm run tslint && npm run test", + "pretest": "npm run clean:compiled && npm run tsc:test", + "posttest": "npm run clean:compiled", + "prepublishOnly": "npm run validate && npm run build", + "precommit": "npm run tslint && exit 0", + "prepush": "npm run validate && exit 0", + "publish:major": "npm version major && npm run commit:readme && git push --no-verify && npm publish", + "publish:minor": "npm version minor && npm run commit:readme && git push --no-verify && npm publish", + "publish:patch": "npm version patch && npm run commit:readme && git push --no-verify && npm publish" }, "keywords": [ "color", @@ -26,28 +49,29 @@ "saturate", "random" ], + "author": { + "name": "Frederik Wessberg", + "email": "frederikwessberg@hotmail.com", + "url": "https://github.com/wessberg" + }, "devDependencies": { - "babel-cli": ">=6.11.4", - "babel-core": ">=6.6.5", - "babel-eslint": "^6.1.2", - "babel-loader": ">=6.2.4", - "babel-polyfill": ">=6.6.1", - "babel-plugin-transform-flow-strip-types": ">=6.8.0", - "babel-preset-es2015": ">=6.13.2", - "babel-preset-stage-1": ">=6.13.0", - "babel-register": ">=6.7.2", - "eslint": "^3.0.1", - "eslint-plugin-flowtype": ">=2.7.1", - "rollup": "^0.34.10", - "rollup-plugin-babel": "^2.6.1", - "rollup-plugin-commonjs": "^3.3.1", - "rollup-plugin-filesize": "^0.4.4", - "rollup-plugin-node-resolve": "^2.0.0", - "rollup-plugin-uglify": "^1.0.1", - "uglify-js": "github:mishoo/UglifyJS2#harmony", - "chai": "^3.5.0", - "eslint-plugin-babel": "^3.3.0", - "eslint-plugin-esplus": "^1.0.0", - "mocha": "^3.0.2" + "@wessberg/ts-config": "0.0.27", + "ava": "^0.25.0", + "conventional-changelog-cli": "^2.0.1", + "husky": "^0.14.3", + "tslint": "^5.10.0", + "typescript": "^2.9.2" + }, + "dependencies": { + "tslib": "^1.9.2" + }, + "main": "./dist/cjs/index.js", + "module": "./dist/es2015/index.js", + "browser": "./dist/es2015/index.js", + "types": "./dist/es2015/index.d.ts", + "typings": "./dist/es2015/index.d.ts", + "es2015": "./dist/es2015/index.js", + "engines": { + "node": ">=9.0.0" } } diff --git a/rollup.dist.config.js b/rollup.dist.config.js deleted file mode 100644 index 72bd6b5..0000000 --- a/rollup.dist.config.js +++ /dev/null @@ -1,44 +0,0 @@ -import babel from "rollup-plugin-babel"; -import filesize from "rollup-plugin-filesize"; -import nodeResolve from "rollup-plugin-node-resolve"; -import commonjs from "rollup-plugin-commonjs"; -import {minify} from "uglify-js"; -import uglify from "rollup-plugin-uglify"; - -export default { - entry: "./src/colorutilities.js", - format: "umd", - moduleId: "colorutilities", - moduleName: "colorutilities", - plugins: [ - nodeResolve({ - main: true, - jsnext: true - }), - commonjs(), - babel(), - uglify({ - compress: { - warnings: false, - dead_code: true, - unsafe: true, - drop_console: true, - unused: true, - loops: true, - booleans: true, - conditionals: true, - sequences: true, - properties: true, - comparisons: true, - if_return: true, - join_vars: true, - cascade: true, - collapse_vars: true - }, - screwIE8: true, - comments: false, - mangle: true - }, minify), - filesize() - ] -}; diff --git a/rollup.native.config.js b/rollup.native.config.js deleted file mode 100644 index 1b09a54..0000000 --- a/rollup.native.config.js +++ /dev/null @@ -1,41 +0,0 @@ -import babel from "rollup-plugin-babel"; -import filesize from "rollup-plugin-filesize"; -import nodeResolve from "rollup-plugin-node-resolve"; -import commonjs from "rollup-plugin-commonjs"; -import {minify} from "uglify-js"; -import uglify from "rollup-plugin-uglify"; - -export default { - entry: "./src/colorutilities.js", - plugins: [ - nodeResolve({ - main: true, - jsnext: true - }), - commonjs(), - babel(), - uglify({ - compress: { - warnings: false, - dead_code: true, - unsafe: true, - drop_console: true, - unused: true, - loops: true, - booleans: true, - conditionals: true, - sequences: true, - properties: true, - comparisons: true, - if_return: true, - join_vars: true, - cascade: true, - collapse_vars: true - }, - screwIE8: true, - comments: false, - mangle: true - }, minify), - filesize() - ] -}; diff --git a/src/colorutilities.js b/src/colorutilities.ts similarity index 54% rename from src/colorutilities.js rename to src/colorutilities.ts index 3593c39..4c9c1c2 100644 --- a/src/colorutilities.js +++ b/src/colorutilities.ts @@ -1,150 +1,157 @@ -// @flow +// tslint:disable:no-magic-numbers +// tslint:disable:binary-expression-operand-order /** -* A map between HTML color names and their hex values. -* @type {Object} -*/ -export const COLOR_NAMES = { - "aliceblue":"#f0f8ff", - "antiquewhite":"#faebd7", - "aqua":"#00ffff", - "aquamarine":"#7fffd4", - "azure":"#f0ffff", - "beige":"#f5f5dc", - "bisque":"#ffe4c4", - "black":"#000000", - "blanchedalmond":"#ffebcd", - "blue":"#0000ff", - "blueviolet":"#8a2be2", - "brown":"#a52a2a", - "burlywood":"#deb887", - "cadetblue":"#5f9ea0", - "chartreuse":"#7fff00", - "chocolate":"#d2691e", - "coral":"#ff7f50", - "cornflowerblue":"#6495ed", - "cornsilk":"#fff8dc", - "crimson":"#dc143c", - "cyan":"#00ffff", - "darkblue":"#00008b", - "darkcyan":"#008b8b", - "darkgoldenrod":"#b8860b", - "darkgray":"#a9a9a9", - "darkgreen":"#006400", - "darkkhaki":"#bdb76b", - "darkmagenta":"#8b008b", - "darkolivegreen":"#556b2f", - "darkorange":"#ff8c00", - "darkorchid":"#9932cc", - "darkred":"#8b0000", - "darksalmon":"#e9967a", - "darkseagreen":"#8fbc8f", - "darkslateblue":"#483d8b", - "darkslategray":"#2f4f4f", - "darkturquoise":"#00ced1", - "darkviolet":"#9400d3", - "deeppink":"#ff1493", - "deepskyblue":"#00bfff", - "dimgray":"#696969", - "dodgerblue":"#1e90ff", - "firebrick":"#b22222", - "floralwhite":"#fffaf0", - "forestgreen":"#228b22", - "fuchsia":"#ff00ff", - "gainsboro":"#dcdcdc", - "ghostwhite":"#f8f8ff", - "gold":"#ffd700", - "goldenrod":"#daa520", - "gray":"#808080", - "green":"#008000", - "greenyellow":"#adff2f", - "honeydew":"#f0fff0", - "hotpink":"#ff69b4", - "indianred ":"#cd5c5c", - "indigo":"#4b0082", - "ivory":"#fffff0", - "khaki":"#f0e68c", - "lavender":"#e6e6fa", - "lavenderblush":"#fff0f5", - "lawngreen":"#7cfc00", - "lemonchiffon":"#fffacd", - "lightblue":"#add8e6", - "lightcoral":"#f08080", - "lightcyan":"#e0ffff", - "lightgoldenrodyellow":"#fafad2", - "lightgrey":"#d3d3d3", - "lightgreen":"#90ee90", - "lightpink":"#ffb6c1", - "lightsalmon":"#ffa07a", - "lightseagreen":"#20b2aa", - "lightskyblue":"#87cefa", - "lightslategray":"#778899", - "lightsteelblue":"#b0c4de", - "lightyellow":"#ffffe0", - "lime":"#00ff00", - "limegreen":"#32cd32", - "linen":"#faf0e6", - "magenta":"#ff00ff", - "maroon":"#800000", - "mediumaquamarine":"#66cdaa", - "mediumblue":"#0000cd", - "mediumorchid":"#ba55d3", - "mediumpurple":"#9370d8", - "mediumseagreen":"#3cb371", - "mediumslateblue":"#7b68ee", - "mediumspringgreen":"#00fa9a", - "mediumturquoise":"#48d1cc", - "mediumvioletred":"#c71585", - "midnightblue":"#191970", - "mintcream":"#f5fffa", - "mistyrose":"#ffe4e1", - "moccasin":"#ffe4b5", - "navajowhite":"#ffdead", - "navy":"#000080", - "oldlace":"#fdf5e6", - "olive":"#808000", - "olivedrab":"#6b8e23", - "orange":"#ffa500", - "orangered":"#ff4500", - "orchid":"#da70d6", - "palegoldenrod":"#eee8aa", - "palegreen":"#98fb98", - "paleturquoise":"#afeeee", - "palevioletred":"#d87093", - "papayawhip":"#ffefd5", - "peachpuff":"#ffdab9", - "peru":"#cd853f", - "pink":"#ffc0cb", - "plum":"#dda0dd", - "powderblue":"#b0e0e6", - "purple":"#800080", - "red":"#ff0000", - "rosybrown":"#bc8f8f", - "royalblue":"#4169e1", - "saddlebrown":"#8b4513", - "salmon":"#fa8072", - "sandybrown":"#f4a460", - "seagreen":"#2e8b57", - "seashell":"#fff5ee", - "sienna":"#a0522d", - "silver":"#c0c0c0", - "skyblue":"#87ceeb", - "slateblue":"#6a5acd", - "slategray":"#708090", - "snow":"#fffafa", - "springgreen":"#00ff7f", - "steelblue":"#4682b4", - "tan":"#d2b48c", - "teal":"#008080", - "thistle":"#d8bfd8", - "tomato":"#ff6347", - "turquoise":"#40e0d0", - "violet":"#ee82ee", - "wheat":"#f5deb3", - "white":"#ffffff", - "whitesmoke":"#f5f5f5", - "yellow":"#ffff00", - "yellowgreen":"#9acd32" + * The maximum possible RGB value + * @type {number} + */ +const RGB_MAX_VALUE: number = 255; + +/** + * A map between HTML color names and their hex values. + * @type {Object} + */ +export const COLOR_NAMES: { [key: string]: string } = { + aliceblue: "#f0f8ff", + antiquewhite: "#faebd7", + aqua: "#00ffff", + aquamarine: "#7fffd4", + azure: "#f0ffff", + beige: "#f5f5dc", + bisque: "#ffe4c4", + black: "#000000", + blanchedalmond: "#ffebcd", + blue: "#0000ff", + blueviolet: "#8a2be2", + brown: "#a52a2a", + burlywood: "#deb887", + cadetblue: "#5f9ea0", + chartreuse: "#7fff00", + chocolate: "#d2691e", + coral: "#ff7f50", + cornflowerblue: "#6495ed", + cornsilk: "#fff8dc", + crimson: "#dc143c", + cyan: "#00ffff", + darkblue: "#00008b", + darkcyan: "#008b8b", + darkgoldenrod: "#b8860b", + darkgray: "#a9a9a9", + darkgreen: "#006400", + darkkhaki: "#bdb76b", + darkmagenta: "#8b008b", + darkolivegreen: "#556b2f", + darkorange: "#ff8c00", + darkorchid: "#9932cc", + darkred: "#8b0000", + darksalmon: "#e9967a", + darkseagreen: "#8fbc8f", + darkslateblue: "#483d8b", + darkslategray: "#2f4f4f", + darkturquoise: "#00ced1", + darkviolet: "#9400d3", + deeppink: "#ff1493", + deepskyblue: "#00bfff", + dimgray: "#696969", + dodgerblue: "#1e90ff", + firebrick: "#b22222", + floralwhite: "#fffaf0", + forestgreen: "#228b22", + fuchsia: "#ff00ff", + gainsboro: "#dcdcdc", + ghostwhite: "#f8f8ff", + gold: "#ffd700", + goldenrod: "#daa520", + gray: "#808080", + green: "#008000", + greenyellow: "#adff2f", + honeydew: "#f0fff0", + hotpink: "#ff69b4", + "indianred ": "#cd5c5c", + indigo: "#4b0082", + ivory: "#fffff0", + khaki: "#f0e68c", + lavender: "#e6e6fa", + lavenderblush: "#fff0f5", + lawngreen: "#7cfc00", + lemonchiffon: "#fffacd", + lightblue: "#add8e6", + lightcoral: "#f08080", + lightcyan: "#e0ffff", + lightgoldenrodyellow: "#fafad2", + lightgrey: "#d3d3d3", + lightgreen: "#90ee90", + lightpink: "#ffb6c1", + lightsalmon: "#ffa07a", + lightseagreen: "#20b2aa", + lightskyblue: "#87cefa", + lightslategray: "#778899", + lightsteelblue: "#b0c4de", + lightyellow: "#ffffe0", + lime: "#00ff00", + limegreen: "#32cd32", + linen: "#faf0e6", + magenta: "#ff00ff", + maroon: "#800000", + mediumaquamarine: "#66cdaa", + mediumblue: "#0000cd", + mediumorchid: "#ba55d3", + mediumpurple: "#9370d8", + mediumseagreen: "#3cb371", + mediumslateblue: "#7b68ee", + mediumspringgreen: "#00fa9a", + mediumturquoise: "#48d1cc", + mediumvioletred: "#c71585", + midnightblue: "#191970", + mintcream: "#f5fffa", + mistyrose: "#ffe4e1", + moccasin: "#ffe4b5", + navajowhite: "#ffdead", + navy: "#000080", + oldlace: "#fdf5e6", + olive: "#808000", + olivedrab: "#6b8e23", + orange: "#ffa500", + orangered: "#ff4500", + orchid: "#da70d6", + palegoldenrod: "#eee8aa", + palegreen: "#98fb98", + paleturquoise: "#afeeee", + palevioletred: "#d87093", + papayawhip: "#ffefd5", + peachpuff: "#ffdab9", + peru: "#cd853f", + pink: "#ffc0cb", + plum: "#dda0dd", + powderblue: "#b0e0e6", + purple: "#800080", + red: "#ff0000", + rosybrown: "#bc8f8f", + royalblue: "#4169e1", + saddlebrown: "#8b4513", + salmon: "#fa8072", + sandybrown: "#f4a460", + seagreen: "#2e8b57", + seashell: "#fff5ee", + sienna: "#a0522d", + silver: "#c0c0c0", + skyblue: "#87ceeb", + slateblue: "#6a5acd", + slategray: "#708090", + snow: "#fffafa", + springgreen: "#00ff7f", + steelblue: "#4682b4", + tan: "#d2b48c", + teal: "#008080", + thistle: "#d8bfd8", + tomato: "#ff6347", + turquoise: "#40e0d0", + violet: "#ee82ee", + wheat: "#f5deb3", + white: "#ffffff", + whitesmoke: "#f5f5f5", + yellow: "#ffff00", + yellowgreen: "#9acd32" }; /** @@ -161,32 +168,32 @@ export function hslStringToHslTuple (hsl: string): [number, string, string] { const result = hsl.slice(hsl.indexOf("(") + 1, hsl.lastIndexOf(")")).split(","); - const HUE = parseInt(result[0]); - const SATURATION = result[1].trim(); - const LIGHTNESS = result[2].trim(); + const HUE = parseInt(result[0]); + const SATURATION = result[1].trim(); + const LIGHTNESS = result[2].trim(); - if (isNaN(HUE)) throw new TypeError(`Couldn't decode the 'hue' value for the given hsl color: ${hsl}`); - if (isNaN(parseInt(SATURATION))) throw new TypeError(`Couldn't decode the 'saturation' value for the given hsl color: ${hsl}`); - if (isNaN(parseInt(LIGHTNESS))) throw new TypeError(`Couldn't decode the 'lightness' value for the given hsl color: ${hsl}`); + if (isNaN(HUE)) throw new TypeError(`Couldn't decode the 'hue' value for the given hsl color: ${hsl}`); + if (isNaN(parseInt(SATURATION))) throw new TypeError(`Couldn't decode the 'saturation' value for the given hsl color: ${hsl}`); + if (isNaN(parseInt(LIGHTNESS))) throw new TypeError(`Couldn't decode the 'lightness' value for the given hsl color: ${hsl}`); return [ HUE, - SATURATION.slice(SATURATION.length - 1) !== "%" ? `${SATURATION}%` : SATURATION, - LIGHTNESS.slice(LIGHTNESS.length - 1) !== "%" ? `${LIGHTNESS}%` : LIGHTNESS + SATURATION.slice(SATURATION.length - 1) !== "%" ? `${SATURATION}%` : SATURATION, + LIGHTNESS.slice(LIGHTNESS.length - 1) !== "%" ? `${LIGHTNESS}%` : LIGHTNESS ]; } /** -* Generates a HSLA color from a HSL color. -* @param {string} hsl - The HSL color. For instance, hsl(50, 100%, 100%). -* @param {number} [alpha=1] - The alpha channel value of the HSLA color. -* @returns {string} The HSLA version of the color. -* @throws {TypeError} If the first argument is not of type 'string'. -* @throws {TypeError} If the second argument is not of type 'number'. -*/ + * Generates a HSLA color from a HSL color. + * @param {string} hsl - The HSL color. For instance, hsl(50, 100%, 100%). + * @param {number} [alpha=1] - The alpha channel value of the HSLA color. + * @returns {string} The HSLA version of the color. + * @throws {TypeError} If the first argument is not of type 'string'. + * @throws {TypeError} If the second argument is not of type 'number'. + */ export function hslToHsla (hsl: string, alpha: number = 1): string { - if (typeof hsl !== "string") throw new TypeError(`first argument to 'hslToHsla' must be of type 'string'!`); - if (typeof alpha !== "number") throw new TypeError(`second argument to 'hslToHsla' must be of type 'number'!`); + if (typeof hsl !== "string") throw new TypeError(`first argument to 'hslToHsla' must be of type 'string'!`); + if (typeof alpha !== "number") throw new TypeError(`second argument to 'hslToHsla' must be of type 'number'!`); const [H, S, L] = hslStringToHslTuple(hsl); return `hsla(${H}, ${S}, ${L}, ${alpha})`; } @@ -208,13 +215,12 @@ export function hslaStringToHslaTuple (hsla: string): [number, string, string, n return [ HUE, - SATURATION.slice(SATURATION.length - 1) !== "%" ? `${SATURATION}%` : SATURATION, - LIGHTNESS.slice(LIGHTNESS.length - 1) !== "%" ? `${LIGHTNESS}%` : LIGHTNESS, + SATURATION.slice(SATURATION.length - 1) !== "%" ? `${SATURATION}%` : SATURATION, + LIGHTNESS.slice(LIGHTNESS.length - 1) !== "%" ? `${LIGHTNESS}%` : LIGHTNESS, ALPHA ]; } - /** * Converts the given HSL color to an RGB color and returns it as a tuple: [RED: number, GREEN: number, BLUE: number]. * @param {string} hsl - The HSL color to convert. @@ -239,9 +245,9 @@ export function hslToRgbTuple (hsl: string, rounding: boolean = true): [number, let g = 0; let b = 0; - // If there is no saturation, we only need to multiply the lightness by 255, the maximum span of the RGB color space. + // If there is no saturation, we only need to multiply the lightness by RGB_MAX_VALUE, the maximum span of the RGB color space. if (s === 0) { - r = g = b = l * 255; + r = g = b = l * RGB_MAX_VALUE; return rounding ? [Math.round(r), Math.round(g), Math.round(b)] : [r, g, b]; } @@ -255,39 +261,39 @@ export function hslToRgbTuple (hsl: string, rounding: boolean = true): [number, // Convert the 360 degrees in a circle to 1 by dividing the hue by 360. const calculatedHue = h / 360; - let temporaryRed = calculatedHue + (1 / 3); - let temporaryGreen = calculatedHue; - let temporaryBlue = calculatedHue - (1 / 3); + let temporaryRed = calculatedHue + (1 / 3); + let temporaryGreen = calculatedHue; + let temporaryBlue = calculatedHue - (1 / 3); // All values need to be between 0 and 1. For negative values, we must add 1. // For negative values, we must subtract 1. - if (temporaryRed < 0) ++temporaryRed; - else if (temporaryRed > 1) --temporaryRed; - if (temporaryGreen < 0) ++temporaryGreen; - else if (temporaryGreen > 1) --temporaryGreen; - if (temporaryBlue < 0) ++temporaryBlue; - else if (temporaryBlue > 1) --temporaryBlue; + if (temporaryRed < 0) ++temporaryRed; + else if (temporaryRed > 1) --temporaryRed; + if (temporaryGreen < 0) ++temporaryGreen; + else if (temporaryGreen > 1) --temporaryGreen; + if (temporaryBlue < 0) ++temporaryBlue; + else if (temporaryBlue > 1) --temporaryBlue; // Find the 'red' value. - if ((6 * temporaryRed) < 1) r = temp2 + (temp1 - temp2) * 6 * temporaryRed; - else if ((2 * temporaryRed) < 1) r = temp1; - else if ((3 * temporaryRed) < 2) r = temp2 + (temp1 - temp2) * ((2 / 3) - temporaryRed) * 6; - else r = temp2; + if ((6 * temporaryRed) < 1) r = temp2 + (temp1 - temp2) * 6 * temporaryRed; + else if ((2 * temporaryRed) < 1) r = temp1; + else if ((3 * temporaryRed) < 2) r = temp2 + (temp1 - temp2) * ((2 / 3) - temporaryRed) * 6; + else r = temp2; // Find the 'green' value. - if ((6 * temporaryGreen) < 1) g = temp2 + (temp1 - temp2) * 6 * temporaryGreen; - else if ((2 * temporaryGreen) < 1) g = temp1; - else if ((3 * temporaryGreen) < 2) g = temp2 + (temp1 - temp2) * ((2 / 3) - temporaryGreen) * 6; - else g = temp2; + if ((6 * temporaryGreen) < 1) g = temp2 + (temp1 - temp2) * 6 * temporaryGreen; + else if ((2 * temporaryGreen) < 1) g = temp1; + else if ((3 * temporaryGreen) < 2) g = temp2 + (temp1 - temp2) * ((2 / 3) - temporaryGreen) * 6; + else g = temp2; // Find the 'blue' value. - if ((6 * temporaryBlue) < 1) b = temp2 + (temp1 - temp2) * 6 * temporaryBlue; - else if ((2 * temporaryBlue) < 1) b = temp1; - else if ((3 * temporaryBlue) < 2) b = temp2 + (temp1 - temp2) * ((2 / 3) - temporaryBlue) * 6; - else b = temp2; + if ((6 * temporaryBlue) < 1) b = temp2 + (temp1 - temp2) * 6 * temporaryBlue; + else if ((2 * temporaryBlue) < 1) b = temp1; + else if ((3 * temporaryBlue) < 2) b = temp2 + (temp1 - temp2) * ((2 / 3) - temporaryBlue) * 6; + else b = temp2; - if (rounding) return [ Math.round(r * 255), Math.round(g * 255), Math.round(b * 255) ]; - else return [ r * 255, g * 255, b * 255]; + if (rounding) return [Math.round(r * RGB_MAX_VALUE), Math.round(g * RGB_MAX_VALUE), Math.round(b * RGB_MAX_VALUE)]; + else return [r * RGB_MAX_VALUE, g * RGB_MAX_VALUE, b * RGB_MAX_VALUE]; } /** @@ -301,10 +307,9 @@ export function hslToRgb (hsl: string, rounding: boolean = true): string { return `rgb(${R}, ${G}, ${B})`; } - /** * Converts the given HSLa color to an RGBa color and returns it as a tuple: [RED: number, GREEN: number, BLUE: number, ALPHA: number]. - * @param {string} hsl - The HSLa color to convert. + * @param {string} hsla - The HSLa color to convert. * @returns {[number, number, number, number]} The RGBa color as a tuple. */ export function hslaToRgbaTuple (hsla: string): [number, number, number, number] { @@ -317,7 +322,7 @@ export function hslaToRgbaTuple (hsla: string): [number, number, number, number] /** * Converts the given HSLa color to an RGBa color and returns it as a string. - * @param {string} hsl - The HSLa color to convert. + * @param {string} hsla - The HSLa color to convert. * @returns {string} The RGBa color as a string. */ export function hslaToRgba (hsla: string): string { @@ -327,7 +332,7 @@ export function hslaToRgba (hsla: string): string { /** * Converts the given HSLa color to an HSL color and returns it as a string. - * @param {string} hsl - The HSLa color to convert. + * @param {string} hsla - The HSLa color to convert. * @returns {string} The HSL color as a string. */ export function hslaToHsl (hsla: string): string { @@ -336,19 +341,20 @@ export function hslaToHsl (hsla: string): string { } /** -* Generates a tuple-representation of an RGB color: [RED: number, GREEN: number, BLUE: number]. -* @param {string} rgbString - An RGB string. For instance, rgb(255, 255, 255). -* @returns {[number, number, number]} A tuple representation of the RGB color. -* @throws {TypeError} If the first argument is not of type 'string'. -* @throws {TypeError} If an RGB color couldn't be extracted from the given string. -*/ -export function rgbStringToRgbTuple(rgbString: string): [number, number, number] { + * Generates a tuple-representation of an RGB color: [RED: number, GREEN: number, BLUE: number]. + * @param {string} rgbString - An RGB string. For instance, rgb(RGB_MAX_VALUE, RGB_MAX_VALUE, RGB_MAX_VALUE). + * @returns {[number, number, number]} A tuple representation of the RGB color. + * @throws {TypeError} If the first argument is not of type 'string'. + * @throws {TypeError} If an RGB color couldn't be extracted from the given string. + */ +export function rgbStringToRgbTuple (rgbString: string): [number, number, number] { if (typeof rgbString !== "string") throw new TypeError(`first argument must be of type 'string'!`); const rgb = rgbString.match(/rgb\((.+)\s*,\s*(.+)\s*,\s*(.+)\s*\)/); if (rgb == null) throw new TypeError(`'rgbToHex()' couldn't decode an RGB color from the given input: ${rgbString}`); - - let r = parseFloat(rgb[1]), g = parseFloat(rgb[2]), b = parseFloat(rgb[3]); + const r = parseFloat(rgb[1]); + const g = parseFloat(rgb[2]); + const b = parseFloat(rgb[3]); if (isNaN(r)) throw new TypeError(`'rgbToString()' couldn't decode the 'red' value of the given RGB color: '${rgbString}'.`); if (isNaN(g)) throw new TypeError(`'rgbToString()' couldn't decode the 'green' value of the given RGB color: '${rgbString}'.`); @@ -358,19 +364,21 @@ export function rgbStringToRgbTuple(rgbString: string): [number, number, number] } /** -* Generates a tuple-representation of an RGBa color: [RED: number, GREEN: number, BLUE: number, ALPHA: number]. -* @param {string} rgbaString - An RGBa string. For instance, rgb(255, 255, 255, 0.4). -* @returns {[number, number, number, number]} A tuple representation of the RGBa color. -* @throws {TypeError} If the first argument is not of type 'string'. -* @throws {TypeError} If an RGBa color couldn't be extracted from the given string. -*/ -export function rgbaStringToRgbaTuple(rgbaString: string): [number, number, number, number] { + * Generates a tuple-representation of an RGBa color: [RED: number, GREEN: number, BLUE: number, ALPHA: number]. + * @param {string} rgbaString - An RGBa string. For instance, rgb(RGB_MAX_VALUE, RGB_MAX_VALUE, RGB_MAX_VALUE, 0.4). + * @returns {[number, number, number, number]} A tuple representation of the RGBa color. + * @throws {TypeError} If the first argument is not of type 'string'. + * @throws {TypeError} If an RGBa color couldn't be extracted from the given string. + */ +export function rgbaStringToRgbaTuple (rgbaString: string): [number, number, number, number] { if (typeof rgbaString !== "string") throw new TypeError(`first argument must be of type 'string'!`); const rgba = rgbaString.match(/rgba\((.+)\s*,\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*\)/); if (rgba == null) throw new TypeError(`Couldn't decode an RGBa color from the given input: ${rgbaString}`); - - let r = parseFloat(rgba[1]), g = parseFloat(rgba[2]), b = parseFloat(rgba[3]), a = parseFloat(rgba[4]); + const r = parseFloat(rgba[1]); + const g = parseFloat(rgba[2]); + const b = parseFloat(rgba[3]); + const a = parseFloat(rgba[4]); if (isNaN(r)) throw new TypeError(`Couldn't decode the 'red' value of the given RGBa color: '${rgbaString}'.`); if (isNaN(g)) throw new TypeError(`Couldn't decode the 'green' value of the given RGBa color: '${rgbaString}'.`); @@ -381,27 +389,27 @@ export function rgbaStringToRgbaTuple(rgbaString: string): [number, number, numb } /** -* Generates a hex representation of an RGB color. -* @param {string} rgbString - An RGB string. For instance, rgb(255, 255, 255). -* @returns {string} A hex representation of the string. For instance #f5f5f5. -*/ -export function rgbToHex(rgbString: string): string { + * Generates a hex representation of an RGB color. + * @param {string} rgbString - An RGB string. For instance, rgb(RGB_MAX_VALUE, RGB_MAX_VALUE, RGB_MAX_VALUE). + * @returns {string} A hex representation of the string. For instance #f5f5f5. + */ +export function rgbToHex (rgbString: string): string { const [r, g, b] = rgbStringToRgbTuple(rgbString); - let bin = r << 16 | g << 8 | b; - return (function(h){ - return `#${new Array(7-h.length).join("0") + h }`; + const bin = r << 16 | g << 8 | b; + return (function (h) { + return `#${new Array(7 - h.length).join("0") + h }`; })(bin.toString(16).toLowerCase()); } /** -* Generates a hex representation of an RGBa color. -* @param {string} rgbString - An RGB string. For instance, rgb(255, 255, 255). -* @param {string} [againstColor] - If given, the generated hex color will count in interpolation between the alpha channel and the color. So, for instance, even though the alpha channel value is "0" (so its invisible), if the color below it is pure white, the visible color would still be white and the generated hex color would be '#ffffff' -* @returns {string} A hex representation of the string. For instance #f5f5f5. -* @throws {TypeError} If the second argument is given but is not of type 'string'. -*/ -export function rgbaToHex(rgbaString: string, againstColor?: string) { + * Generates a hex representation of an RGBa color. + * @param {string} rgbaString - An RGB string. For instance, rgb(RGB_MAX_VALUE, RGB_MAX_VALUE, RGB_MAX_VALUE). + * @param {string} [againstColor] - If given, the generated hex color will count in interpolation between the alpha channel and the color. So, for instance, even though the alpha channel value is "0" (so its invisible), if the color below it is pure white, the visible color would still be white and the generated hex color would be '#ffffff' + * @returns {string} A hex representation of the string. For instance #f5f5f5. + * @throws {TypeError} If the second argument is given but is not of type 'string'. + */ +export function rgbaToHex (rgbaString: string, againstColor?: string) { if (againstColor != null && typeof againstColor !== "string") throw new TypeError(`argument 'againstColor' must be of type 'string'!`); const [r, g, b, a] = rgbaStringToRgbaTuple(rgbaString); if (againstColor != null) { @@ -412,21 +420,22 @@ export function rgbaToHex(rgbaString: string, againstColor?: string) { } /** -* Generates a tuple-representation of an HSV/HSB color. -* @param {string} hsvString - An HSV/HSB string. For instance, hsb(5, 10, 20). -* @returns {[number, number, number]} A tuple representation of the HSV color. -* @throws {TypeError} If the first argument is not of type 'string'. -* @throws {TypeError} If an HSV color couldn't be extracted from the given string. -*/ -export function hsvStringToHsvTuple(hsvString: string): [number, number, number] { + * Generates a tuple-representation of an HSV/HSB color. + * @param {string} hsvString - An HSV/HSB string. For instance, hsb(5, 10, 20). + * @returns {[number, number, number]} A tuple representation of the HSV color. + * @throws {TypeError} If the first argument is not of type 'string'. + * @throws {TypeError} If an HSV color couldn't be extracted from the given string. + */ +export function hsvStringToHsvTuple (hsvString: string): [number, number, number] { if (typeof hsvString !== "string") throw new TypeError(`first argument must be of type 'string'!`); let hsv = hsvString.match(/hsv\((.+)\s*,\s*(.+)\s*,\s*(.+)\s*\)/); if (hsv == null) hsv = hsvString.match(/hsb\((.+)\s*,\s*(.+)\s*,\s*(.+)\s*\)/); if (hsv == null) throw new TypeError(`Couldn't decode an HSV color from the given input: ${hsvString}`); - - let h = parseFloat(hsv[1]), s = parseFloat(hsv[2]), v = parseFloat(hsv[3]); + const h = parseFloat(hsv[1]); + const s = parseFloat(hsv[2]); + const v = parseFloat(hsv[3]); if (isNaN(h)) throw new TypeError(`Couldn't decode the 'Hue' value of the given HSV color: '${hsvString}'.`); if (isNaN(s)) throw new TypeError(`Couldn't decode the 'Saturation' value of the given HSV color: '${hsvString}'.`); @@ -443,9 +452,9 @@ export function hsvStringToHsvTuple(hsvString: string): [number, number, number] export function hsvToRgbTuple (hsv: string): [number, number, number] { const [H, S, V] = hsvStringToHsvTuple(hsv); - const normalizedH = (H === 360) ? 1 : (H % 360 / parseFloat(360) * 6); - const normalizedS = (S === 100) ? 1 : (S % 100 / parseFloat(100)); - const normalizedV = (V === 100) ? 1 : (V % 100 / parseFloat(100)); + const normalizedH = (H === 360) ? 1 : (H % 360 / 360 * 6); + const normalizedS = (S === 100) ? 1 : (S % 100 / 100); + const normalizedV = (V === 100) ? 1 : (V % 100 / 100); const hFloor = Math.floor(normalizedH); const hFloorDiff = normalizedH - hFloor; @@ -456,7 +465,7 @@ export function hsvToRgbTuple (hsv: string): [number, number, number] { const R = [normalizedV, q, p, p, t, normalizedV][mod]; const G = [t, normalizedV, normalizedV, q, p, p][mod]; const B = [p, p, t, normalizedV, normalizedV, q][mod]; - return [Math.round(R * 255), Math.round(G * 255), Math.round(B * 255)]; + return [Math.round(R * RGB_MAX_VALUE), Math.round(G * RGB_MAX_VALUE), Math.round(B * RGB_MAX_VALUE)]; } /** @@ -470,14 +479,14 @@ export function hsvToRgb (hsv: string): string { } /** -* Takes a color and converts it to a hex color. -* @param {string} color - The color to normalize. -* @returns {string} The normalized hex color. -* @throws {TypeError} If the first argument is not of type 'string'. -* @throws {TypeError} If the given color appears to be a hex color but is of invalid length. -* @throws {TypeError} If the method didn't succeed in normalizing the given argument into a hex color. -*/ -export function toHex(color: string): string { + * Takes a color and converts it to a hex color. + * @param {string} color - The color to normalize. + * @returns {string} The normalized hex color. + * @throws {TypeError} If the first argument is not of type 'string'. + * @throws {TypeError} If the given color appears to be a hex color but is of invalid length. + * @throws {TypeError} If the method didn't succeed in normalizing the given argument into a hex color. + */ +export function toHex (color: string): string { if (typeof color !== "string") throw new TypeError(`first argument to 'toHex()' must be a string!`); if (color[0] === "#") { @@ -497,16 +506,16 @@ export function toHex(color: string): string { return rgbToHex(hsvToRgb(color)); } - else if (color.slice(0, 3) === "rgb") { - return rgbToHex(color); - } - else if (color.slice(0, 4) === "rgba") { return rgbaToHex(color); } + else if (color.slice(0, 3) === "rgb") { + return rgbToHex(color); + } + const colorNameMatch = COLOR_NAMES[color.toLowerCase()]; - if (colorNameMatch) return colorNameMatch; + if (colorNameMatch != null) return colorNameMatch; else if (/^[0-9A-F]{3}$|^[0-9A-F]{6}$/i.test(color)) { // Must be a hex color without a '#' in front of it. @@ -518,49 +527,49 @@ export function toHex(color: string): string { } /** -* Generates a RGB version of a hex color and returns it as a tuple: [RED: number, GREEN: number, BLUE: number]. -* @param {string} hex - The hex color to convert. For instance, #123456. -* @returns {[number, number, number]} A tuple of the red, green and blue values. -* @throws {TypeError} If the first argument is not a string. -* @throws {TypeError} If the given hex could not be decoded as a hex color. -*/ + * Generates a RGB version of a hex color and returns it as a tuple: [RED: number, GREEN: number, BLUE: number]. + * @param {string} hex - The hex color to convert. For instance, #123456. + * @returns {[number, number, number]} A tuple of the red, green and blue values. + * @throws {TypeError} If the first argument is not a string. + * @throws {TypeError} If the given hex could not be decoded as a hex color. + */ export function hexToRgbTuple (hex: string): [number, number, number] { if (typeof hex !== "string") throw new TypeError(`first argument to 'hexToRgbTuple' must be a string!`); const properHex = toHex(hex); // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; - const normalizedHex = properHex.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b); + const normalizedHex = properHex.replace(shorthandRegex, (_m, r, g, b) => r + r + g + g + b + b); const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(normalizedHex); - if (!result) throw new TypeError(`first argument to 'hexToRgb': '${hex}' could not be decoded as a hex color!`); + if (result == null) throw new TypeError(`first argument to 'hexToRgb': '${hex}' could not be decoded as a hex color!`); - const RED = parseInt(result[1], 16); + const RED = parseInt(result[1], 16); const GREEN = parseInt(result[2], 16); - const BLUE = parseInt(result[3], 16); + const BLUE = parseInt(result[3], 16); return [RED, GREEN, BLUE]; } /** -* Generates a RGB version of a hex color and returns it as a string. -* @param {string} hex - The hex color to convert. For instance, #f5f5f5. -* @returns {string} The RGB version of the color. -*/ + * Generates a RGB version of a hex color and returns it as a string. + * @param {string} hex - The hex color to convert. For instance, #f5f5f5. + * @returns {string} The RGB version of the color. + */ export function hexToRgb (hex: string): string { const [RED, GREEN, BLUE] = hexToRgbTuple(toHex(hex)); return `rgb(${RED}, ${GREEN}, ${BLUE})`; } /** -* Takes a color value or name and converts it to an RGB color. -* @param {string} color - The color to normalize. -* @returns {string} The normalized RGB color. -* @throws {TypeError} If the first argument is not of type 'string'. -* @throws {TypeError} If the given color appears to be a hex color but is of invalid length. -* @throws {TypeError} If the method didn't succeed in normalizing the given argument into a RGB color. -*/ -export function toRgb(color: string): string { + * Takes a color value or name and converts it to an RGB color. + * @param {string} color - The color to normalize. + * @returns {string} The normalized RGB color. + * @throws {TypeError} If the first argument is not of type 'string'. + * @throws {TypeError} If the given color appears to be a hex color but is of invalid length. + * @throws {TypeError} If the method didn't succeed in normalizing the given argument into a RGB color. + */ +export function toRgb (color: string): string { if (typeof color !== "string") throw new TypeError(`first argument to 'toRgb()' must be a string!`); if (color[0] === "#") { @@ -580,19 +589,18 @@ export function toRgb(color: string): string { return hsvToRgb(color); } + else if (color.slice(0, 4) === "rgba") { + return hexToRgb(rgbaToHex(color)); + } + else if (color.slice(0, 3) === "rgb") { // Only call 'rgbStringToRgbTuple' to validate the actual rgb color. Will throw errors if something is missing. rgbStringToRgbTuple(color); return color; } - else if (color.slice(0, 4) === "rgba") { - return hexToRgb(rgbaToHex(color)); - } - const colorNameMatch = COLOR_NAMES[color.toLowerCase()]; - if (colorNameMatch) return hexToRgb(colorNameMatch); - + if (colorNameMatch != null) return hexToRgb(colorNameMatch); else if (/^[0-9A-F]{3}$|^[0-9A-F]{6}$/i.test(color)) { // Must be a hex color without a '#' in front of it. @@ -604,26 +612,37 @@ export function toRgb(color: string): string { } /** -* Generates a HSL color from a hex color and returns it as a tuple: [HUE: number, SATURATION: string, LIGHTNESS: string] -* @param {string} hex - The hex color to convert. For instance, #123456. -* @param {boolean} [rounding=true] - If true, the h, s and l values will be rounded. -* @returns {[number, string, string]} A tuple of the HSL values. -*/ -export function hexToHslTuple(hex: string, rounding: boolean = true): [number, string, string] { + * Generates a HSL color from a hex color and returns it as a tuple: [HUE: number, SATURATION: string, LIGHTNESS: string] + * @param {string} hex - The hex color to convert. For instance, #123456. + * @param {boolean} [rounding=true] - If true, the h, s and l values will be rounded. + * @returns {[number, string, string]} A tuple of the HSL values. + */ +export function hexToHslTuple (hex: string, rounding: boolean = true): [number, string, string] { let [r, g, b] = hexToRgbTuple(toHex(hex)); - r /= 255, g /= 255, b /= 255; - let max = Math.max(r, g, b), min = Math.min(r, g, b); - let h = 0, s = 0, l = (max + min) / 2; + r /= RGB_MAX_VALUE; + g /= RGB_MAX_VALUE; + b /= RGB_MAX_VALUE; + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + let h = 0; + let s = 0; + let l = (max + min) / 2; if (max === min) h = s = 0; else { - let d = max - min; + const d = max - min; s = l > 0.5 ? d / (2 - max - min) : d / (max + min); switch (max) { - case r: h = (g - b) / d + (g < b ? 6 : 0); break; - case g: h = (b - r) / d + 2; break; - case b: h = (r - g) / d + 4; break; + case r: + h = (g - b) / d + (g < b ? 6 : 0); + break; + case g: + h = (b - r) / d + 2; + break; + case b: + h = (r - g) / d + 4; + break; } h /= 6; } @@ -636,11 +655,11 @@ export function hexToHslTuple(hex: string, rounding: boolean = true): [number, s } /** -* Generates a HSLA color from a hex color and returns it as a tuple: [HUE, SATURATION, LIGHTNESS, ALPHA] -* @param {string} hex - The hex color. For instance, #123456. -* @param {number} [alpha=1] - The alpha channel value of the HSLA color. -* @returns {[number, string, string, number]} A tuple of the hue, saturation, lightness and alpha values. -*/ + * Generates a HSLA color from a hex color and returns it as a tuple: [HUE, SATURATION, LIGHTNESS, ALPHA] + * @param {string} hex - The hex color. For instance, #123456. + * @param {number} [alpha=1] - The alpha channel value of the HSLA color. + * @returns {[number, string, string, number]} A tuple of the hue, saturation, lightness and alpha values. + */ export function hexToHslaTuple (hex: string, alpha: number = 1): [number, string, string, number] { if (typeof alpha !== "number") throw new TypeError(`second argument to 'hexToHslaTuple' must be of type 'number'!`); const [HUE, SATURATION, LIGHTNESS] = hexToHslTuple(hex); @@ -648,11 +667,11 @@ export function hexToHslaTuple (hex: string, alpha: number = 1): [number, string } /** -* Generates a HSLA color from a hex color and returns it as a string. -* @param {string} hex - The hex color. For instance, #123456. -* @param {number} [alpha=1] - The alpha channel value of the HSLA color. -* @returns {string} The generated HSLA color. -*/ + * Generates a HSLA color from a hex color and returns it as a string. + * @param {string} hex - The hex color. For instance, #123456. + * @param {number} [alpha=1] - The alpha channel value of the HSLA color. + * @returns {string} The generated HSLA color. + */ export function hexToHsla (hex: string, alpha: number = 1): string { if (typeof alpha !== "number") throw new TypeError(`second argument to 'hexToHslaTuple' must be of type 'number'!`); const [HUE, SATURATION, LIGHTNESS, ALPHA] = hexToHslaTuple(hex, alpha); @@ -660,11 +679,11 @@ export function hexToHsla (hex: string, alpha: number = 1): string { } /** -* Generates a HSL color from a hex color and returns it as a string. -* @param {string} hex - The hex color to convert. For instance, #123456. -* @returns {string} The converted HSL color. -*/ -export function hexToHsl(hex: string): string { + * Generates a HSL color from a hex color and returns it as a string. + * @param {string} hex - The hex color to convert. For instance, #123456. + * @returns {string} The converted HSL color. + */ +export function hexToHsl (hex: string): string { const [HUE, SATURATION, LIGHTNESS] = hexToHslTuple(hex); return `hsl(${HUE}, ${SATURATION}, ${LIGHTNESS})`; } @@ -678,15 +697,15 @@ export function hexToHsl(hex: string): string { export function rgbToHslTuple (rgb: string, rounding: boolean = true): [number, string, string] { const [R, G, B] = rgbStringToRgbTuple(rgb); - const normalizedRed = (R === 255) ? 1 : (R % 255 / parseFloat(255)); - const normalizedGreen = (G === 255) ? 1 : (G % 255 / parseFloat(255)); - const normalizedBlue = (B === 255) ? 1 : (B % 255 / parseFloat(255)); + const normalizedRed = (R === RGB_MAX_VALUE) ? 1 : (R % RGB_MAX_VALUE / RGB_MAX_VALUE); + const normalizedGreen = (G === RGB_MAX_VALUE) ? 1 : (G % RGB_MAX_VALUE / RGB_MAX_VALUE); + const normalizedBlue = (B === RGB_MAX_VALUE) ? 1 : (B % RGB_MAX_VALUE / RGB_MAX_VALUE); const max = Math.max(normalizedRed, normalizedGreen, normalizedBlue); const min = Math.min(normalizedRed, normalizedGreen, normalizedBlue); let h = 0; let s = 0; - let l = (max + min) / 2; + const l = (max + min) / 2; if (max !== min) { const diff = max - min; @@ -710,7 +729,7 @@ export function rgbToHslTuple (rgb: string, rounding: boolean = true): [number, h /= 6; } if (rounding) return [Math.round(h * 360), `${Math.round(s * 100)}%`, `${Math.round(l * 100)}%`]; - else return [h * 360, `${s * 100}%`, `${l * 100}%`]; + else return [h * 360, `${s * 100}%`, `${l * 100}%`]; } /** @@ -724,14 +743,14 @@ export function rgbToHsl (rgb: string): string { } /** -* Takes a color value or name and converts it to an HSL color. -* @param {string} color - The color to normalize. -* @returns {string} The normalized HSL color. -* @throws {TypeError} If the first argument is not of type 'string'. -* @throws {TypeError} If the given color appears to be a hex color but is of invalid length. -* @throws {TypeError} If the method didn't succeed in normalizing the given argument into a HSL color. -*/ -export function toHsl(color: string): string { + * Takes a color value or name and converts it to an HSL color. + * @param {string} color - The color to normalize. + * @returns {string} The normalized HSL color. + * @throws {TypeError} If the first argument is not of type 'string'. + * @throws {TypeError} If the given color appears to be a hex color but is of invalid length. + * @throws {TypeError} If the method didn't succeed in normalizing the given argument into a HSL color. + */ +export function toHsl (color: string): string { if (typeof color !== "string") throw new TypeError(`first argument to 'toHsl()' must be a string!`); if (color[0] === "#") { @@ -753,16 +772,16 @@ export function toHsl(color: string): string { return rgbToHsl(hsvToRgb(color)); } - else if (color.slice(0, 3) === "rgb") { - return rgbToHsl(color); - } - else if (color.slice(0, 4) === "rgba") { return hexToHsl(rgbaToHex(color)); } + else if (color.slice(0, 3) === "rgb") { + return rgbToHsl(color); + } + const colorNameMatch = COLOR_NAMES[color.toLowerCase()]; - if (colorNameMatch) return hexToHsl(colorNameMatch); + if (colorNameMatch != null) return hexToHsl(colorNameMatch); else if (/^[0-9A-F]{3}$|^[0-9A-F]{6}$/i.test(color)) { // Must be a hex color without a '#' in front of it. @@ -783,17 +802,16 @@ export function rgbToHsvTuple (rgb: string): [number, number, number] { if (typeof rgb !== "string") throw new TypeError(`first argument must be a string!`); const [R, G, B] = rgbStringToRgbTuple(rgb); - const normalizedRed = (R === 255) ? 1 : (R % 255 / parseFloat(255)); - const normalizedGreen = (G === 255) ? 1 : (G % 255 / parseFloat(255)); - const normalizedBlue = (B === 255) ? 1 : (B % 255 / parseFloat(255)); + const normalizedRed = (R === RGB_MAX_VALUE) ? 1 : (R % RGB_MAX_VALUE / RGB_MAX_VALUE); + const normalizedGreen = (G === RGB_MAX_VALUE) ? 1 : (G % RGB_MAX_VALUE / RGB_MAX_VALUE); + const normalizedBlue = (B === RGB_MAX_VALUE) ? 1 : (B % RGB_MAX_VALUE / RGB_MAX_VALUE); const max = Math.max(normalizedRed, normalizedGreen, normalizedBlue); const min = Math.min(normalizedRed, normalizedGreen, normalizedBlue); let h = 0; - let s = 0; - let v = max; - let diff = max - min; - s = max === 0 ? 0 : diff / max; + const v = max; + const diff = max - min; + const s = max === 0 ? 0 : diff / max; if (max === min) h = 0; else { switch (max) { @@ -824,14 +842,14 @@ export function rgbToHsv (rgb: string): string { } /** -* Takes a color value or name and converts it to an HSV/HSB color. -* @param {string} color - The color to normalize. -* @returns {string} The normalized HSV/HSB color. -* @throws {TypeError} If the first argument is not of type 'string'. -* @throws {TypeError} If the given color appears to be a hex color but is of invalid length. -* @throws {TypeError} If the method didn't succeed in normalizing the given argument into a HSV/HSB color. -*/ -export function toHsv(color: string): string { + * Takes a color value or name and converts it to an HSV/HSB color. + * @param {string} color - The color to normalize. + * @returns {string} The normalized HSV/HSB color. + * @throws {TypeError} If the first argument is not of type 'string'. + * @throws {TypeError} If the given color appears to be a hex color but is of invalid length. + * @throws {TypeError} If the method didn't succeed in normalizing the given argument into a HSV/HSB color. + */ +export function toHsv (color: string): string { if (typeof color !== "string") throw new TypeError(`first argument to 'toHsv()' must be a string!`); if (color[0] === "#") { @@ -853,16 +871,16 @@ export function toHsv(color: string): string { return color; } - else if (color.slice(0, 3) === "rgb") { - return rgbToHsv(color); - } - else if (color.slice(0, 4) === "rgba") { return rgbToHsv(hexToRgb(rgbaToHex(color))); } + else if (color.slice(0, 3) === "rgb") { + return rgbToHsv(color); + } + const colorNameMatch = COLOR_NAMES[color.toLowerCase()]; - if (colorNameMatch) return rgbToHsv(hexToRgb(colorNameMatch)); + if (colorNameMatch != null) return rgbToHsv(hexToRgb(colorNameMatch)); else if (/^[0-9A-F]{3}$|^[0-9A-F]{6}$/i.test(color)) { // Must be a hex color without a '#' in front of it. @@ -874,112 +892,111 @@ export function toHsv(color: string): string { } /** -* Generates a random hex color and returns it. -* @returns {string} a hex color. -*/ + * Generates a random hex color and returns it. + * @returns {string} a hex color. + */ export function randomHexColor (): string { - let color = (Math.random()*0xFFFFFF<<0).toString(16); + const color = (Math.random() * 0xFFFFFF << 0).toString(16); if (color.length < 6) return randomHexColor(); - else return `#${color}`; + else return `#${color}`; } - /** -* Generates a random RGB color and returns it. -* @returns {string} an RGB color. -*/ + * Generates a random RGB color and returns it. + * @returns {string} an RGB color. + */ export function randomRgbColor (): string { return hexToRgb(randomHexColor()); } /** -* Generates a random HSL color and returns it. -* @returns {string} an HSL color. -*/ + * Generates a random HSL color and returns it. + * @returns {string} an HSL color. + */ export function randomHslColor (): string { return hexToHsl(randomHexColor()); } /** -* Generates a random HSV/HSB color and returns it. -* @returns {string} an HSV/HSB color. -*/ + * Generates a random HSV/HSB color and returns it. + * @returns {string} an HSV/HSB color. + */ export function randomHsvColor (): string { return rgbToHsv(randomRgbColor()); } /** -* Changes the saturation of a HSL color. -* @param {string} hsl - The HSL color. For instance, hsl(50, 100%, 100%). -* @param {number} [percentage=10] - The percentage with which to saturate the color. -* @returns {string} The new HSL color. -*/ + * Changes the saturation of a HSL color. + * @param {string} hsl - The HSL color. For instance, hsl(50, 100%, 100%). + * @param {number} [percentage=10] - The percentage with which to saturate the color. + * @returns {string} The new HSL color. + */ export function saturateHsl (hsl: string, percentage: number = 10): string { const [H, S, L] = hslStringToHslTuple(hsl); return `hsl(${H}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(S) / 100))}%, ${L})`; } /** -* Changes the lightness of a HSL color. -* @param {string} hsl - The HSL color. For instance, hsl(50, 100%, 100%). -* @param {number} [percentage=10] - The percentage with which to lighten the color. -* @returns {string} The new HSL color. -*/ + * Changes the lightness of a HSL color. + * @param {string} hsl - The HSL color. For instance, hsl(50, 100%, 100%). + * @param {number} [percentage=10] - The percentage with which to lighten the color. + * @returns {string} The new HSL color. + */ export function lightenHsl (hsl: string, percentage: number = 10): string { const [H, S, L] = hslStringToHslTuple(hsl); return `hsl(${H}, ${S}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(L) / 100))}%)`; } /** -* Changes the saturation of a HSLa color. -* @param {string} hsla - The HSLa color. For instance, hsla(50, 100%, 100%, 0.5). -* @param {number} [percentage=10] - The percentage with which to saturate the color. -* @returns {string} The new HSLa color. -*/ + * Changes the saturation of a HSLa color. + * @param {string} hsla - The HSLa color. For instance, hsla(50, 100%, 100%, 0.5). + * @param {number} [percentage=10] - The percentage with which to saturate the color. + * @returns {string} The new HSLa color. + */ export function saturateHsla (hsla: string, percentage: number = 10): string { const [H, S, L, A] = hslaStringToHslaTuple(hsla); return `hsla(${H}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(S) / 100))}%, ${L}, ${A})`; } /** -* Changes the lightness of a HSLa color. -* @param {string} hsla - The HSLa color. For instance, hsla(50, 100%, 100%, 0.5). -* @param {number} [percentage=10] - The percentage with which to lighten the color. -* @returns {string} The new HSLa color. -*/ + * Changes the lightness of a HSLa color. + * @param {string} hsla - The HSLa color. For instance, hsla(50, 100%, 100%, 0.5). + * @param {number} [percentage=10] - The percentage with which to lighten the color. + * @returns {string} The new HSLa color. + */ export function lightenHsla (hsla: string, percentage: number = 10): string { const [H, S, L, A] = hslaStringToHslaTuple(hsla); return `hsla(${H}, ${S}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(L) / 100))}%, ${A})`; } /** -* Changes the saturation of a RGB color. -* @param {string} rgb - The RGB color. For instance, rgb(255, 80, 30). -* @param {number} [percentage=10] - The percentage with which to saturate the color. -* @returns {string} The new RGB color. -*/ + * Changes the saturation of a RGB color. + * @param {string} rgb - The RGB color. For instance, rgb(RGB_MAX_VALUE, 80, 30). + * @param {number} [percentage=10] - The percentage with which to saturate the color. + * @returns {string} The new RGB color. + */ export function saturateRgb (rgb: string, percentage: number = 10): string { const [H, S, L] = rgbToHslTuple(rgb); return hslToRgb(`hsl(${H}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(S) / 100))}%, ${L})`); } /** -* Changes the lightness of a RGB color. -* @param {string} rgb - The RGB color. For instance, rgb(255, 80, 30). -* @param {number} [percentage=10] - The percentage with which to lighten the color. -* @returns {string} The new RGB color. -*/ + * Changes the lightness of a RGB color. + * @param {string} rgb - The RGB color. For instance, rgb(255, 80, 30). + * @param {number} [percentage=10] - The percentage with which to lighten the color. + * @returns {string} The new RGB color. + */ export function lightenRgb (rgb: string, percentage: number = 10): string { const [H, S, L] = rgbToHslTuple(rgb); return hslToRgb(`hsl(${H}, ${S}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(L) / 100))}%)`); } /** -* Changes the saturation of a RGBa color. -* @param {string} rgba - The RGBa color. For instance, rgba(255, 80, 30, 0.5). -* @param {number} [percentage=10] - The percentage with which to saturate the color. -* @returns {string} The new RGBa color. -*/ + * Changes the saturation of a RGBa color. + * @param {string} rgba - The RGBa color. For instance, rgba(255, 80, 30, 0.5). + * @param {number} [percentage=10] - The percentage with which to saturate the color. + * @returns {string} The new RGBa color. + */ export function saturateRgba (rgba: string, percentage: number = 10): string { const [R, G, B, A] = rgbaStringToRgbaTuple(rgba); const [H, S, L] = rgbToHslTuple(`rgb(${R}, ${G}, ${B})`); @@ -988,11 +1005,11 @@ export function saturateRgba (rgba: string, percentage: number = 10): string { } /** -* Changes the lightness of a RGBa color. -* @param {string} rgba - The RGBa color. For instance, rgba(255, 80, 30, 0.5). -* @param {number} [percentage=10] - The percentage with which to lighten the color. -* @returns {string} The new RGBa color. -*/ + * Changes the lightness of a RGBa color. + * @param {string} rgba - The RGBa color. For instance, rgba(255, 80, 30, 0.5). + * @param {number} [percentage=10] - The percentage with which to lighten the color. + * @returns {string} The new RGBa color. + */ export function lightenRgba (rgba: string, percentage: number = 10): string { const [R, G, B, A] = rgbaStringToRgbaTuple(rgba); const [H, S, L] = rgbToHslTuple(`rgb(${R}, ${G}, ${B})`); @@ -1001,45 +1018,45 @@ export function lightenRgba (rgba: string, percentage: number = 10): string { } /** -* Changes the saturation of a HEX color. -* @param {string} hex - The hex color. For instance, #f5f5f5 -* @param {number} [percentage=10] - The percentage with which to saturate the color. -* @returns {string} The new hex color. -*/ + * Changes the saturation of a HEX color. + * @param {string} hex - The hex color. For instance, #f5f5f5 + * @param {number} [percentage=10] - The percentage with which to saturate the color. + * @returns {string} The new hex color. + */ export function saturateHex (hex: string, percentage: number = 10): string { const [H, S, L] = hexToHslTuple(hex); return rgbToHex(hslToRgb(`hsl(${H}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(S) / 100))}%, ${L})`)); } /** -* Changes the lightness of a HEX color. -* @param {string} hex - The hex color. For instance, #f5f5f5 -* @param {number} [percentage=10] - The percentage with which to lighten the color. -* @returns {string} The new hex color. -*/ + * Changes the lightness of a HEX color. + * @param {string} hex - The hex color. For instance, #f5f5f5 + * @param {number} [percentage=10] - The percentage with which to lighten the color. + * @returns {string} The new hex color. + */ export function lightenHex (hex: string, percentage: number = 10): string { const [H, S, L] = hexToHslTuple(hex); return rgbToHex(hslToRgb(`hsl(${H}, ${S}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(L) / 100))}%)`)); } /** -* Changes the saturation of a HSV/HSB color. -* @param {string} hsv - The HSV/HSB color. For instance, hsb(15, 30, 55). -* @param {number} [percentage=10] - The percentage with which to saturate the color. -* @returns {string} The new HSV/HSB color. -*/ + * Changes the saturation of a HSV/HSB color. + * @param {string} hsv - The HSV/HSB color. For instance, hsb(15, 30, 55). + * @param {number} [percentage=10] - The percentage with which to saturate the color. + * @returns {string} The new HSV/HSB color. + */ export function saturateHsv (hsv: string, percentage: number = 10): string { const [H, S, V] = hsvStringToHsvTuple(hsv); const hsbOrHsv = hsv.slice(0, 3) === "hsb" ? "hsb" : "hsv"; - return `${hsbOrHsv}(${H}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(S) / 100))}, ${V})`; + return `${hsbOrHsv}(${H}, ${Math.max(0, Math.min(100, (100 + percentage) * S / 100))}, ${V})`; } /** -* Changes the lightness of a HSV/HSB color. -* @param {string} hsv - The HSV/HSB color. For instance, hsb(15, 30, 55). -* @param {number} [percentage=10] - The percentage with which to lighten the color. -* @returns {string} The new HSV/HSB color. -*/ + * Changes the lightness of a HSV/HSB color. + * @param {string} hsv - The HSV/HSB color. For instance, hsb(15, 30, 55). + * @param {number} [percentage=10] - The percentage with which to lighten the color. + * @returns {string} The new HSV/HSB color. + */ export function lightenHsv (hsv: string, percentage: number = 10): string { const [H, S, L] = rgbToHslTuple(hsvToRgb(hsv)); return rgbToHsv(hslToRgb(`hsl(${H}, ${S}, ${Math.max(0, Math.min(100, (100 + percentage) * parseFloat(L) / 100))})`)); @@ -1055,11 +1072,11 @@ export function lightenHsv (hsv: string, percentage: number = 10): string { export function saturate (color: string, percentage: number = 10): string { if (color.slice(0, 4) === "rgba") return saturateRgba(color, percentage); if (color.slice(0, 4) === "hsla") return saturateHsla(color, percentage); - if (color.slice(0, 3) === "rgb") return saturateRgb(color, percentage); - if (color.slice(0, 3) === "hsl") return saturateHsl(color, percentage); - if (color.slice(0, 3) === "hsb") return saturateHsv(color, percentage); - if (color.slice(0, 3) === "hsv") return saturateHsv(color, percentage); - else return saturateHex(color, percentage); + if (color.slice(0, 3) === "rgb") return saturateRgb(color, percentage); + if (color.slice(0, 3) === "hsl") return saturateHsl(color, percentage); + if (color.slice(0, 3) === "hsb") return saturateHsv(color, percentage); + if (color.slice(0, 3) === "hsv") return saturateHsv(color, percentage); + else return saturateHex(color, percentage); } /** @@ -1072,18 +1089,18 @@ export function saturate (color: string, percentage: number = 10): string { export function lighten (color: string, percentage: number = 10): string { if (color.slice(0, 4) === "rgba") return lightenRgba(color, percentage); if (color.slice(0, 4) === "hsla") return lightenHsla(color, percentage); - if (color.slice(0, 3) === "rgb") return lightenRgb(color, percentage); - if (color.slice(0, 3) === "hsl") return lightenHsl(color, percentage); - if (color.slice(0, 3) === "hsb") return lightenHsv(color, percentage); - if (color.slice(0, 3) === "hsv") return lightenHsv(color, percentage); - else return lightenHex(color, percentage); + if (color.slice(0, 3) === "rgb") return lightenRgb(color, percentage); + if (color.slice(0, 3) === "hsl") return lightenHsl(color, percentage); + if (color.slice(0, 3) === "hsb") return lightenHsv(color, percentage); + if (color.slice(0, 3) === "hsv") return lightenHsv(color, percentage); + else return lightenHex(color, percentage); } /** -* Determines if the given color is light or not. -* @param {string} color - The color to check. Can be a hex, RGB, RGBa, HSL, HSLa, HSV, HSB or color name. -* @returns {boolean} Whether or not the color is light. -*/ + * Determines if the given color is light or not. + * @param {string} color - The color to check. Can be a hex, RGB, RGBa, HSL, HSLa, HSV, HSB or color name. + * @returns {boolean} Whether or not the color is light. + */ export function isLight (color: string): boolean { const luminance = parseFloat(hslStringToHslTuple(toHsl(color))[2]); return luminance > 40; diff --git a/src/test/colorutilities.test.js b/src/test/colorutilities.test.js deleted file mode 100644 index 302c26c..0000000 --- a/src/test/colorutilities.test.js +++ /dev/null @@ -1,2737 +0,0 @@ -import { - hslToHsla, - hslStringToHslTuple, - hslaStringToHslaTuple, - hslToRgbTuple, - hslToRgb, - hslaToRgbaTuple, - hslaToRgba, - hslaToHsl, - rgbStringToRgbTuple, - rgbaStringToRgbaTuple, - rgbToHex, - rgbaToHex, - hsvStringToHsvTuple, - hsvToRgbTuple, - hsvToRgb, - toHex, - hexToRgbTuple, - hexToRgb, - toRgb, - hexToHslTuple, - hexToHslaTuple, - hexToHsla, - hexToHsl, - rgbToHslTuple, - rgbToHsl, - toHsl, - rgbToHsvTuple, - rgbToHsv, - toHsv, - randomHexColor, - randomRgbColor, - randomHslColor, - randomHsvColor, - saturateHsl, - saturateHsla, - saturateHsv, - saturateRgb, - saturateRgba, - saturateHex, - saturate, - lightenHsl, - lightenHsla, - lightenHsv, - lightenRgb, - lightenRgba, - lightenHex, - lighten, - isLight -} from "../colorutilities"; - -describe("Colorutilities", function () { - - context("#hslStringToHslTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hslStringToHslTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the 'hue' property of the 'hsl' color isn't valid.", function () { - - try { - hslStringToHslTuple("hsl(false, 50%, 100%)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the 'saturation' property of the 'hsl' color isn't valid.", function () { - - try { - hslStringToHslTuple("hsl(30, false, 100%)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the 'lightness' property of the 'hsl' color isn't valid.", function () { - - try { - hslStringToHslTuple("hsl(30, 50%, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts string represented hsl colors to tuple represented hsl colors. #1", function () { - const [H, S, L] = hslStringToHslTuple("hsl(50, 60%, 100%)"); - - assert.equal(H, 50); - assert.equal(S, "60%"); - assert.equal(L, "100%"); - }); - - it("Converts string represented hsl colors to tuple represented hsl colors. #2", function () { - const [H, S, L] = hslStringToHslTuple("hsl(50, 60, 100)"); - - assert.equal(H, 50); - assert.equal(S, "60%"); - assert.equal(L, "100%"); - }); - - }); - - context("#hslaStringToHslaTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hslaStringToHslaTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the 'hue' property of the 'hsla' color isn't valid.", function () { - - try { - hslaStringToHslaTuple("hsla(false, 50%, 100%, 1)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the 'saturation' property of the 'hsla' color isn't valid.", function () { - - try { - hslaStringToHslaTuple("hsla(30, false, 100%, 1)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the 'lightness' property of the 'hsla' color isn't valid.", function () { - - try { - hslaStringToHslaTuple("hsla(30, 50%, false, 1)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the 'alpha' property of the 'hsla' color isn't valid.", function () { - - try { - hslaStringToHslaTuple("hsla(30, 50%, 100%, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts string represented hsla colors to tuple represented hsla colors. #1", function () { - const [H, S, L, A] = hslaStringToHslaTuple("hsl(50, 60%, 100%, 0.5)"); - - assert.equal(H, 50); - assert.equal(S, "60%"); - assert.equal(L, "100%"); - assert.equal(A, 0.5); - }); - - it("Converts string represented hsla colors to tuple represented hsla colors. #2", function () { - const [H, S, L, A] = hslaStringToHslaTuple("hsl(50, 60, 100, 0.9)"); - - assert.equal(H, 50); - assert.equal(S, "60%"); - assert.equal(L, "100%"); - assert.equal(A, 0.9); - }); - - }); - - context("#hslToHsla()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hslToHsla(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSL color", function () { - - try { - hslToHsla("hsl(true, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the second argument is not of type 'number'", function () { - - try { - hslToHsla("hsl(50, 100%, 50%)", null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts hsl colors to hsla colors. #1", function () { - assert.equal(hslToHsla("hsl(50, 100%, 100%)"), "hsla(50, 100%, 100%, 1)"); - }); - - it("Converts hsl colors to hsla colors. #2", function () { - assert.equal(hslToHsla("hsl(50, 100%, 100%)", 0.5), "hsla(50, 100%, 100%, 0.5)"); - }); - - it("Automatically adds missing '%' symbols for saturation and lightness.", function () { - assert.equal(hslToHsla("hsl(50, 100, 100)"), "hsla(50, 100%, 100%, 1)"); - }); - - }); - - context("#hslToRgbTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hslToRgbTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSL color", function () { - - try { - hslToRgbTuple("hsl(true, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts hsl colors to rgb colors. #1", function () { - const [R, G, B] = hslToRgbTuple("hsl(50, 100%, 100%)"); - assert.equal(R, 255); - assert.equal(G, 255); - assert.equal(B, 255); - }); - - it("Converts hsl colors to rgb colors. #2", function () { - const [R, G, B] = hslToRgbTuple("hsl(50, 84%, 44%)"); - assert.equal(R, 206); - assert.equal(G, 175); - assert.equal(B, 18); - }); - - it("Converts hsl colors to rgb colors. #3", function () { - const [R, G, B] = hslToRgbTuple("hsl(194, 63%, 8%)"); - assert.equal(R, 8); - assert.equal(G, 27); - assert.equal(B, 33); - }); - - it("Works, even if '%' symbols are omitted from the saturation and lightness properties.", function () { - const [R, G, B] = hslToRgbTuple("hsl(194, 63, 8)"); - assert.equal(R, 8); - assert.equal(G, 27); - assert.equal(B, 33); - }); - - }); - - context("#hslToRgb()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hslToRgb(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSL color", function () { - - try { - hslToRgb("hsl(true, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts hsl colors to rgb colors. #1", function () { - assert.equal(hslToRgb("hsl(50, 100%, 100%)"), "rgb(255, 255, 255)"); - }); - - it("Converts hsl colors to rgb colors. #2", function () { - assert.equal(hslToRgb("hsl(50, 84%, 44%)"), "rgb(206, 175, 18)"); - }); - - it("Converts hsl colors to rgb colors. #3", function () { - assert.equal(hslToRgb("hsl(194, 63%, 8%)"), "rgb(8, 27, 33)"); - }); - - it("Works, even if '%' symbols are omitted from the saturation and lightness properties.", function () { - assert.equal(hslToRgb("hsl(194, 63, 8)"), "rgb(8, 27, 33)"); - }); - - }); - - context("#hslaToRgbaTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hslaToRgbaTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSLa color", function () { - - try { - hslaToRgbaTuple("hsl(true, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts HSLa colors to RGBa colors. #1", function () { - const [R, G, B, A] = hslaToRgbaTuple("hsla(118, 100%, 52%, 0.56)"); - assert.equal(R, 18); - assert.equal(G, 255); - assert.equal(B, 10); - assert.equal(A, 0.56); - }); - - it("Converts HSLa colors to RGBa colors. #2", function () { - const [R, G, B, A] = hslaToRgbaTuple("hsla(36, 31%, 69%, 0.84)"); - assert.equal(R, 200); - assert.equal(G, 181); - assert.equal(B, 151); - assert.equal(A, 0.84); - }); - - it("Converts HSLa colors to RGBa colors. #3", function () { - const [R, G, B, A] = hslaToRgbaTuple("hsla(360, 100%, 100%, 0.00)"); - assert.equal(R, 255); - assert.equal(G, 255); - assert.equal(B, 255); - assert.equal(A, 0); - }); - - it("Works, even if '%' symbols are omitted from the saturation and lightness properties.", function () { - const [R, G, B, A] = hslaToRgbaTuple("hsla(36, 31, 69, 0.84)"); - assert.equal(R, 200); - assert.equal(G, 181); - assert.equal(B, 151); - assert.equal(A, 0.84); - }); - - }); - - context("#hslaToRgba()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hslaToRgba(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSLa color", function () { - - try { - hslaToRgba("hsl(true, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - - it("Converts HSLa colors to RGBa colors. #1", function () { - assert.equal(hslaToRgba("hsla(118, 100%, 52%, 0.56)"), "rgba(18, 255, 10, 0.56)"); - }); - - it("Converts HSLa colors to RGBa colors. #2", function () { - assert.equal(hslaToRgba("hsla(36, 31%, 69%, 0.84)"), "rgba(200, 181, 151, 0.84)"); - }); - - it("Converts HSLa colors to RGBa colors. #3", function () { - assert.equal(hslaToRgba("hsla(360, 100%, 100%, 0.00)"), "rgba(255, 255, 255, 0)"); - }); - - it("Works, even if '%' symbols are omitted from the saturation and lightness properties.", function () { - assert.equal(hslaToRgba("hsla(36, 31, 69, 0.84)"), "rgba(200, 181, 151, 0.84)"); - }); - - }); - - context("#hslaToHsl()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hslaToHsl(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSLa color", function () { - - try { - hslaToHsl("hsl(true, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts HSLa colors to hsl colors. #1", function () { - assert.equal(hslaToHsl("hsla(118, 100%, 52%, 0.56)"), "hsl(118, 100%, 52%)"); - }); - - it("Converts HSLa colors to RGBa colors. #2", function () { - assert.equal(hslaToHsl("hsla(36, 31%, 69%, 0.84)"), "hsl(36, 31%, 69%)"); - }); - - it("Converts HSLa colors to RGBa colors. #3", function () { - assert.equal(hslaToHsl("hsla(360, 100%, 100%, 0.00)"), "hsl(360, 100%, 100%)"); - }); - - it("Works, even if '%' symbols are omitted from the saturation and lightness properties.", function () { - assert.equal(hslaToHsl("hsla(36, 31, 69, 0.84)"), "hsl(36, 31%, 69%)"); - }); - - }); - - context("#rgbStringToRgbTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - rgbStringToRgbTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper rgb color", function () { - - try { - rgbStringToRgbTuple("rgb(true, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts string representations of RGB colors to tuple representations. #1", function () { - const [R, G, B] = rgbStringToRgbTuple("rgb(50, 100, 200)"); - assert.equal(R, 50); - assert.equal(G, 100); - assert.equal(B, 200); - }); - - it("Converts string representations of RGB colors to tuple representations. #2", function () { - const [R, G, B] = rgbStringToRgbTuple("rgb(20, 85, 255)"); - assert.equal(R, 20); - assert.equal(G, 85); - assert.equal(B, 255); - }); - - it("Converts string representations of RGB colors to tuple representations. #3", function () { - const [R, G, B] = rgbStringToRgbTuple("rgb(0, 0, 80)"); - assert.equal(R, 0); - assert.equal(G, 0); - assert.equal(B, 80); - }); - - }); - - context("#rgbaStringToRgbaTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - rgbaStringToRgbaTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - }); - - it("Throws an exception if the first argument is not a proper RGBa color", function () { - - try { - rgbaStringToRgbaTuple("rgba(true, false, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - }); - - it("Converts string representations of RGBa colors to tuple representations. #1", function () { - const [R, G, B, A] = rgbaStringToRgbaTuple("rgba(50, 100, 200, 0.9)"); - assert.equal(R, 50); - assert.equal(G, 100); - assert.equal(B, 200); - assert.equal(A, 0.9); - }); - - it("Converts string representations of RGBa colors to tuple representations. #2", function () { - const [R, G, B, A] = rgbaStringToRgbaTuple("rgba(140, 10, 89, 0.2)"); - assert.equal(R, 140); - assert.equal(G, 10); - assert.equal(B, 89); - assert.equal(A, 0.2); - }); - - it("Converts string representations of RGBa colors to tuple representations. #3", function () { - const [R, G, B, A] = rgbaStringToRgbaTuple("rgba(0, 0, 0, 0)"); - assert.equal(R, 0); - assert.equal(G, 0); - assert.equal(B, 0); - assert.equal(A, 0); - }); - - }); - - context("#rgbToHex()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - rgbToHex(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGB color", function () { - - try { - rgbToHex("rgb(true, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts RGB colors to hex colors. #1", function () { - assert.equal(rgbToHex("rgb(125, 244, 66)"), "#7df442"); - }); - - it("Converts RGB colors to hex colors. #2", function () { - assert.equal(rgbToHex("rgb(255, 255, 255)"), "#ffffff"); - }); - - it("Converts RGB colors to hex colors. #3", function () { - assert.equal(rgbToHex("rgb(0, 0, 0)"), "#000000"); - }); - - it("Converts RGB colors to hex colors. #4", function () { - assert.equal(rgbToHex("rgb(39, 109, 65)"), "#276d41"); - }); - - }); - - context("#rgbaToHex()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - rgbaToHex(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGBa color", function () { - - try { - rgbaToHex("rgba(true, false, false, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the second argument is given but is not a string", function () { - - try { - rgbaToHex("rgba(10, 50, 80, 1)", false); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Converts RGBa colors to hex colors. #1", function () { - assert.equal(rgbaToHex("rgba(125, 244, 66, 1)"), "#7df442"); - }); - - it("Converts RGBa colors to hex colors. #2", function () { - assert.equal(rgbaToHex("rgba(255, 255, 255, 0.5)"), "#ffffff"); - }); - - it("Converts RGBa colors to hex colors. #3", function () { - assert.equal(rgbaToHex("rgba(0, 0, 0, 1)"), "#000000"); - }); - - it("Converts RGBa colors to hex colors. #4", function () { - assert.equal(rgbaToHex("rgba(39, 109, 65, 0.8)"), "#276d41"); - }); - - it("Converts RGBa colors to hex colors including interpolation of the alpha channel. #1", function () { - assert.equal(rgbaToHex("rgba(255, 255, 255, 0)", "white"), "#ffffff"); - }); - - it("Converts RGBa colors to hex colors including interpolation of the alpha channel. #2", function () { - assert.equal(rgbaToHex("rgba(255, 255, 255, 0)", "black"), "#000000"); - }); - - }); - - context("#hsvStringToHsvTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hsvStringToHsvTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSV/HSB color. #1", function () { - - try { - hsvStringToHsvTuple("hsb(50, 100, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSV/HSB color. #2", function () { - - try { - hsvStringToHsvTuple("hsv(50, 100, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - - it("Generates tuple representations of HSV/HSB colors from HSV/HSB strings. #1", function () { - const [H, S, V] = hsvStringToHsvTuple("hsb(1, 2, 3)"); - assert.equal(H, 1); - assert.equal(S, 2); - assert.equal(V, 3); - }); - - it("Generates tuple representations of HSV/HSB colors from HSV/HSB strings. #2", function () { - const [H, S, V] = hsvStringToHsvTuple("hsv(0, 0, 0)"); - assert.equal(H, 0); - assert.equal(S, 0); - assert.equal(V, 0); - }); - - it("Generates tuple representations of HSV/HSB colors from HSV/HSB strings. #3", function () { - const [H, S, V] = hsvStringToHsvTuple("hsb(360, 100, 100)"); - assert.equal(H, 360); - assert.equal(S, 100); - assert.equal(V, 100); - }); - - }); - - context("#hsvToRgbTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hsvToRgbTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSV/HSB color. #1", function () { - - try { - hsvToRgbTuple("hsb(50, 100, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSV/HSB color. #2", function () { - - try { - hsvToRgbTuple("hsv(50, 100, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates tuple representations of RGB colors from HSV/HSB strings. #1", function () { - const [R, G, B] = hsvToRgbTuple("hsv(10, 5, 50)"); - assert.equal(R, 128); - assert.equal(G, 122); - assert.equal(B, 121); - }); - - it("Generates tuple representations of RGB colors from HSV/HSB strings. #2", function () { - const [R, G, B] = hsvToRgbTuple("hsv(125, 36, 11)"); - assert.equal(R, 18); - assert.equal(G, 28); - assert.equal(B, 19); - }); - - it("Generates tuple representations of RGB colors from HSV/HSB strings. #3", function () { - const [R, G, B] = hsvToRgbTuple("hsv(359, 0, 0)"); - assert.equal(R, 0); - assert.equal(G, 0); - assert.equal(B, 0); - }); - - }); - - context("#hsvToRgb()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hsvToRgb(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSV/HSB color. #1", function () { - - try { - hsvToRgb("hsb(50, 100, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSV/HSB color. #2", function () { - - try { - hsvToRgb("hsv(50, 100, false)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates RGB colors from HSV/HSB strings. #1", function () { - assert.equal(hsvToRgb("hsv(10, 5, 50)"), "rgb(128, 122, 121)"); - }); - - it("Generates RGB colors from HSV/HSB strings. #2", function () { - assert.equal(hsvToRgb("hsv(125, 36, 11)"), "rgb(18, 28, 19)"); - }); - - it("Generates RGB colors from HSV/HSB strings. #3", function () { - assert.equal(hsvToRgb("hsv(359, 0, 0)"), "rgb(0, 0, 0)"); - }); - - }); - - context("#toHex()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - toHex(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #1", function () { - - try { - toHex("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #2", function () { - - try { - toHex("#1234"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #3", function () { - - try { - toHex("1234"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #4", function () { - - try { - toHex("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - }); - - it("Throws an exception if the first argument is not a proper color. #5", function () { - - try { - toHex("hsl(false, rgb, true)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("returns given hex codes just as they were if they start with a '#' and is otherwise valid. #1", function () { - assert.equal(toHex("#123"), "#123"); - }); - - it("returns given hex codes just as they were if they start with a '#' and is otherwise valid. #2", function () { - assert.equal(toHex("#ffffff"), "#ffffff"); - }); - - it("Adds a '#' in front of valid hex codes that lacks it. #1", function () { - assert.equal(toHex("ffffff"), "#ffffff"); - }); - - it("Adds a '#' in front of valid hex codes that lacks it. #2", function () { - assert.equal(toHex("123"), "#123"); - }); - - it("Converts HTML color names to their actual hex values. #1", function () { - assert.equal(toHex("white"), "#ffffff"); - }); - - it("Converts HTML color names to their actual hex values. #2", function () { - assert.equal(toHex("antiquewhite"), "#faebd7"); - }); - - it("Converts RGB colors to hex values. #1", function () { - assert.equal(toHex("rgb(0, 0, 0)"), "#000000"); - }); - - it("Converts RGB colors to hex values. #2", function () { - assert.equal(toHex("rgb(255, 255, 255)"), "#ffffff"); - }); - - it("Converts RGB colors to hex values. #3", function () { - assert.equal(toHex("rgb(88, 100, 119)"), "#586477"); - }); - - it("Converts HSL colors to hex values. #1", function () { - assert.equal(toHex("hsl(157, 100%, 50%)"), "#00ff9d"); - }); - - it("Converts HSL colors to hex values. #2", function () { - assert.equal(toHex("hsl(79, 100%, 16%)"), "#385200"); - }); - - it("Converts HSL colors to hex values. #3", function () { - assert.equal(toHex("hsl(229, 47%, 16%)"), "#161d3c"); - }); - - it("Converts HSV/HSB colors to hex values. #1", function () { - assert.equal(toHex("hsv(228, 75, 78)"), "#3250c7"); - }); - - it("Converts HSV/HSB colors to hex values. #2", function () { - assert.equal(toHex("hsv(272, 83, 51)"), "#501682"); - }); - - it("Converts HSV/HSB colors to hex values. #3", function () { - assert.equal(toHex("hsb(344, 100, 12)"), "#1f0008"); - }); - - }); - - context("#hexToRgbTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hexToRgbTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper hex color.", function () { - - try { - hexToRgbTuple("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates RGB tuples from hex strings. #1", function () { - const [R, G, B] = hexToRgbTuple("#000000"); - assert.equal(R, 0); - assert.equal(G, 0); - assert.equal(B, 0); - }); - - it("Generates RGB tuples from hex strings. #2", function () { - const [R, G, B] = hexToRgbTuple("ffffff"); - assert.equal(R, 255); - assert.equal(G, 255); - assert.equal(B, 255); - }); - - it("Generates RGB tuples from hex strings. #3", function () { - const [R, G, B] = hexToRgbTuple("4286f4"); - assert.equal(R, 66); - assert.equal(G, 134); - assert.equal(B, 244); - }); - - }); - - context("#hexToRgb()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hexToRgb(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper hex color.", function () { - - try { - hexToRgb("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates RGB colors from hex strings. #1", function () { - assert.equal(hexToRgb("#000000"), "rgb(0, 0, 0)"); - }); - - it("Generates RGB tuples from hex strings. #2", function () { - assert.equal(hexToRgb("ffffff"), "rgb(255, 255, 255)"); - }); - - it("Generates RGB tuples from hex strings. #3", function () { - assert.equal(hexToRgb("4286f4"), "rgb(66, 134, 244)"); - }); - - }); - - context("#toRgb()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - toRgb(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #1", function () { - - try { - toRgb("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #2", function () { - - try { - toRgb("#1234"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #3", function () { - - try { - toRgb("1234"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #4", function () { - - try { - toRgb("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - }); - - it("Throws an exception if the first argument is not a proper color. #5", function () { - - try { - toRgb("hsl(false, rgb, true)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("returns rgb colors just as they were. #1", function () { - assert.equal(toRgb("rgb(50, 60, 70)"), "rgb(50, 60, 70)"); - }); - - it("returns rgb colors just as they were. #2", function () { - assert.equal(toRgb("rgb(255, 255, 255)"), "rgb(255, 255, 255)"); - }); - - it("Converts hex colors to RGB colors. #1", function () { - assert.equal(toRgb("#000000"), "rgb(0, 0, 0)"); - }); - - it("Converts hex colors to RGB colors. #2", function () { - assert.equal(toRgb("ffffff"), "rgb(255, 255, 255)"); - }); - - it("Converts hex colors to RGB colors. #3", function () { - assert.equal(toRgb("4286f4"), "rgb(66, 134, 244)"); - }); - - it("Converts HTML color names to RGB values. #1", function () { - assert.equal(toRgb("antiquewhite"), "rgb(250, 235, 215)"); - }); - - it("Converts HTML color names to RGB values. #2", function () { - assert.equal(toRgb("aquamarine"), "rgb(127, 255, 212)"); - }); - - it("Converts HSL colors to RGB colors. #1", function () { - assert.equal(toRgb("hsl(96, 41%, 78%)"), "rgb(194, 222, 176)"); - }); - - it("Converts HSL colors to RGB colors. #2", function () { - assert.equal(toRgb("hsl(29, 87%, 50%)"), "rgb(238, 124, 17)"); - }); - - it("Converts HSL colors to RGB colors. #3", function () { - assert.equal(toRgb("hsl(0, 87%, 0%)"), "rgb(0, 0, 0)"); - }); - - it("Converts HSV/HSB colors to RGB colors. #1", function () { - assert.equal(toRgb("hsv(162, 68, 13)"), "rgb(11, 33, 26)"); - }); - - it("Converts HSV/HSB colors to RGB colors. #2", function () { - assert.equal(toRgb("hsv(245, 35, 67)"), "rgb(116, 111, 171)"); - }); - - it("Converts HSV/HSB colors to RGB colors. #3", function () { - assert.equal(toRgb("hsb(259, 0, 99)"), "rgb(252, 252, 252)"); - }); - - }); - - context("#hexToHslTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hexToHslTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper hex color.", function () { - - try { - hexToHslTuple("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates HSL tuples from hex strings. #1", function () { - const [H, S, L] = hexToHslTuple("#87eeca"); - assert.equal(H, 159); - assert.equal(S, "75%"); - assert.equal(L, "73%"); - }); - - it("Generates HSL tuples from hex strings. #2", function () { - const [H, S, L] = hexToHslTuple("#6e64a0"); - assert.equal(H, 250); - assert.equal(S, "24%"); - assert.equal(L, "51%"); - }); - - it("Generates HSL tuples from hex strings. #3", function () { - const [H, S, L] = hexToHslTuple("#1e681d"); - assert.equal(H, 119); - assert.equal(S, "56%"); - assert.equal(L, "26%"); - }); - - }); - - context("#hexToHslaTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hexToHslaTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper hex color.", function () { - - try { - hexToHslaTuple("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the second argument is not a number.", function () { - - try { - hexToHslaTuple("#000000", false); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates HSLa tuples from hex strings. #1", function () { - const [H, S, L, A] = hexToHslaTuple("#87eeca"); - assert.equal(H, 159); - assert.equal(S, "75%"); - assert.equal(L, "73%"); - assert.equal(A, 1); - }); - - it("Generates HSLa tuples from hex strings. #2", function () { - const [H, S, L, A] = hexToHslaTuple("#6e64a0", 0.5); - assert.equal(H, 250); - assert.equal(S, "24%"); - assert.equal(L, "51%"); - assert.equal(A, 0.5); - }); - - it("Generates HSLa tuples from hex strings. #3", function () { - const [H, S, L, A] = hexToHslaTuple("#1e681d", 0); - assert.equal(H, 119); - assert.equal(S, "56%"); - assert.equal(L, "26%"); - assert.equal(A, 0); - }); - - }); - - context("#hexToHsla()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hexToHsla(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper hex color.", function () { - - try { - hexToHsla("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the second argument is not a number.", function () { - - try { - hexToHsla("#000000", false); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates HSLa colors from hex strings. #1", function () { - assert.equal(hexToHsla("#87eeca"), "hsla(159, 75%, 73%, 1)"); - }); - - it("Generates HSLa colors from hex strings. #2", function () { - assert.equal(hexToHsla("#6e64a0", 0.5), "hsla(250, 24%, 51%, 0.5)"); - }); - - it("Generates HSLa tuples from hex strings. #3", function () { - assert.equal(hexToHsla("#1e681d", 0), "hsla(119, 56%, 26%, 0)"); - }); - - }); - - context("#hexToHsl()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - hexToHsl(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper hex color.", function () { - - try { - hexToHsl("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates HSL colors from hex strings. #1", function () { - assert.equal(hexToHsl("#87eeca"), "hsl(159, 75%, 73%)"); - }); - - it("Generates HSL colors from hex strings. #2", function () { - assert.equal(hexToHsl("#6e64a0"), "hsl(250, 24%, 51%)"); - }); - - it("Generates HSL colors from hex strings. #3", function () { - assert.equal(hexToHsl("#1e681d"), "hsl(119, 56%, 26%)"); - }); - - }); - - context("#rgbToHslTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - rgbToHslTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGB color.", function () { - - try { - rgbToHslTuple("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates HSL tuples from RGB colors. #1", function () { - const [H, S, L] = rgbToHslTuple("rgb(24, 102, 20)"); - assert.equal(H, 117); - assert.equal(S, "67%"); - assert.equal(L, "24%"); - - }); - - it("Generates HSL tuples from RGB colors. #2", function () { - const [H, S, L] = rgbToHslTuple("rgb(81, 122, 179)"); - assert.equal(H, 215); - assert.equal(S, "39%"); - assert.equal(L, "51%"); - }); - - it("Generates HSL tuples from RGB colors. #3", function () { - const [H, S, L] = rgbToHslTuple("rgb(184, 0, 3)"); - assert.equal(H, 359); - assert.equal(S, "100%"); - assert.equal(L, "36%"); - }); - - }); - - context("#rgbToHsl()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - rgbToHsl(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGB color.", function () { - - try { - rgbToHsl("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates HSL colors from RGB colors. #1", function () { - assert.equal(rgbToHsl("rgb(24, 102, 20)"), "hsl(117, 67%, 24%)"); - }); - - it("Generates HSL tuples from RGB colors. #2", function () { - assert.equal(rgbToHsl("rgb(81, 122, 179)"), "hsl(215, 39%, 51%)"); - }); - - it("Generates HSL tuples from RGB colors. #3", function () { - assert.equal(rgbToHsl("rgb(184, 0, 3)"), "hsl(359, 100%, 36%)"); - }); - - }); - - context("#toHsl()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - toHsl(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #1", function () { - - try { - toHsl("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #2", function () { - - try { - toHsl("#1234"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #3", function () { - - try { - toHsl("1234"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #4", function () { - - try { - toHsl("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - }); - - it("Throws an exception if the first argument is not a proper color. #5", function () { - - try { - toHsl("hsl(false, rgb, true)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("returns correct HSL colors just as they were.", function () { - assert.equal(toHsl("hsl(50, 60%, 70%)"), "hsl(50, 60%, 70%)"); - }); - - it("returns HSL with missing '%' symbols with the addition of those..", function () { - assert.equal(toHsl("hsl(50, 60, 70)"), "hsl(50, 60%, 70%)"); - }); - - it("Converts RGB colors to HSL colors. #1", function () { - assert.equal(toHsl("rgb(30, 104, 29)"), "hsl(119, 56%, 26%)"); - }); - - it("Converts RGB colors to HSL colors. #2", function () { - assert.equal(toHsl("rgb(65, 58, 95)"), "hsl(251, 24%, 30%)"); - }); - - it("Converts RGB colors to HSL colors. #3", function () { - assert.equal(toHsl("rgb(201, 243, 205)"), "hsl(126, 64%, 87%)"); - }); - - it("Converts hex colors to HSL colors. #1", function () { - assert.equal(toHsl("#000000"), "hsl(0, 0%, 0%)"); - }); - - it("Converts hex colors to HSL colors. #2", function () { - assert.equal(toHsl("#ffffff"), "hsl(0, 0%, 100%)"); - }); - - it("Converts hex colors to HSL colors. #3", function () { - assert.equal(toHsl("ff4d4d"), "hsl(0, 100%, 65%)"); - }); - - it("Converts HTML color names to HSL values. #1", function () { - assert.equal(toHsl("antiquewhite"), "hsl(34, 78%, 91%)"); - }); - - it("Converts HTML color names to HSL values. #2", function () { - assert.equal(toHsl("aquamarine"), "hsl(160, 100%, 75%)"); - }); - - it("Converts HSV/HSB colors to HSL colors. #1", function () { - assert.equal(toHsl("hsv(162, 68, 13)"), "hsl(161, 50%, 9%)"); - }); - - it("Converts HSV/HSB colors to HSL colors. #2", function () { - assert.equal(toHsl("hsv(245, 35, 67)"), "hsl(245, 26%, 55%)"); - }); - - it("Converts HSV/HSB colors to RGB colors. #3", function () { - assert.equal(toHsl("hsb(259, 0, 99)"), "hsl(0, 0%, 99%)"); - }); - - }); - - context("#rgbToHsvTuple()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - rgbToHsvTuple(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGB color.", function () { - - try { - rgbToHsvTuple("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates HSV/HSB tuples from RGB colors. #1", function () { - const [H, S, V] = rgbToHsvTuple("rgb(24, 102, 20)"); - assert.equal(H, 117); - assert.equal(S, 80); - assert.equal(V, 40); - - }); - - it("Generates HSV/HSB tuples from RGB colors. #2", function () { - const [H, S, V] = rgbToHsvTuple("rgb(81, 122, 179)"); - assert.equal(H, 215); - assert.equal(S, 55); - assert.equal(V, 70); - }); - - it("Generates HSV/HSB tuples from RGB colors. #3", function () { - const [H, S, V] = rgbToHsvTuple("rgb(184, 0, 3)"); - assert.equal(H, 359); - assert.equal(S, 100); - assert.equal(V, 72); - }); - - }); - - context("#rgbToHsv()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - rgbToHsv(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGB color.", function () { - - try { - rgbToHsv("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Generates HSV/HSB colors from RGB colors. #1", function () { - assert.equal(rgbToHsv("rgb(24, 102, 20)"), "hsb(117, 80, 40)"); - }); - - it("Generates HSV/HSB colors from RGB colors. #2", function () { - assert.equal(rgbToHsv("rgb(81, 122, 179)"), "hsb(215, 55, 70)"); - }); - - it("Generates HSV/HSB colors from RGB colors. #3", function () { - assert.equal(rgbToHsv("rgb(184, 0, 3)"), "hsb(359, 100, 72)"); - }); - - }); - - context("#toHsv()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - toHsv(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #1", function () { - - try { - toHsv("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #2", function () { - - try { - toHsv("#1234"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #3", function () { - - try { - toHsv("1234"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #4", function () { - - try { - toHsv("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - }); - - it("Throws an exception if the first argument is not a proper color. #5", function () { - - try { - toHsv("hsl(false, rgb, true)"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #6", function () { - - try { - toHsv("hsb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper color. #7", function () { - - try { - toHsv("hsv()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("returns correct HSV/HSB colors just as they were. #1", function () { - assert.equal(toHsv("hsv(50, 60, 70)"), "hsv(50, 60, 70)"); - }); - - it("returns correct HSV/HSB colors just as they were. #2", function () { - assert.equal(toHsv("hsb(50, 60, 70)"), "hsb(50, 60, 70)"); - }); - - it("Converts RGB colors to HSV/HSB colors. #1", function () { - assert.equal(toHsv("rgb(30, 104, 29)"), "hsb(119, 72, 41)"); - }); - - it("Converts RGB colors to HSV/HSB colors. #2", function () { - assert.equal(toHsv("rgb(65, 58, 95)"), "hsb(251, 39, 37)"); - }); - - it("Converts RGB colors to HSV/HSB colors. #3", function () { - assert.equal(toHsv("rgb(201, 243, 205)"), "hsb(126, 17, 95)"); - }); - - it("Converts hex colors to HSV/HSB colors. #1", function () { - assert.equal(toHsv("#000000"), "hsb(0, 0, 0)"); - }); - - it("Converts hex colors to HSV/HSB colors. #2", function () { - assert.equal(toHsv("#ffffff"), "hsb(0, 0, 100)"); - }); - - it("Converts hex colors to HSV/HSB colors. #3", function () { - assert.equal(toHsv("ff4d4d"), "hsb(0, 70, 100)"); - }); - - it("Converts HTML color names to HSV/HSB values. #1", function () { - assert.equal(toHsv("antiquewhite"), "hsb(34, 14, 98)"); - }); - - it("Converts HTML color names to HSV/HSB values. #2", function () { - assert.equal(toHsv("aquamarine"), "hsb(160, 50, 100)"); - }); - - it("Converts HSL colors to HSV/HSB colors. #1", function () { - assert.equal(toHsv("hsl(96, 41%, 78%)"), "hsb(97, 21, 87)"); - }); - - it("Converts HSL colors to HSV/HSB colors. #2", function () { - assert.equal(toHsv("hsl(29, 87%, 50%)"), "hsb(29, 93, 93)"); - }); - - it("Converts HSL colors to HSV/HSB colors. #3", function () { - assert.equal(toHsv("hsl(0, 87%, 0%)"), "hsb(0, 0, 0)"); - }); - - }); - - context("#randomHexColor()", function () { - - it("always returns random valid hex colors", function () { - - // 5000 times should be more than enough! - let limit = 5000; - for (let i = 0; i < limit; i++) { - let hex = randomHexColor(); - if (!(/^#[0-9A-F]{6}$/i.test(hex))) assert.fail(null, null, `invalid hex color generated: ${hex}`); - } - }); - - }); - - context("#randomRgbColor()", function () { - - it("always returns random valid RGB colors", function () { - - // 5000 times should be more than enough! - let limit = 5000; - for (let i = 0; i < limit; i++) { - let rgb = randomRgbColor(); - if (!(/^rgb\(\s*.+\s*,\s*.+\s*,\s*.+\s*\)$/.test(rgb))) assert.fail(null, null, `invalid RGB color generated: ${rgb}`); - } - }); - - }); - - context("#randomHslColor()", function () { - - it("always returns random valid HSL colors", function () { - - // 5000 times should be more than enough! - let limit = 5000; - for (let i = 0; i < limit; i++) { - let hsl = randomHslColor(); - if (!(/^hsl\(\s*.+\s*,\s*.+%\s*,\s*.+%\s*\)$/.test(hsl))) assert.fail(null, null, `invalid HSL color generated: ${hsl}`); - } - }); - - }); - - context("#randomHsvColor()", function () { - - it("always returns random valid HSV/HSB colors", function () { - - // 5000 times should be more than enough! - let limit = 5000; - for (let i = 0; i < limit; i++) { - let hsv = randomHsvColor(); - if (!(/^hsb\(\s*.+\s*,\s*.+\s*,\s*.+\s*\)$/.test(hsv))) assert.fail(null, null, `invalid HSV/HSB color generated: ${hsv}`); - } - }); - - }); - - context("#saturateHsl()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - saturateHsl(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSL color.", function () { - - try { - saturateHsl("hsl()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a saturated version of the given HSL color. #1", function () { - assert.equal(saturateHsl("hsl(50, 50, 80)"), "hsl(50, 55%, 80%)"); - }); - - it("Returns a saturated version of the given HSL color. #2", function () { - assert.equal(saturateHsl("hsl(50, 50, 80)", 10), "hsl(50, 55%, 80%)"); - }); - - it("Returns a saturated version of the given HSL color. #3", function () { - assert.equal(saturateHsl("hsl(50, 50, 80)", 100), "hsl(50, 100%, 80%)"); - }); - - it("Returns a less saturated version of the given HSL color for negative values. #1", function () { - assert.equal(saturateHsl("hsl(50, 50, 80)", -10), "hsl(50, 45%, 80%)"); - }); - - it("Returns a less saturated version of the given HSL color for negative values. #2", function () { - assert.equal(saturateHsl("hsl(50, 50, 80)", -95), "hsl(50, 2.5%, 80%)"); - }); - - it("Never goes beyond 100%", function () { - assert.equal(saturateHsl("hsl(50, 50, 80)", 100000), "hsl(50, 100%, 80%)"); - }); - - it("Never goes below 0%", function () { - assert.equal(saturateHsl("hsl(50, 50, 80)", -100000), "hsl(50, 0%, 80%)"); - }); - - }); - - context("#saturateHsla()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - saturateHsla(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSLa color.", function () { - - try { - saturateHsla("hsla()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a saturated version of the given HSLa color. #1", function () { - assert.equal(saturateHsla("hsla(50, 50, 80, 1)"), "hsla(50, 55%, 80%, 1)"); - }); - - it("Returns a saturated version of the given HSLa color. #2", function () { - assert.equal(saturateHsla("hsla(50, 50, 80, 0.5)", 10), "hsla(50, 55%, 80%, 0.5)"); - }); - - it("Returns a saturated version of the given HSLa color. #3", function () { - assert.equal(saturateHsla("hsla(50, 50, 80, 0)", 100), "hsla(50, 100%, 80%, 0)"); - }); - - it("Returns a less saturated version of the given HSLa color for negative values. #1", function () { - assert.equal(saturateHsla("hsla(50, 50, 80, 1)", -10), "hsla(50, 45%, 80%, 1)"); - }); - - it("Returns a less saturated version of the given HSLa color for negative values. #2", function () { - assert.equal(saturateHsla("hsla(50, 50, 80, 0.6)", -95), "hsla(50, 2.5%, 80%, 0.6)"); - }); - - it("Never goes beyond 100%", function () { - assert.equal(saturateHsla("hsla(50, 50, 80, 1)", 100000), "hsla(50, 100%, 80%, 1)"); - }); - - it("Never goes below 0%", function () { - assert.equal(saturateHsla("hsla(50, 50, 80, 0.2)", -100000), "hsla(50, 0%, 80%, 0.2)"); - }); - - }); - - context("#saturateRgb()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - saturateRgb(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGB color.", function () { - - try { - saturateRgb("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a saturated version of the given RGB color. #1", function () { - assert.equal(saturateRgb("rgb(23, 30, 129)"), "rgb(18, 25, 135)"); - }); - - it("Returns a saturated version of the given RGB color. #2", function () { - assert.equal(saturateRgb("rgb(77, 147, 210)", 10), "rgb(69, 148, 217)"); - }); - - it("Returns a less saturated version of the given RGB color for negative values. #1", function () { - assert.equal(saturateRgb("rgb(77, 147, 210)", -10), "rgb(82, 147, 203)"); - }); - - it("Returns a less saturated version of the given RGB color for negative values. #2", function () { - assert.equal(saturateRgb("rgb(23, 30, 129)", -90), "rgb(71, 72, 82)"); - }); - - }); - - context("#saturateRgba()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - saturateRgba(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGBa color.", function () { - - try { - saturateRgba("rgba()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a saturated version of the given RGBa color. #1", function () { - assert.equal(saturateRgba("rgba(23, 30, 129, 1)"), "rgba(18, 25, 135, 1)"); - }); - - it("Returns a saturated version of the given RGBa color. #2", function () { - assert.equal(saturateRgba("rgba(77, 147, 210, 0.5)", 10), "rgba(69, 148, 217, 0.5)"); - }); - - it("Returns a less saturated version of the given RGBa color for negative values. #1", function () { - assert.equal(saturateRgba("rgba(77, 147, 210, 0)", -10), "rgba(82, 147, 203, 0)"); - }); - - it("Returns a less saturated version of the given RGBa color for negative values. #2", function () { - assert.equal(saturateRgba("rgba(23, 30, 129, 0.2)", -90), "rgba(71, 72, 82, 0.2)"); - }); - - }); - - context("#saturateHex()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - saturateHex(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper hex color.", function () { - - try { - saturateHex("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a saturated version of the given hex color. #1", function () { - assert.equal(saturateHex("#a3a3b3"), "#a2a2b4"); - }); - - it("Returns a saturated version of the given hex color. #2", function () { - assert.equal(saturateHex("#ddace3"), "#dfa8e6"); - }); - - it("Returns a less saturated version of the given hex color for negative values. #1", function () { - assert.equal(saturateHex("#a3a3b3", -10), "#a3a3b2"); - }); - - it("Returns a less saturated version of the given hex color for negative values. #2", function () { - assert.equal(saturateHex("#ddace3", -90), "#c9c4ca"); - }); - - }); - - context("#saturateHsv()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - saturateHsv(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSV/HSB color.", function () { - - try { - saturateHsv("hsb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a saturated version of the given HSV/HSB color. #1", function () { - assert.equal(saturateHsv("hsb(290, 10, 79)"), "hsb(290, 11, 79)"); - }); - - it("Returns a saturated version of the given HSV/HSB color. #2", function () { - assert.equal(saturateHsv("hsb(290, 10, 79)", 10), "hsb(290, 11, 79)"); - }); - - it("Returns a saturated version of the given HSV/HSB color. #3", function () { - assert.equal(saturateHsv("hsb(290, 10, 79)", 90), "hsb(290, 19, 79)"); - }); - - it("Returns a less saturated version of the given HSV/HSB color for negative values. #1", function () { - assert.equal(saturateHsv("hsb(120, 40, 80)", -10), "hsb(120, 36, 80)"); - }); - - it("Returns a less saturated version of the given HSV/HSB color for negative values. #2", function () { - assert.equal(saturateHsv("hsb(120, 40, 80)", -90), "hsb(120, 4, 80)"); - }); - - }); - - context("#saturate()", function () { - - it("Returns a saturated version of a given RGB color. #1", function () { - assert.equal(saturate("rgb(23, 30, 129)"), "rgb(18, 25, 135)"); - }); - - it("Returns a saturated version of a given RGB color. #2", function () { - assert.equal(saturate("rgb(77, 147, 210)", 10), "rgb(69, 148, 217)"); - }); - - it("Returns a less saturated version of a given RGB color for negative values. #1", function () { - assert.equal(saturate("rgb(77, 147, 210)", -10), "rgb(82, 147, 203)"); - }); - - it("Returns a less saturated version of a given RGB color for negative values. #2", function () { - assert.equal(saturate("rgb(23, 30, 129)", -90), "rgb(71, 72, 82)"); - }); - - it("Returns a saturated version of a given HSL color. #1", function () { - assert.equal(saturate("hsl(50, 50, 80)"), "hsl(50, 55%, 80%)"); - }); - - it("Returns a saturated version of a given HSL color. #2", function () { - assert.equal(saturate("hsl(50, 50, 80)", 10), "hsl(50, 55%, 80%)"); - }); - - it("Returns a saturated version of a given HSL color. #3", function () { - assert.equal(saturate("hsl(50, 50, 80)", 100), "hsl(50, 100%, 80%)"); - }); - - it("Returns a less saturated version of a given HSL color for negative values. #1", function () { - assert.equal(saturate("hsl(50, 50, 80)", -10), "hsl(50, 45%, 80%)"); - }); - - it("Returns a less saturated version of a given HSL color for negative values. #2", function () { - assert.equal(saturate("hsl(50, 50, 80)", -95), "hsl(50, 2.5%, 80%)"); - }); - - it("Never goes beyond 100% for HSL colors", function () { - assert.equal(saturate("hsl(50, 50, 80)", 100000), "hsl(50, 100%, 80%)"); - }); - - it("Never goes below 0% for HSL colors", function () { - assert.equal(saturate("hsl(50, 50, 80)", -100000), "hsl(50, 0%, 80%)"); - }); - - it("Returns a saturated version of a given HSV/HSB color. #1", function () { - assert.equal(saturate("hsb(290, 10, 79)"), "hsb(290, 11, 79)"); - }); - - it("Returns a saturated version of a given HSV/HSB color. #2", function () { - assert.equal(saturate("hsb(290, 10, 79)", 10), "hsb(290, 11, 79)"); - }); - - it("Returns a saturated version of a given HSV/HSB color. #3", function () { - assert.equal(saturate("hsb(290, 10, 79)", 90), "hsb(290, 19, 79)"); - }); - - it("Returns a less saturated version of a given HSV/HSB color for negative values. #1", function () { - assert.equal(saturate("hsb(120, 40, 80)", -10), "hsb(120, 36, 80)"); - }); - - it("Returns a less saturated version of a given HSV/HSB color for negative values. #2", function () { - assert.equal(saturate("hsb(120, 40, 80)", -90), "hsb(120, 4, 80)"); - }); - - it("Returns a saturated version of a given RGBa color. #1", function () { - assert.equal(saturate("rgba(23, 30, 129, 1)"), "rgba(18, 25, 135, 1)"); - }); - - it("Returns a saturated version of a given RGBa color. #2", function () { - assert.equal(saturate("rgba(77, 147, 210, 0.5)", 10), "rgba(69, 148, 217, 0.5)"); - }); - - it("Returns a less saturated version of a given RGBa color for negative values. #1", function () { - assert.equal(saturate("rgba(77, 147, 210, 0)", -10), "rgba(82, 147, 203, 0)"); - }); - - it("Returns a less saturated version of a given RGBa color for negative values. #2", function () { - assert.equal(saturate("rgba(23, 30, 129, 0.2)", -90), "rgba(71, 72, 82, 0.2)"); - }); - - it("Returns a saturated version of a given HSLa color. #1", function () { - assert.equal(saturate("hsla(50, 50, 80, 1)"), "hsla(50, 55%, 80%, 1)"); - }); - - it("Returns a saturated version of a given HSLa color. #2", function () { - assert.equal(saturate("hsla(50, 50, 80, 0.5)", 10), "hsla(50, 55%, 80%, 0.5)"); - }); - - it("Returns a saturated version of a given HSLa color. #3", function () { - assert.equal(saturate("hsla(50, 50, 80, 0)", 100), "hsla(50, 100%, 80%, 0)"); - }); - - it("Returns a less saturated version of a given HSLa color for negative values. #1", function () { - assert.equal(saturate("hsla(50, 50, 80, 1)", -10), "hsla(50, 45%, 80%, 1)"); - }); - - it("Returns a less saturated version of a given HSLa color for negative values. #2", function () { - assert.equal(saturate("hsla(50, 50, 80, 0.6)", -95), "hsla(50, 2.5%, 80%, 0.6)"); - }); - - it("Never goes beyond 100% for HSLa colors", function () { - assert.equal(saturate("hsla(50, 50, 80, 1)", 100000), "hsla(50, 100%, 80%, 1)"); - }); - - it("Never goes below 0% for HSLa colors", function () { - assert.equal(saturate("hsla(50, 50, 80, 0.2)", -100000), "hsla(50, 0%, 80%, 0.2)"); - }); - - it("Returns a saturated version of a given hex color. #1", function () { - assert.equal(saturate("#a3a3b3"), "#a2a2b4"); - }); - - it("Returns a saturated version of a given hex color. #2", function () { - assert.equal(saturate("#ddace3"), "#dfa8e6"); - }); - - it("Returns a less saturated version of a given hex color for negative values. #1", function () { - assert.equal(saturate("#a3a3b3", -10), "#a3a3b2"); - }); - - it("Returns a less saturated version of a given hex color for negative values. #2", function () { - assert.equal(saturate("#ddace3", -90), "#c9c4ca"); - }); - - }); - - context("#lightenHsl()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - lightenHsl(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSL color.", function () { - - try { - lightenHsl("hsl()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a lightened version of the given HSL color. #1", function () { - assert.equal(lightenHsl("hsl(50, 50, 80)"), "hsl(50, 50%, 88%)"); - }); - - it("Returns a lightened version of the given HSL color. #2", function () { - assert.equal(lightenHsl("hsl(50, 50, 80)", 10), "hsl(50, 50%, 88%)"); - }); - - it("Returns a lightened version of the given HSL color. #3", function () { - assert.equal(lightenHsl("hsl(50, 50, 10)", 100), "hsl(50, 50%, 20%)"); - }); - - it("Returns a less lightened version of the given HSL color for negative values. #1", function () { - assert.equal(lightenHsl("hsl(50, 50, 80)", -10), "hsl(50, 50%, 72%)"); - }); - - it("Returns a less lightened version of the given HSL color for negative values. #2", function () { - assert.equal(lightenHsl("hsl(50, 50, 80)", -95), "hsl(50, 50%, 4%)"); - }); - - it("Never goes beyond 100%", function () { - assert.equal(lightenHsl("hsl(50, 50, 80)", 100000), "hsl(50, 50%, 100%)"); - }); - - it("Never goes below 0%", function () { - assert.equal(lightenHsl("hsl(50, 50, 80)", -100000), "hsl(50, 50%, 0%)"); - }); - - }); - - context("#lightenHsla()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - lightenHsla(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSLa color.", function () { - - try { - lightenHsla("hsla()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a lightened version of the given HSLa color. #1", function () { - assert.equal(lightenHsla("hsla(50, 50, 80, 1)"), "hsla(50, 50%, 88%, 1)"); - }); - - it("Returns a lightened version of the given HSLa color. #2", function () { - assert.equal(lightenHsla("hsla(50, 50, 80, 0.5)", 10), "hsla(50, 50%, 88%, 0.5)"); - }); - - it("Returns a lightened version of the given HSLa color. #3", function () { - assert.equal(lightenHsla("hsla(50, 50, 10, 0)", 100), "hsla(50, 50%, 20%, 0)"); - }); - - it("Returns a less lightened version of the given HSLa color for negative values. #1", function () { - assert.equal(lightenHsla("hsla(50, 50, 80, 1)", -10), "hsla(50, 50%, 72%, 1)"); - }); - - it("Returns a less lightened version of the given HSLa color for negative values. #2", function () { - assert.equal(lightenHsla("hsla(50, 50, 80, 0.6)", -95), "hsla(50, 50%, 4%, 0.6)"); - }); - - it("Never goes beyond 100%", function () { - assert.equal(lightenHsla("hsla(50, 50, 80, 1)", 100000), "hsla(50, 50%, 100%, 1)"); - }); - - it("Never goes below 0%", function () { - assert.equal(lightenHsla("hsla(50, 50, 80, 0.2)", -100000), "hsla(50, 50%, 0%, 0.2)"); - }); - - }); - - context("#lightenRgb()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - lightenRgb(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGB color.", function () { - - try { - lightenRgb("rgb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a lightened version of the given RGB color. #1", function () { - assert.equal(lightenRgb("rgb(23, 30, 129)"), "rgb(25, 33, 143)"); - }); - - it("Returns a lightened version of the given RGB color. #2", function () { - assert.equal(lightenRgb("rgb(231, 175, 212)", 10), "rgb(241, 208, 230)"); - }); - - it("Returns a less lightened version of the given RGB color for negative values. #1", function () { - assert.equal(lightenRgb("rgb(231, 175, 212)", -10), "rgb(222, 145, 196)"); - }); - - it("Returns a less lightened version of the given RGB color for negative values. #2", function () { - assert.equal(lightenRgb("rgb(23, 30, 129)", -90), "rgb(2, 3, 13)"); - }); - - }); - - context("#lightenRgba()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - lightenRgba(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper RGBa color.", function () { - - try { - lightenRgba("rgba()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a lightened version of the given RGBa color. #1", function () { - assert.equal(lightenRgba("rgba(23, 30, 129, 1)"), "rgba(25, 33, 143, 1)"); - }); - - it("Returns a lightened version of the given RGBa color. #2", function () { - assert.equal(lightenRgba("rgba(231, 175, 212, 0.5)", 10), "rgba(241, 208, 230, 0.5)"); - }); - - it("Returns a less lightened version of the given RGBa color for negative values. #1", function () { - assert.equal(lightenRgba("rgba(231, 175, 212, 0)", -10), "rgba(222, 145, 196, 0)"); - }); - - it("Returns a less lightened version of the given RGBa color for negative values. #2", function () { - assert.equal(lightenRgba("rgba(23, 30, 129, 0.2)", -90), "rgba(2, 3, 13, 0.2)"); - }); - - }); - - context("#lightenHex()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - lightenHex(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper hex color.", function () { - - try { - lightenHex("foobar"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a lightened version of the given hex color. #1", function () { - assert.equal(lightenHex("#af0"), "#b2ff1a"); - }); - - it("Returns a lightened version of the given hex color. #2", function () { - assert.equal(lightenHex("#ddaced"), "#eacdf4"); - }); - - it("Returns a less lightened version of the given hex color for negative values. #1", function () { - assert.equal(lightenHex("#af0", -10), "#99e600"); - }); - - it("Returns a less lightened version of the given hex color for negative values. #2", function () { - assert.equal(lightenHex("#ddaced", -90), "#1b0721"); - }); - - }); - - context("#lightenHsv()", function () { - - it("Throws an exception if the first argument is not of type 'string'", function () { - - try { - lightenHsv(null); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Throws an exception if the first argument is not a proper HSV/HSB color.", function () { - - try { - lightenHsv("hsb()"); - throw new Error("Expected an exception to be thrown."); - } catch (e) { - if (e.message.includes("Expected an exception to be thrown.")) assert.fail(null, null, "Expected an exception to be thrown."); - } - - }); - - it("Returns a lightened version of the given HSV/HSB color. #1", function () { - assert.equal(lightenHsv("hsb(290, 10, 84)"), "hsb(291, 6, 91)"); - }); - - it("Returns a lightened version of the given HSV/HSB color. #2", function () { - assert.equal(lightenHsv("hsb(359, 12, 53)", 10), "hsb(0, 10, 58)"); - }); - - it("Returns a lightened version of the given HSV/HSB color. #3", function () { - assert.equal(lightenHsv("hsb(290, 10, 32)", 90), "hsb(286, 9, 60)"); - }); - - it("Returns a less lightened version of the given HSV/HSB color for negative values. #1", function () { - assert.equal(lightenHsv("hsb(290, 10, 84)", -10), "hsb(292, 15, 78)"); - }); - - it("Returns a less lightened version of the given HSV/HSB color for negative values. #2", function () { - assert.equal(lightenHsv("hsb(359, 12, 53)", -90), "hsb(0, 14, 5)"); - }); - - }); - - context("#lighten()", function () { - - it("Returns a lightened version of a given RGBa color. #1", function () { - assert.equal(lighten("rgba(23, 30, 129, 1)"), "rgba(25, 33, 143, 1)"); - }); - - it("Returns a lightened version of a given RGBa color. #2", function () { - assert.equal(lighten("rgba(231, 175, 212, 0.5)", 10), "rgba(241, 208, 230, 0.5)"); - }); - - it("Returns a less lightened version of a given RGBa color for negative values. #1", function () { - assert.equal(lighten("rgba(231, 175, 212, 0)", -10), "rgba(222, 145, 196, 0)"); - }); - - it("Returns a less lightened version of a given RGBa color for negative values. #2", function () { - assert.equal(lighten("rgba(23, 30, 129, 0.2)", -90), "rgba(2, 3, 13, 0.2)"); - }); - - it("Returns a lightened version of a given HSLa color. #1", function () { - assert.equal(lighten("hsla(50, 50, 80, 1)"), "hsla(50, 50%, 88%, 1)"); - }); - - it("Returns a lightened version of a given HSLa color. #2", function () { - assert.equal(lighten("hsla(50, 50, 80, 0.5)", 10), "hsla(50, 50%, 88%, 0.5)"); - }); - - it("Returns a lightened version of a given HSLa color. #3", function () { - assert.equal(lighten("hsla(50, 50, 10, 0)", 100), "hsla(50, 50%, 20%, 0)"); - }); - - it("Returns a less lightened version of a given HSLa color for negative values. #1", function () { - assert.equal(lighten("hsla(50, 50, 80, 1)", -10), "hsla(50, 50%, 72%, 1)"); - }); - - it("Returns a less lightened version of a given HSLa color for negative values. #2", function () { - assert.equal(lighten("hsla(50, 50, 80, 0.6)", -95), "hsla(50, 50%, 4%, 0.6)"); - }); - - it("Never goes beyond 100% for HSLa values", function () { - assert.equal(lighten("hsla(50, 50, 80, 1)", 100000), "hsla(50, 50%, 100%, 1)"); - }); - - it("Never goes below 0% for HSLa values", function () { - assert.equal(lighten("hsla(50, 50, 80, 0.2)", -100000), "hsla(50, 50%, 0%, 0.2)"); - }); - - it("Returns a lightened version of a given RGB color. #1", function () { - assert.equal(lighten("rgb(23, 30, 129)"), "rgb(25, 33, 143)"); - }); - - it("Returns a lightened version of a given RGB color. #2", function () { - assert.equal(lighten("rgb(231, 175, 212)", 10), "rgb(241, 208, 230)"); - }); - - it("Returns a less lightened version of a given RGB color for negative values. #1", function () { - assert.equal(lighten("rgb(231, 175, 212)", -10), "rgb(222, 145, 196)"); - }); - - it("Returns a less lightened version of a given RGB color for negative values. #2", function () { - assert.equal(lighten("rgb(23, 30, 129)", -90), "rgb(2, 3, 13)"); - }); - - it("Returns a lightened version of a given HSL color. #1", function () { - assert.equal(lighten("hsl(50, 50, 80)"), "hsl(50, 50%, 88%)"); - }); - - it("Returns a lightened version of a given HSL color. #2", function () { - assert.equal(lighten("hsl(50, 50, 80)", 10), "hsl(50, 50%, 88%)"); - }); - - it("Returns a lightened version of a given HSL color. #3", function () { - assert.equal(lighten("hsl(50, 50, 10)", 100), "hsl(50, 50%, 20%)"); - }); - - it("Returns a less lightened version of a given HSL color for negative values. #1", function () { - assert.equal(lighten("hsl(50, 50, 80)", -10), "hsl(50, 50%, 72%)"); - }); - - it("Returns a less lightened version of a given HSL color for negative values. #2", function () { - assert.equal(lighten("hsl(50, 50, 80)", -95), "hsl(50, 50%, 4%)"); - }); - - it("Never goes beyond 100% for HSL values", function () { - assert.equal(lighten("hsl(50, 50, 80)", 100000), "hsl(50, 50%, 100%)"); - }); - - it("Never goes below 0% for HSL values", function () { - assert.equal(lighten("hsl(50, 50, 80)", -100000), "hsl(50, 50%, 0%)"); - }); - - it("Returns a lightened version of a given HSV/HSB color. #1", function () { - assert.equal(lighten("hsb(290, 10, 84)"), "hsb(291, 6, 91)"); - }); - - it("Returns a lightened version of a given HSV/HSB color. #2", function () { - assert.equal(lighten("hsb(359, 12, 53)", 10), "hsb(0, 10, 58)"); - }); - - it("Returns a lightened version of a given HSV/HSB color. #3", function () { - assert.equal(lighten("hsb(290, 10, 32)", 90), "hsb(286, 9, 60)"); - }); - - it("Returns a less lightened version of a given HSV/HSB color for negative values. #1", function () { - assert.equal(lighten("hsb(290, 10, 84)", -10), "hsb(292, 15, 78)"); - }); - - it("Returns a less lightened version of a given HSV/HSB color for negative values. #2", function () { - assert.equal(lighten("hsb(359, 12, 53)", -90), "hsb(0, 14, 5)"); - }); - - it("Returns a lightened version of a given hex color. #1", function () { - assert.equal(lighten("#af0"), "#b2ff1a"); - }); - - it("Returns a lightened version of a given hex color. #2", function () { - assert.equal(lighten("#ddaced"), "#eacdf4"); - }); - - it("Returns a less lightened version of a given hex color for negative values. #1", function () { - assert.equal(lighten("#af0", -10), "#99e600"); - }); - - it("Returns a less lightened version of a given hex color for negative values. #2", function () { - assert.equal(lighten("#ddaced", -90), "#1b0721"); - }); - - }); - - context("#isLight()", function () { - - it("correctly determines bright from dark colors, no matter the input. #1", function () { - assert.isTrue(isLight("#4286f4")); - }); - - it("correctly determines bright from dark colors, no matter the input. #2", function () { - assert.isTrue(isLight("#165ac9")); - }); - - it("correctly determines bright from dark colors, no matter the input. #3", function () { - assert.isFalse(isLight("#0c3e8e")); - }); - - it("correctly determines bright from dark colors, no matter the input. #4", function () { - assert.isTrue(isLight("rgb(159, 188, 234)")); - }); - - it("correctly determines bright from dark colors, no matter the input. #5", function () { - assert.isTrue(isLight("rgb(124, 134, 150)")); - }); - - it("correctly determines bright from dark colors, no matter the input. #6", function () { - assert.isFalse(isLight("rgb(85, 92, 104)")); - }); - - it("correctly determines bright from dark colors, no matter the input. #7", function () { - assert.isTrue(isLight("hsl(0, 0%, 100%)")); - }); - - it("correctly determines bright from dark colors, no matter the input. #8", function () { - assert.isTrue(isLight("hsl(300, 100%, 50%)")); - }); - - it("correctly determines bright from dark colors, no matter the input. #9", function () { - assert.isFalse(isLight("hsl(40, 100%, 30%)")); - }); - - it("correctly determines bright from dark colors, no matter the input. #10", function () { - assert.isTrue(isLight("darkseagreen")); - }); - - it("correctly determines bright from dark colors, no matter the input. #11", function () { - assert.isTrue(isLight("floralwhite")); - }); - - it("correctly determines bright from dark colors, no matter the input. #12", function () { - assert.isFalse(isLight("darkslateblue")); - }); - - }); - -}); diff --git a/src/test/colorutilities.test.ts b/src/test/colorutilities.test.ts new file mode 100644 index 0000000..6ca3103 --- /dev/null +++ b/src/test/colorutilities.test.ts @@ -0,0 +1,1817 @@ +import {test} from "ava"; +import {hexToHsl, hexToHsla, hexToHslaTuple, hexToHslTuple, hexToRgb, hexToRgbTuple, hslaStringToHslaTuple, hslaToHsl, hslaToRgba, hslaToRgbaTuple, hslStringToHslTuple, hslToHsla, hslToRgb, hslToRgbTuple, hsvStringToHsvTuple, hsvToRgb, hsvToRgbTuple, isLight, lighten, lightenHex, lightenHsl, lightenHsla, lightenHsv, lightenRgb, lightenRgba, randomHexColor, randomHslColor, randomHsvColor, randomRgbColor, rgbaStringToRgbaTuple, rgbaToHex, rgbStringToRgbTuple, rgbToHex, rgbToHsl, rgbToHslTuple, rgbToHsv, rgbToHsvTuple, saturate, saturateHex, saturateHsl, saturateHsla, saturateHsv, saturateRgb, saturateRgba, toHex, toHsl, toHsv, toRgb} from "../colorutilities"; + +// tslint:disable:no-magic-numbers + +test("#hslStringToHslTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hslStringToHslTuple(null)); +}); + +test("#hslStringToHslTuple() => Throws an exception if the 'hue' property of the 'hsl' color isn't valid.", t => { + t.throws(() => hslStringToHslTuple("hsl(false, 50%, 100%)")); +}); + +test("#hslStringToHslTuple() => Throws an exception if the 'saturation' property of the 'hsl' color isn't valid.", t => { + t.throws(() => hslStringToHslTuple("hsl(30, false, 100%)")); +}); + +test("#hslStringToHslTuple() => Throws an exception if the 'lightness' property of the 'hsl' color isn't valid.", t => { + t.throws(() => hslStringToHslTuple("hsl(30, 50%, false)")); +}); + +test("#hslStringToHslTuple() => Converts string represented hsl colors to tuple represented hsl colors. #1", t => { + const [H, S, L] = hslStringToHslTuple("hsl(50, 60%, 100%)"); + + t.deepEqual(H, 50); + t.deepEqual(S, "60%"); + t.deepEqual(L, "100%"); +}); + +test("#hslStringToHslTuple() => Converts string represented hsl colors to tuple represented hsl colors. #2", t => { + const [H, S, L] = hslStringToHslTuple("hsl(50, 60, 100)"); + + t.deepEqual(H, 50); + t.deepEqual(S, "60%"); + t.deepEqual(L, "100%"); +}); + +test("#hslaStringToHslaTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hslaStringToHslaTuple(null)); +}); + +test("#hslaStringToHslaTuple() => Throws an exception if the 'hue' property of the 'hsla' color isn't valid.", t => { + t.throws(() => hslaStringToHslaTuple("hsla(false, 50%, 100%, 1)")); +}); + +test("#hslaStringToHslaTuple() => Throws an exception if the 'hue' property of the 'hsla' color isn't valid.", t => { + t.throws(() => hslaStringToHslaTuple("hsla(30, false, 100%, 1)")); +}); + +test("#hslaStringToHslaTuple() => Throws an exception if the 'lightness' property of the 'hsla' color isn't valid.", t => { + t.throws(() => hslaStringToHslaTuple("hsla(30, 50%, false, 1)")); +}); + +test("#hslaStringToHslaTuple() => Throws an exception if the 'alpha' property of the 'hsla' color isn't valid.", t => { + t.throws(() => hslaStringToHslaTuple("hsla(30, 50%, 100%, false)")); +}); + +test("#hslaStringToHslaTuple() => Converts string represented hsla colors to tuple represented hsla colors. #1", t => { + const [H, S, L, A] = hslaStringToHslaTuple("hsl(50, 60%, 100%, 0.5)"); + + t.deepEqual(H, 50); + t.deepEqual(S, "60%"); + t.deepEqual(L, "100%"); + t.deepEqual(A, 0.5); +}); + +test("#hslaStringToHslaTuple() => Converts string represented hsla colors to tuple represented hsla colors. #2", t => { + const [H, S, L, A] = hslaStringToHslaTuple("hsl(50, 60, 100, 0.9)"); + + t.deepEqual(H, 50); + t.deepEqual(S, "60%"); + t.deepEqual(L, "100%"); + t.deepEqual(A, 0.9); +}); + +test("#hslToHsla() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hslToHsla(null)); +}); + +test("#hslToHsla() => Throws an exception if the first argument is not a proper HSL color", t => { + // @ts-ignore + t.throws(() => hslToHsla("hsl(true, false, false)")); +}); + +test("#hslToHsla() => Throws an exception if the second argument is not of type 'number'", t => { + // @ts-ignore + t.throws(() => hslToHsla("hsl(50, 100%, 50%)", null)); +}); + +test("#hslToHsla() => Converts hsl colors to hsla colors. #1", t => { + t.deepEqual(hslToHsla("hsl(50, 100%, 100%)"), "hsla(50, 100%, 100%, 1)"); +}); + +test("#hslToHsla() => Converts hsl colors to hsla colors. #2", t => { + t.deepEqual(hslToHsla("hsl(50, 100%, 100%)", 0.5), "hsla(50, 100%, 100%, 0.5)"); +}); + +test("#hslToHsla() => Automatically adds missing '%' symbols for saturation and lightness.", t => { + t.deepEqual(hslToHsla("hsl(50, 100, 100)"), "hsla(50, 100%, 100%, 1)"); +}); + +test("#hslToRgbTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hslToRgbTuple(null)); +}); + +test("#hslToRgbTuple() => Throws an exception if the first argument is not a proper HSL color", t => { + // @ts-ignore + t.throws(() => hslToRgbTuple("hsl(true, false, false)")); +}); + +test("#hslToRgbTuple() => Converts hsl colors to rgb colors. #1", t => { + const [R, G, B] = hslToRgbTuple("hsl(50, 100%, 100%)"); + t.deepEqual(R, 255); + t.deepEqual(G, 255); + t.deepEqual(B, 255); +}); + +test("#hslToRgbTuple() => Converts hsl colors to rgb colors. #2", t => { + const [R, G, B] = hslToRgbTuple("hsl(50, 84%, 44%)"); + t.deepEqual(R, 206); + t.deepEqual(G, 175); + t.deepEqual(B, 18); +}); + +test("#hslToRgbTuple() => Converts hsl colors to rgb colors. #3", t => { + const [R, G, B] = hslToRgbTuple("hsl(194, 63%, 8%)"); + t.deepEqual(R, 8); + t.deepEqual(G, 27); + t.deepEqual(B, 33); +}); + +test("#hslToRgbTuple() => Works, even if '%' symbols are omitted from the saturation and lightness properties.", t => { + const [R, G, B] = hslToRgbTuple("hsl(194, 63, 8)"); + t.deepEqual(R, 8); + t.deepEqual(G, 27); + t.deepEqual(B, 33); +}); + +test("#hslToRgb() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hslToRgb(null)); +}); + +test("#hslToRgb() => Throws an exception if the first argument is not a proper HSL color", t => { + t.throws(() => hslToRgb("hsl(true, false, false)")); +}); + +test("#hslToRgb() => Converts hsl colors to rgb colors. #1", t => { + t.deepEqual(hslToRgb("hsl(50, 100%, 100%)"), "rgb(255, 255, 255)"); +}); + +test("#hslToRgb() => Converts hsl colors to rgb colors. #2", t => { + t.deepEqual(hslToRgb("hsl(50, 84%, 44%)"), "rgb(206, 175, 18)"); +}); + +test("#hslToRgb() => Converts hsl colors to rgb colors. #3", t => { + t.deepEqual(hslToRgb("hsl(194, 63%, 8%)"), "rgb(8, 27, 33)"); +}); + +test("#hslToRgb() => Works, even if '%' symbols are omitted from the saturation and lightness properties.", t => { + t.deepEqual(hslToRgb("hsl(194, 63, 8)"), "rgb(8, 27, 33)"); +}); + +test("#hslaToRgbaTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hslaToRgbaTuple(null)); +}); + +test("#hslaToRgbaTuple() => Throws an exception if the first argument is not a proper HSLa color", t => { + t.throws(() => hslaToRgbaTuple("hsl(true, false, false)")); +}); + +test("#hslaToRgbaTuple() => Converts HSLa colors to RGBa colors. #1", t => { + const [R, G, B, A] = hslaToRgbaTuple("hsla(118, 100%, 52%, 0.56)"); + t.deepEqual(R, 18); + t.deepEqual(G, 255); + t.deepEqual(B, 10); + t.deepEqual(A, 0.56); +}); + +test("#hslaToRgbaTuple() => Converts HSLa colors to RGBa colors. #2", t => { + const [R, G, B, A] = hslaToRgbaTuple("hsla(36, 31%, 69%, 0.84)"); + t.deepEqual(R, 200); + t.deepEqual(G, 181); + t.deepEqual(B, 151); + t.deepEqual(A, 0.84); +}); + +test("#hslaToRgbaTuple() => Converts HSLa colors to RGBa colors. #3", t => { + const [R, G, B, A] = hslaToRgbaTuple("hsla(360, 100%, 100%, 0.00)"); + t.deepEqual(R, 255); + t.deepEqual(G, 255); + t.deepEqual(B, 255); + t.deepEqual(A, 0); +}); + +test("#hslaToRgbaTuple() => Works, even if '%' symbols are omitted from the saturation and lightness properties.", t => { + const [R, G, B, A] = hslaToRgbaTuple("hsla(36, 31, 69, 0.84)"); + t.deepEqual(R, 200); + t.deepEqual(G, 181); + t.deepEqual(B, 151); + t.deepEqual(A, 0.84); +}); + +test("#hslaToRgba() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hslaToRgba(null)); +}); + +test("#hslaToRgba() => Throws an exception if the first argument is not a proper HSLa color", t => { + t.throws(() => hslaToRgba("hsl(true, false, false)")); +}); + +test("#hslaToRgba() => Converts HSLa colors to RGBa colors. #1", t => { + t.deepEqual(hslaToRgba("hsla(118, 100%, 52%, 0.56)"), "rgba(18, 255, 10, 0.56)"); +}); + +test("#hslaToRgba() => Converts HSLa colors to RGBa colors. #2", t => { + t.deepEqual(hslaToRgba("hsla(36, 31%, 69%, 0.84)"), "rgba(200, 181, 151, 0.84)"); +}); + +test("#hslaToRgba() => Converts HSLa colors to RGBa colors. #3", t => { + t.deepEqual(hslaToRgba("hsla(360, 100%, 100%, 0.00)"), "rgba(255, 255, 255, 0)"); +}); + +test("#hslaToRgba() => Works, even if '%' symbols are omitted from the saturation and lightness properties.", t => { + t.deepEqual(hslaToRgba("hsla(36, 31, 69, 0.84)"), "rgba(200, 181, 151, 0.84)"); +}); + +test("#hslaToHsl() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hslaToHsl(null)); +}); + +test("#hslaToHsl() => Throws an exception if the first argument is not a proper HSLa color", t => { + t.throws(() => hslaToHsl("hsl(true, false, false)")); +}); + +test("#hslaToHsl() => Converts HSLa colors to hsl colors. #1", t => { + t.deepEqual(hslaToHsl("hsla(118, 100%, 52%, 0.56)"), "hsl(118, 100%, 52%)"); +}); + +test("#hslaToHsl() => Converts HSLa colors to RGBa colors. #2", t => { + t.deepEqual(hslaToHsl("hsla(36, 31%, 69%, 0.84)"), "hsl(36, 31%, 69%)"); +}); + +test("#hslaToHsl() => Converts HSLa colors to RGBa colors. #3", t => { + t.deepEqual(hslaToHsl("hsla(360, 100%, 100%, 0.00)"), "hsl(360, 100%, 100%)"); +}); + +test("#hslaToHsl() => Works, even if '%' symbols are omitted from the saturation and lightness properties.", t => { + t.deepEqual(hslaToHsl("hsla(36, 31, 69, 0.84)"), "hsl(36, 31%, 69%)"); +}); + +test("#rgbStringToRgbTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => rgbStringToRgbTuple(null)); +}); + +test("#rgbStringToRgbTuple() => Throws an exception if the first argument is not a proper rgb color", t => { + t.throws(() => rgbStringToRgbTuple("rgb(true, false, false)")); +}); + +test("#rgbStringToRgbTuple() => Converts string representations of RGB colors to tuple representations. #1", t => { + const [R, G, B] = rgbStringToRgbTuple("rgb(50, 100, 200)"); + t.deepEqual(R, 50); + t.deepEqual(G, 100); + t.deepEqual(B, 200); +}); + +test("#rgbStringToRgbTuple() => Converts string representations of RGB colors to tuple representations. #2", t => { + const [R, G, B] = rgbStringToRgbTuple("rgb(20, 85, 255)"); + t.deepEqual(R, 20); + t.deepEqual(G, 85); + t.deepEqual(B, 255); +}); + +test("#rgbStringToRgbTuple() => Converts string representations of RGB colors to tuple representations. #3", t => { + const [R, G, B] = rgbStringToRgbTuple("rgb(0, 0, 80)"); + t.deepEqual(R, 0); + t.deepEqual(G, 0); + t.deepEqual(B, 80); +}); + +test("#rgbaStringToRgbaTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => rgbaStringToRgbaTuple(null)); +}); + +test("#rgbaStringToRgbaTuple() => Throws an exception if the first argument is not a proper RGBa color", t => { + t.throws(() => rgbaStringToRgbaTuple("rgba(true, false, false, false)")); +}); + +test("#rgbaStringToRgbaTuple() => Converts string representations of RGBa colors to tuple representations. #1", t => { + const [R, G, B, A] = rgbaStringToRgbaTuple("rgba(50, 100, 200, 0.9)"); + t.deepEqual(R, 50); + t.deepEqual(G, 100); + t.deepEqual(B, 200); + t.deepEqual(A, 0.9); +}); + +test("#rgbaStringToRgbaTuple() => Converts string representations of RGBa colors to tuple representations. #2", t => { + const [R, G, B, A] = rgbaStringToRgbaTuple("rgba(140, 10, 89, 0.2)"); + t.deepEqual(R, 140); + t.deepEqual(G, 10); + t.deepEqual(B, 89); + t.deepEqual(A, 0.2); +}); + +test("#rgbaStringToRgbaTuple() => Converts string representations of RGBa colors to tuple representations. #3", t => { + const [R, G, B, A] = rgbaStringToRgbaTuple("rgba(0, 0, 0, 0)"); + t.deepEqual(R, 0); + t.deepEqual(G, 0); + t.deepEqual(B, 0); + t.deepEqual(A, 0); +}); + +test("#rgbToHex() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => rgbToHex(null)); + +}); + +test("#rgbToHex() => Throws an exception if the first argument is not a proper RGB color", t => { + t.throws(() => rgbToHex("rgb(true, false, false)")); + +}); + +test("#rgbToHex() => Converts RGB colors to hex colors. #1", t => { + t.deepEqual(rgbToHex("rgb(125, 244, 66)"), "#7df442"); +}); + +test("#rgbToHex() => Converts RGB colors to hex colors. #2", t => { + t.deepEqual(rgbToHex("rgb(255, 255, 255)"), "#ffffff"); +}); + +test("#rgbToHex() => Converts RGB colors to hex colors. #3", t => { + t.deepEqual(rgbToHex("rgb(0, 0, 0)"), "#000000"); +}); + +test("#rgbToHex() => Converts RGB colors to hex colors. #4", t => { + t.deepEqual(rgbToHex("rgb(39, 109, 65)"), "#276d41"); +}); + +test("#rgbaToHex() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => rgbaToHex(null)); + +}); + +test("#rgbaToHex() => Throws an exception if the first argument is not a proper RGBa color", t => { + + t.throws(() => rgbaToHex("rgba(true, false, false, false)")); + +}); + +test("#rgbaToHex() => Throws an exception if the second argument is given but is not a string", t => { + // @ts-ignore + t.throws(() => rgbaToHex("rgba(10, 50, 80, 1)", false)); + +}); + +test("#rgbaToHex() => Converts RGBa colors to hex colors. #1", t => { + t.deepEqual(rgbaToHex("rgba(125, 244, 66, 1)"), "#7df442"); +}); + +test("#rgbaToHex() => Converts RGBa colors to hex colors. #2", t => { + t.deepEqual(rgbaToHex("rgba(255, 255, 255, 0.5)"), "#ffffff"); +}); + +test("#rgbaToHex() => Converts RGBa colors to hex colors. #3", t => { + t.deepEqual(rgbaToHex("rgba(0, 0, 0, 1)"), "#000000"); +}); + +test("#rgbaToHex() => Converts RGBa colors to hex colors. #4", t => { + t.deepEqual(rgbaToHex("rgba(39, 109, 65, 0.8)"), "#276d41"); +}); + +test("#rgbaToHex() => Converts RGBa colors to hex colors including interpolation of the alpha channel. #1", t => { + t.deepEqual(rgbaToHex("rgba(255, 255, 255, 0)", "white"), "#ffffff"); +}); + +test("#rgbaToHex() => Converts RGBa colors to hex colors including interpolation of the alpha channel. #2", t => { + t.deepEqual(rgbaToHex("rgba(255, 255, 255, 0)", "black"), "#000000"); +}); + +test("#hsvStringToHsvTuple() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => hsvStringToHsvTuple(null)); + +}); + +test("#hsvStringToHsvTuple() => Throws an exception if the first argument is not a proper HSV/HSB color. #1", t => { + t.throws(() => hsvStringToHsvTuple("hsb(50, 100, false)")); + +}); + +test("#hsvStringToHsvTuple() => Throws an exception if the first argument is not a proper HSV/HSB color. #2", t => { + t.throws(() => hsvStringToHsvTuple("hsv(50, 100, false)")); + +}); + +test("#hsvStringToHsvTuple() => Generates tuple representations of HSV/HSB colors from HSV/HSB strings. #1", t => { + const [H, S, V] = hsvStringToHsvTuple("hsb(1, 2, 3)"); + t.deepEqual(H, 1); + t.deepEqual(S, 2); + t.deepEqual(V, 3); +}); + +test("#hsvStringToHsvTuple() => Generates tuple representations of HSV/HSB colors from HSV/HSB strings. #2", t => { + const [H, S, V] = hsvStringToHsvTuple("hsv(0, 0, 0)"); + t.deepEqual(H, 0); + t.deepEqual(S, 0); + t.deepEqual(V, 0); +}); + +test("#hsvStringToHsvTuple() => Generates tuple representations of HSV/HSB colors from HSV/HSB strings. #3", t => { + const [H, S, V] = hsvStringToHsvTuple("hsb(360, 100, 100)"); + t.deepEqual(H, 360); + t.deepEqual(S, 100); + t.deepEqual(V, 100); +}); + +test("#hsvToRgbTuple() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => hsvToRgbTuple(null)); + +}); + +test("#hsvToRgbTuple() => Throws an exception if the first argument is not a proper HSV/HSB color. #1", t => { + + t.throws(() => hsvToRgbTuple("hsb(50, 100, false)")); + +}); + +test("#hsvToRgbTuple() => Throws an exception if the first argument is not a proper HSV/HSB color. #2", t => { + + t.throws(() => hsvToRgbTuple("hsv(50, 100, false)")); + +}); + +test("#hsvToRgbTuple() => Generates tuple representations of RGB colors from HSV/HSB strings. #1", t => { + const [R, G, B] = hsvToRgbTuple("hsv(10, 5, 50)"); + t.deepEqual(R, 128); + t.deepEqual(G, 122); + t.deepEqual(B, 121); +}); + +test("#hsvToRgbTuple() => Generates tuple representations of RGB colors from HSV/HSB strings. #2", t => { + const [R, G, B] = hsvToRgbTuple("hsv(125, 36, 11)"); + t.deepEqual(R, 18); + t.deepEqual(G, 28); + t.deepEqual(B, 19); +}); + +test("#hsvToRgbTuple() => Generates tuple representations of RGB colors from HSV/HSB strings. #3", t => { + const [R, G, B] = hsvToRgbTuple("hsv(359, 0, 0)"); + t.deepEqual(R, 0); + t.deepEqual(G, 0); + t.deepEqual(B, 0); +}); + +test("#hsvToRgb() =>Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => hsvToRgb(null)); + +}); + +test("#hsvToRgb() =>Throws an exception if the first argument is not a proper HSV/HSB color. #1", t => { + + t.throws(() => hsvToRgb("hsb(50, 100, false)")); + +}); + +test("#hsvToRgb() =>Throws an exception if the first argument is not a proper HSV/HSB color. #2", t => { + + t.throws(() => hsvToRgb("hsv(50, 100, false)")); + +}); + +test("#hsvToRgb() =>Generates RGB colors from HSV/HSB strings. #1", t => { + t.deepEqual(hsvToRgb("hsv(10, 5, 50)"), "rgb(128, 122, 121)"); +}); + +test("#hsvToRgb() =>Generates RGB colors from HSV/HSB strings. #2", t => { + t.deepEqual(hsvToRgb("hsv(125, 36, 11)"), "rgb(18, 28, 19)"); +}); + +test("#hsvToRgb() =>Generates RGB colors from HSV/HSB strings. #3", t => { + t.deepEqual(hsvToRgb("hsv(359, 0, 0)"), "rgb(0, 0, 0)"); +}); + +test("#toHex() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => toHex(null)); + +}); + +test("#toHex() => Throws an exception if the first argument is not a proper color. #1", t => { + + t.throws(() => toHex("foobar")); + +}); + +test("#toHex() => Throws an exception if the first argument is not a proper color. #2", t => { + + t.throws(() => toHex("#1234")); + +}); + +test("#toHex() => Throws an exception if the first argument is not a proper color. #3", t => { + t.throws(() => toHex("1234")); + +}); + +test("#toHex() => Throws an exception if the first argument is not a proper color. #4", t => { + t.throws(() => toHex("rgb()")); +}); + +test("#toHex() => Throws an exception if the first argument is not a proper color. #5", t => { + t.throws(() => toHex("hsl(false, rgb, true)")); + +}); + +test("#toHex() => returns given hex codes just as they were if they start with a '#' and is otherwise valid. #1", t => { + t.deepEqual(toHex("#123"), "#123"); +}); + +test("#toHex() => returns given hex codes just as they were if they start with a '#' and is otherwise valid. #2", t => { + t.deepEqual(toHex("#ffffff"), "#ffffff"); +}); + +test("#toHex() => Adds a '#' in front of valid hex codes that lacks it. #1", t => { + t.deepEqual(toHex("ffffff"), "#ffffff"); +}); + +test("#toHex() => Adds a '#' in front of valid hex codes that lacks it. #2", t => { + t.deepEqual(toHex("123"), "#123"); +}); + +test("#toHex() => Converts HTML color names to their actual hex values. #1", t => { + t.deepEqual(toHex("white"), "#ffffff"); +}); + +test("#toHex() => Converts HTML color names to their actual hex values. #2", t => { + t.deepEqual(toHex("antiquewhite"), "#faebd7"); +}); + +test("#toHex() => Converts RGB colors to hex values. #1", t => { + t.deepEqual(toHex("rgb(0, 0, 0)"), "#000000"); +}); + +test("#toHex() => Converts RGB colors to hex values. #2", t => { + t.deepEqual(toHex("rgb(255, 255, 255)"), "#ffffff"); +}); + +test("#toHex() => Converts RGB colors to hex values. #3", t => { + t.deepEqual(toHex("rgb(88, 100, 119)"), "#586477"); +}); + +test("#toHex() => Converts RGBa colors to hex values. #1", t => { + t.deepEqual(toHex("rgba(88, 100, 119, .5)"), "#586477"); +}); + +test("#toHex() => Converts HSL colors to hex values. #1", t => { + t.deepEqual(toHex("hsl(157, 100%, 50%)"), "#00ff9d"); +}); + +test("#toHex() => Converts HSL colors to hex values. #2", t => { + t.deepEqual(toHex("hsl(79, 100%, 16%)"), "#385200"); +}); + +test("#toHex() => Converts HSL colors to hex values. #3", t => { + t.deepEqual(toHex("hsl(229, 47%, 16%)"), "#161d3c"); +}); + +test("#toHex() => Converts HSV/HSB colors to hex values. #1", t => { + t.deepEqual(toHex("hsv(228, 75, 78)"), "#3250c7"); +}); + +test("#toHex() => Converts HSV/HSB colors to hex values. #2", t => { + t.deepEqual(toHex("hsv(272, 83, 51)"), "#501682"); +}); + +test("#toHex() => Converts HSV/HSB colors to hex values. #3", t => { + t.deepEqual(toHex("hsb(344, 100, 12)"), "#1f0008"); +}); + +test("#hexToRgbTuple() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => hexToRgbTuple(null)); + +}); + +test("#hexToRgbTuple() => Throws an exception if the first argument is not a proper hex color.", t => { + t.throws(() => hexToRgbTuple("foobar")); + +}); + +test("#hexToRgbTuple() => Generates RGB tuples from hex strings. #1", t => { + const [R, G, B] = hexToRgbTuple("#000000"); + t.deepEqual(R, 0); + t.deepEqual(G, 0); + t.deepEqual(B, 0); +}); + +test("#hexToRgbTuple() => Generates RGB tuples from hex strings. #2", t => { + const [R, G, B] = hexToRgbTuple("ffffff"); + t.deepEqual(R, 255); + t.deepEqual(G, 255); + t.deepEqual(B, 255); +}); + +test("#hexToRgbTuple() => Generates RGB tuples from hex strings. #3", t => { + const [R, G, B] = hexToRgbTuple("4286f4"); + t.deepEqual(R, 66); + t.deepEqual(G, 134); + t.deepEqual(B, 244); +}); + +test("#hexToRgb() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => hexToRgb(null)); +}); + +test("#hexToRgb() => Throws an exception if the first argument is not a proper hex color.", t => { + t.throws(() => hexToRgb("foobar")); + +}); + +test("#hexToRgb() => Generates RGB colors from hex strings. #1", t => { + t.deepEqual(hexToRgb("#000000"), "rgb(0, 0, 0)"); +}); + +test("#hexToRgb() => Generates RGB tuples from hex strings. #2", t => { + t.deepEqual(hexToRgb("ffffff"), "rgb(255, 255, 255)"); +}); + +test("#hexToRgb() => Generates RGB tuples from hex strings. #3", t => { + t.deepEqual(hexToRgb("4286f4"), "rgb(66, 134, 244)"); +}); + +test("#toRgb() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => toRgb(null)); +}); + +test("#toRgb() => Throws an exception if the first argument is not a proper color. #1", t => { + t.throws(() => toRgb("foobar")); + +}); + +test("#toRgb() => Throws an exception if the first argument is not a proper color. #2", t => { + t.throws(() => toRgb("#1234")); + +}); + +test("#toRgb() => Throws an exception if the first argument is not a proper color. #3", t => { + t.throws(() => toRgb("1234")); + +}); + +test("#toRgb() => Throws an exception if the first argument is not a proper color. #4", t => { + t.throws(() => toRgb("rgb()")); +}); + +test("#toRgb() => Throws an exception if the first argument is not a proper color. #5", t => { + t.throws(() => toRgb("hsl(false, rgb, true)")); + +}); + +test("#toRgb() => returns rgb colors just as they were. #1", t => { + t.deepEqual(toRgb("rgb(50, 60, 70)"), "rgb(50, 60, 70)"); +}); + +test("#toRgb() => returns rgb colors just as they were. #2", t => { + t.deepEqual(toRgb("rgb(255, 255, 255)"), "rgb(255, 255, 255)"); +}); + +test("#toRgb() => Converts hex colors to RGB colors. #1", t => { + t.deepEqual(toRgb("#000000"), "rgb(0, 0, 0)"); +}); + +test("#toRgb() => Converts hex colors to RGB colors. #2", t => { + t.deepEqual(toRgb("ffffff"), "rgb(255, 255, 255)"); +}); + +test("#toRgb() => Converts hex colors to RGB colors. #3", t => { + t.deepEqual(toRgb("4286f4"), "rgb(66, 134, 244)"); +}); + +test("#toRgb() => Converts HTML color names to RGB values. #1", t => { + t.deepEqual(toRgb("antiquewhite"), "rgb(250, 235, 215)"); +}); + +test("#toRgb() => Converts HTML color names to RGB values. #2", t => { + t.deepEqual(toRgb("aquamarine"), "rgb(127, 255, 212)"); +}); + +test("#toRgb() => Converts HSL colors to RGB colors. #1", t => { + t.deepEqual(toRgb("hsl(96, 41%, 78%)"), "rgb(194, 222, 176)"); +}); + +test("#toRgb() => Converts HSL colors to RGB colors. #2", t => { + t.deepEqual(toRgb("hsl(29, 87%, 50%)"), "rgb(238, 124, 17)"); +}); + +test("#toRgb() => Converts HSL colors to RGB colors. #3", t => { + t.deepEqual(toRgb("hsl(0, 87%, 0%)"), "rgb(0, 0, 0)"); +}); + +test("#toRgb() => Converts HSV/HSB colors to RGB colors. #1", t => { + t.deepEqual(toRgb("hsv(162, 68, 13)"), "rgb(11, 33, 26)"); +}); + +test("#toRgb() => Converts HSV/HSB colors to RGB colors. #2", t => { + t.deepEqual(toRgb("hsv(245, 35, 67)"), "rgb(116, 111, 171)"); +}); + +test("#toRgb() => Converts HSV/HSB colors to RGB colors. #3", t => { + t.deepEqual(toRgb("hsb(259, 0, 99)"), "rgb(252, 252, 252)"); +}); + +test("#hexToHslTuple() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => hexToHslTuple(null)); +}); + +test("#hexToHslTuple() => Throws an exception if the first argument is not a proper hex color.", t => { + t.throws(() => hexToHslTuple("foobar")); + +}); + +test("#hexToHslTuple() => Generates HSL tuples from hex strings. #1", t => { + const [H, S, L] = hexToHslTuple("#87eeca"); + t.deepEqual(H, 159); + t.deepEqual(S, "75%"); + t.deepEqual(L, "73%"); +}); + +test("#hexToHslTuple() => Generates HSL tuples from hex strings. #2", t => { + const [H, S, L] = hexToHslTuple("#6e64a0"); + t.deepEqual(H, 250); + t.deepEqual(S, "24%"); + t.deepEqual(L, "51%"); +}); + +test("#hexToHslTuple() => Generates HSL tuples from hex strings. #3", t => { + const [H, S, L] = hexToHslTuple("#1e681d"); + t.deepEqual(H, 119); + t.deepEqual(S, "56%"); + t.deepEqual(L, "26%"); +}); + +test("#hexToHslaTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hexToHslaTuple(null)); +}); + +test("#hexToHslaTuple() => Throws an exception if the first argument is not a proper hex color.", t => { + t.throws(() => hexToHslaTuple("foobar")); + +}); + +test("#hexToHslaTuple() => Throws an exception if the second argument is not a number.", t => { + // @ts-ignore + t.throws(() => hexToHslaTuple("#000000", false)); + +}); + +test("#hexToHslaTuple() => Generates HSLa tuples from hex strings. #1", t => { + const [H, S, L, A] = hexToHslaTuple("#87eeca"); + t.deepEqual(H, 159); + t.deepEqual(S, "75%"); + t.deepEqual(L, "73%"); + t.deepEqual(A, 1); +}); + +test("#hexToHslaTuple() => Generates HSLa tuples from hex strings. #2", t => { + const [H, S, L, A] = hexToHslaTuple("#6e64a0", 0.5); + t.deepEqual(H, 250); + t.deepEqual(S, "24%"); + t.deepEqual(L, "51%"); + t.deepEqual(A, 0.5); +}); + +test("#hexToHslaTuple() => Generates HSLa tuples from hex strings. #3", t => { + const [H, S, L, A] = hexToHslaTuple("#1e681d", 0); + t.deepEqual(H, 119); + t.deepEqual(S, "56%"); + t.deepEqual(L, "26%"); + t.deepEqual(A, 0); +}); + +test("#hexToHsla() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => hexToHsla(null)); +}); + +test("#hexToHsla() => Throws an exception if the first argument is not a proper hex color.", t => { + t.throws(() => hexToHsla("foobar")); +}); + +test("#hexToHsla() => Throws an exception if the second argument is not a number.", t => { + // @ts-ignore + t.throws(() => hexToHsla("#000000", false)); +}); + +test("#hexToHsla() => Generates HSLa colors from hex strings. #1", t => { + t.deepEqual(hexToHsla("#87eeca"), "hsla(159, 75%, 73%, 1)"); +}); + +test("#hexToHsla() => Generates HSLa colors from hex strings. #2", t => { + t.deepEqual(hexToHsla("#6e64a0", 0.5), "hsla(250, 24%, 51%, 0.5)"); +}); + +test("#hexToHsla() => Generates HSLa tuples from hex strings. #3", t => { + t.deepEqual(hexToHsla("#1e681d", 0), "hsla(119, 56%, 26%, 0)"); +}); + +test("#hexToHsl() => Throws an exception if the first argument is not of type 'string'", t => { + + // @ts-ignore + t.throws(() => hexToHsl(null)); +}); + +test("#hexToHsl() => Throws an exception if the first argument is not a proper hex color.", t => { + t.throws(() => hexToHsl("foobar")); + +}); + +test("#hexToHsl() => Generates HSL colors from hex strings. #1", t => { + t.deepEqual(hexToHsl("#87eeca"), "hsl(159, 75%, 73%)"); +}); + +test("#hexToHsl() => Generates HSL colors from hex strings. #2", t => { + t.deepEqual(hexToHsl("#6e64a0"), "hsl(250, 24%, 51%)"); +}); + +test("#hexToHsl() => Generates HSL colors from hex strings. #3", t => { + t.deepEqual(hexToHsl("#1e681d"), "hsl(119, 56%, 26%)"); +}); + +test("#rgbToHslTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => rgbToHslTuple(null)); +}); + +test("#rgbToHslTuple() => Throws an exception if the first argument is not a proper RGB color.", t => { + t.throws(() => rgbToHslTuple("rgb()")); +}); + +test("#rgbToHslTuple() => Generates HSL tuples from RGB colors. #1", t => { + const [H, S, L] = rgbToHslTuple("rgb(24, 102, 20)"); + t.deepEqual(H, 117); + t.deepEqual(S, "67%"); + t.deepEqual(L, "24%"); + +}); + +test("#rgbToHslTuple() => Generates HSL tuples from RGB colors. #2", t => { + const [H, S, L] = rgbToHslTuple("rgb(81, 122, 179)"); + t.deepEqual(H, 215); + t.deepEqual(S, "39%"); + t.deepEqual(L, "51%"); +}); + +test("#rgbToHslTuple() => Generates HSL tuples from RGB colors. #3", t => { + const [H, S, L] = rgbToHslTuple("rgb(184, 0, 3)"); + t.deepEqual(H, 359); + t.deepEqual(S, "100%"); + t.deepEqual(L, "36%"); +}); + +test("#rgbToHsl() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => rgbToHsl(null)); +}); + +test("#rgbToHsl() => Throws an exception if the first argument is not a proper RGB color.", t => { + t.throws(() => rgbToHsl("rgb()")); +}); + +test("#rgbToHsl() => Generates HSL colors from RGB colors. #1", t => { + t.deepEqual(rgbToHsl("rgb(24, 102, 20)"), "hsl(117, 67%, 24%)"); +}); + +test("#rgbToHsl() => Generates HSL tuples from RGB colors. #2", t => { + t.deepEqual(rgbToHsl("rgb(81, 122, 179)"), "hsl(215, 39%, 51%)"); +}); + +test("#rgbToHsl() => Generates HSL tuples from RGB colors. #3", t => { + t.deepEqual(rgbToHsl("rgb(184, 0, 3)"), "hsl(359, 100%, 36%)"); +}); + +test("#toHsl() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => toHsl(null)); +}); + +test("#toHsl() => Throws an exception if the first argument is not a proper color. #1", t => { + t.throws(() => toHsl("foobar")); +}); + +test("#toHsl() => Throws an exception if the first argument is not a proper color. #2", t => { + t.throws(() => toHsl("#1234")); +}); + +test("#toHsl() => Throws an exception if the first argument is not a proper color. #3", t => { + t.throws(() => toHsl("1234")); +}); + +test("#toHsl() => Throws an exception if the first argument is not a proper color. #4", t => { + t.throws(() => toHsl("rgb()")); +}); + +test("#toHsl() => Throws an exception if the first argument is not a proper color. #5", t => { + t.throws(() => toHsl("hsl(false, rgb, true)")); +}); + +test("#toHsl() => returns correct HSL colors just as they were.", t => { + t.deepEqual(toHsl("hsl(50, 60%, 70%)"), "hsl(50, 60%, 70%)"); +}); + +test("#toHsl() => returns HSL with missing '%' symbols with the addition of those..", t => { + t.deepEqual(toHsl("hsl(50, 60, 70)"), "hsl(50, 60%, 70%)"); +}); + +test("#toHsl() => Converts RGB colors to HSL colors. #1", t => { + t.deepEqual(toHsl("rgb(30, 104, 29)"), "hsl(119, 56%, 26%)"); +}); + +test("#toHsl() => Converts RGB colors to HSL colors. #2", t => { + t.deepEqual(toHsl("rgb(65, 58, 95)"), "hsl(251, 24%, 30%)"); +}); + +test("#toHsl() => Converts RGB colors to HSL colors. #3", t => { + t.deepEqual(toHsl("rgb(201, 243, 205)"), "hsl(126, 64%, 87%)"); +}); + +test("#toHsl() => Converts hex colors to HSL colors. #1", t => { + t.deepEqual(toHsl("#000000"), "hsl(0, 0%, 0%)"); +}); + +test("#toHsl() => Converts hex colors to HSL colors. #2", t => { + t.deepEqual(toHsl("#ffffff"), "hsl(0, 0%, 100%)"); +}); + +test("#toHsl() => Converts hex colors to HSL colors. #3", t => { + t.deepEqual(toHsl("ff4d4d"), "hsl(0, 100%, 65%)"); +}); + +test("#toHsl() => Converts HTML color names to HSL values. #1", t => { + t.deepEqual(toHsl("antiquewhite"), "hsl(34, 78%, 91%)"); +}); + +test("#toHsl() => Converts HTML color names to HSL values. #2", t => { + t.deepEqual(toHsl("aquamarine"), "hsl(160, 100%, 75%)"); +}); + +test("#toHsl() => Converts HSV/HSB colors to HSL colors. #1", t => { + t.deepEqual(toHsl("hsv(162, 68, 13)"), "hsl(161, 50%, 9%)"); +}); + +test("#toHsl() => Converts HSV/HSB colors to HSL colors. #2", t => { + t.deepEqual(toHsl("hsv(245, 35, 67)"), "hsl(245, 26%, 55%)"); +}); + +test("#toHsl() => Converts HSV/HSB colors to RGB colors. #3", t => { + t.deepEqual(toHsl("hsb(259, 0, 99)"), "hsl(0, 0%, 99%)"); +}); + +test("#rgbToHsvTuple() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => rgbToHsvTuple(null)); +}); + +test("#rgbToHsvTuple() => Throws an exception if the first argument is not a proper RGB color.", t => { + t.throws(() => rgbToHsvTuple("rgb()")); +}); + +test("#rgbToHsvTuple() => Generates HSV/HSB tuples from RGB colors. #1", t => { + const [H, S, V] = rgbToHsvTuple("rgb(24, 102, 20)"); + t.deepEqual(H, 117); + t.deepEqual(S, 80); + t.deepEqual(V, 40); + +}); + +test("#rgbToHsvTuple() => Generates HSV/HSB tuples from RGB colors. #2", t => { + const [H, S, V] = rgbToHsvTuple("rgb(81, 122, 179)"); + t.deepEqual(H, 215); + t.deepEqual(S, 55); + t.deepEqual(V, 70); +}); + +test("#rgbToHsvTuple() => Generates HSV/HSB tuples from RGB colors. #3", t => { + const [H, S, V] = rgbToHsvTuple("rgb(184, 0, 3)"); + t.deepEqual(H, 359); + t.deepEqual(S, 100); + t.deepEqual(V, 72); +}); + +test("#rgbToHsv() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => rgbToHsv(null)); +}); + +test("#rgbToHsv() => Throws an exception if the first argument is not a proper RGB color.", t => { + t.throws(() => rgbToHsv("rgb()")); +}); + +test("#rgbToHsv() => Generates HSV/HSB colors from RGB colors. #1", t => { + t.deepEqual(rgbToHsv("rgb(24, 102, 20)"), "hsb(117, 80, 40)"); +}); + +test("#rgbToHsv() => Generates HSV/HSB colors from RGB colors. #2", t => { + t.deepEqual(rgbToHsv("rgb(81, 122, 179)"), "hsb(215, 55, 70)"); +}); + +test("#rgbToHsv() => Generates HSV/HSB colors from RGB colors. #3", t => { + t.deepEqual(rgbToHsv("rgb(184, 0, 3)"), "hsb(359, 100, 72)"); +}); + +test("#toHsv() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => toHsv(null)); +}); + +test("#toHsv() => Throws an exception if the first argument is not a proper color. #1", t => { + t.throws(() => toHsv("foobar")); +}); + +test("#toHsv() => Throws an exception if the first argument is not a proper color. #2", t => { + t.throws(() => toHsv("#1234")); +}); + +test("#toHsv() => Throws an exception if the first argument is not a proper color. #3", t => { + t.throws(() => toHsv("1234")); +}); + +test("#toHsv() => Throws an exception if the first argument is not a proper color. #4", t => { + t.throws(() => toHsv("rgb()")); +}); + +test("#toHsv() => Throws an exception if the first argument is not a proper color. #5", t => { + t.throws(() => toHsv("hsl(false, rgb, true)")); +}); + +test("#toHsv() => Throws an exception if the first argument is not a proper color. #6", t => { + t.throws(() => toHsv("hsb()")); +}); + +test("#toHsv() => Throws an exception if the first argument is not a proper color. #7", t => { + t.throws(() => toHsv("hsv()")); +}); + +test("#toHsv() => returns correct HSV/HSB colors just as they were. #1", t => { + t.deepEqual(toHsv("hsv(50, 60, 70)"), "hsv(50, 60, 70)"); +}); + +test("#toHsv() => returns correct HSV/HSB colors just as they were. #2", t => { + t.deepEqual(toHsv("hsb(50, 60, 70)"), "hsb(50, 60, 70)"); +}); + +test("#toHsv() => Converts RGB colors to HSV/HSB colors. #1", t => { + t.deepEqual(toHsv("rgb(30, 104, 29)"), "hsb(119, 72, 41)"); +}); + +test("#toHsv() => Converts RGB colors to HSV/HSB colors. #2", t => { + t.deepEqual(toHsv("rgb(65, 58, 95)"), "hsb(251, 39, 37)"); +}); + +test("#toHsv() => Converts RGB colors to HSV/HSB colors. #3", t => { + t.deepEqual(toHsv("rgb(201, 243, 205)"), "hsb(126, 17, 95)"); +}); + +test("#toHsv() => Converts hex colors to HSV/HSB colors. #1", t => { + t.deepEqual(toHsv("#000000"), "hsb(0, 0, 0)"); +}); + +test("#toHsv() => Converts hex colors to HSV/HSB colors. #2", t => { + t.deepEqual(toHsv("#ffffff"), "hsb(0, 0, 100)"); +}); + +test("#toHsv() => Converts hex colors to HSV/HSB colors. #3", t => { + t.deepEqual(toHsv("ff4d4d"), "hsb(0, 70, 100)"); +}); + +test("#toHsv() => Converts HTML color names to HSV/HSB values. #1", t => { + t.deepEqual(toHsv("antiquewhite"), "hsb(34, 14, 98)"); +}); + +test("#toHsv() => Converts HTML color names to HSV/HSB values. #2", t => { + t.deepEqual(toHsv("aquamarine"), "hsb(160, 50, 100)"); +}); + +test("#toHsv() => Converts HSL colors to HSV/HSB colors. #1", t => { + t.deepEqual(toHsv("hsl(96, 41%, 78%)"), "hsb(97, 21, 87)"); +}); + +test("#toHsv() => Converts HSL colors to HSV/HSB colors. #2", t => { + t.deepEqual(toHsv("hsl(29, 87%, 50%)"), "hsb(29, 93, 93)"); +}); + +test("#toHsv() => Converts HSL colors to HSV/HSB colors. #3", t => { + t.deepEqual(toHsv("hsl(0, 87%, 0%)"), "hsb(0, 0, 0)"); +}); + +test("#randomHexColor() => always returns random valid hex colors", t => { + + // 5000 times should be more than enough! + const limit = 5000; + for (let i = 0; i < limit; i++) { + const hex = randomHexColor(); + if (!(/^#[0-9A-F]{6}$/i.test(hex))) t.fail(`invalid hex color generated: ${hex}`); + } + t.pass(); +}); + +test("#randomRgbColor() => always returns random valid RGB colors", t => { + + // 5000 times should be more than enough! + const limit = 5000; + for (let i = 0; i < limit; i++) { + const rgb = randomRgbColor(); + if (!(/^rgb\(\s*.+\s*,\s*.+\s*,\s*.+\s*\)$/.test(rgb))) t.fail(`invalid RGB color generated: ${rgb}`); + } + t.pass(); +}); + +test("#randomHslColor() => always returns random valid HSL colors", t => { + + // 5000 times should be more than enough! + const limit = 5000; + for (let i = 0; i < limit; i++) { + const hsl = randomHslColor(); + if (!(/^hsl\(\s*.+\s*,\s*.+%\s*,\s*.+%\s*\)$/.test(hsl))) t.fail(`invalid HSL color generated: ${hsl}`); + } + t.pass(); +}); + +test("#randomHsvColor() => always returns random valid HSV/HSB colors", t => { + + // 5000 times should be more than enough! + const limit = 5000; + for (let i = 0; i < limit; i++) { + const hsv = randomHsvColor(); + if (!(/^hsb\(\s*.+\s*,\s*.+\s*,\s*.+\s*\)$/.test(hsv))) t.fail(`invalid HSV/HSB color generated: ${hsv}`); + } + t.pass(); +}); + +test("#saturateHsl() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => saturateHsl(null)); +}); + +test("#saturateHsl() => Throws an exception if the first argument is not a proper HSL color.", t => { + t.throws(() => saturateHsl("hsl()")); +}); + +test("#saturateHsl() => Returns a saturated version of the given HSL color. #1", t => { + t.deepEqual(saturateHsl("hsl(50, 50, 80)"), "hsl(50, 55%, 80%)"); +}); + +test("#saturateHsl() => Returns a saturated version of the given HSL color. #2", t => { + t.deepEqual(saturateHsl("hsl(50, 50, 80)", 10), "hsl(50, 55%, 80%)"); +}); + +test("#saturateHsl() => Returns a saturated version of the given HSL color. #3", t => { + t.deepEqual(saturateHsl("hsl(50, 50, 80)", 100), "hsl(50, 100%, 80%)"); +}); + +test("#saturateHsl() => Returns a less saturated version of the given HSL color for negative values. #1", t => { + t.deepEqual(saturateHsl("hsl(50, 50, 80)", -10), "hsl(50, 45%, 80%)"); +}); + +test("#saturateHsl() => Returns a less saturated version of the given HSL color for negative values. #2", t => { + t.deepEqual(saturateHsl("hsl(50, 50, 80)", -95), "hsl(50, 2.5%, 80%)"); +}); + +test("#saturateHsl() => Never goes beyond 100%", t => { + t.deepEqual(saturateHsl("hsl(50, 50, 80)", 100000), "hsl(50, 100%, 80%)"); +}); + +test("#saturateHsl() => Never goes below 0%", t => { + t.deepEqual(saturateHsl("hsl(50, 50, 80)", -100000), "hsl(50, 0%, 80%)"); +}); + +test("#saturateHsla() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => saturateHsla(null)); +}); + +test("#saturateHsla() => Throws an exception if the first argument is not a proper HSLa color.", t => { + t.throws(() => saturateHsla("hsla()")); +}); + +test("#saturateHsla() => Returns a saturated version of the given HSLa color. #1", t => { + t.deepEqual(saturateHsla("hsla(50, 50, 80, 1)"), "hsla(50, 55%, 80%, 1)"); +}); + +test("#saturateHsla() => Returns a saturated version of the given HSLa color. #2", t => { + t.deepEqual(saturateHsla("hsla(50, 50, 80, 0.5)", 10), "hsla(50, 55%, 80%, 0.5)"); +}); + +test("#saturateHsla() => Returns a saturated version of the given HSLa color. #3", t => { + t.deepEqual(saturateHsla("hsla(50, 50, 80, 0)", 100), "hsla(50, 100%, 80%, 0)"); +}); + +test("#saturateHsla() => Returns a less saturated version of the given HSLa color for negative values. #1", t => { + t.deepEqual(saturateHsla("hsla(50, 50, 80, 1)", -10), "hsla(50, 45%, 80%, 1)"); +}); + +test("#saturateHsla() => Returns a less saturated version of the given HSLa color for negative values. #2", t => { + t.deepEqual(saturateHsla("hsla(50, 50, 80, 0.6)", -95), "hsla(50, 2.5%, 80%, 0.6)"); +}); + +test("#saturateHsla() => Never goes beyond 100%", t => { + t.deepEqual(saturateHsla("hsla(50, 50, 80, 1)", 100000), "hsla(50, 100%, 80%, 1)"); +}); + +test("#saturateHsla() => Never goes below 0%", t => { + t.deepEqual(saturateHsla("hsla(50, 50, 80, 0.2)", -100000), "hsla(50, 0%, 80%, 0.2)"); +}); + +test("#saturateRgb() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => saturateRgb(null)); +}); + +test("#saturateRgb() => Throws an exception if the first argument is not a proper RGB color.", t => { + t.throws(() => saturateRgb("rgb()")); +}); + +test("#saturateRgb() => Returns a saturated version of the given RGB color. #1", t => { + t.deepEqual(saturateRgb("rgb(23, 30, 129)"), "rgb(18, 25, 135)"); +}); + +test("#saturateRgb() => Returns a saturated version of the given RGB color. #2", t => { + t.deepEqual(saturateRgb("rgb(77, 147, 210)", 10), "rgb(69, 148, 217)"); +}); + +test("#saturateRgb() => Returns a less saturated version of the given RGB color for negative values. #1", t => { + t.deepEqual(saturateRgb("rgb(77, 147, 210)", -10), "rgb(82, 147, 203)"); +}); + +test("#saturateRgb() => Returns a less saturated version of the given RGB color for negative values. #2", t => { + t.deepEqual(saturateRgb("rgb(23, 30, 129)", -90), "rgb(71, 72, 82)"); +}); + +test("#saturateRgba() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => saturateRgba(null)); +}); + +test("#saturateRgba() => Throws an exception if the first argument is not a proper RGBa color.", t => { + t.throws(() => saturateRgba("rgba()")); +}); + +test("#saturateRgba() => Returns a saturated version of the given RGBa color. #1", t => { + t.deepEqual(saturateRgba("rgba(23, 30, 129, 1)"), "rgba(18, 25, 135, 1)"); +}); + +test("#saturateRgba() => Returns a saturated version of the given RGBa color. #2", t => { + t.deepEqual(saturateRgba("rgba(77, 147, 210, 0.5)", 10), "rgba(69, 148, 217, 0.5)"); +}); + +test("#saturateRgba() => Returns a less saturated version of the given RGBa color for negative values. #1", t => { + t.deepEqual(saturateRgba("rgba(77, 147, 210, 0)", -10), "rgba(82, 147, 203, 0)"); +}); + +test("#saturateRgba() => Returns a less saturated version of the given RGBa color for negative values. #2", t => { + t.deepEqual(saturateRgba("rgba(23, 30, 129, 0.2)", -90), "rgba(71, 72, 82, 0.2)"); +}); + +test("#saturateHex() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => saturateHex(null)); +}); + +test("#saturateHex() => Throws an exception if the first argument is not a proper hex color.", t => { + t.throws(() => saturateHex("foobar")); +}); + +test("#saturateHex() => Returns a saturated version of the given hex color. #1", t => { + t.deepEqual(saturateHex("#a3a3b3"), "#a2a2b4"); +}); + +test("#saturateHex() => Returns a saturated version of the given hex color. #2", t => { + t.deepEqual(saturateHex("#ddace3"), "#dfa8e6"); +}); + +test("#saturateHex() => Returns a less saturated version of the given hex color for negative values. #1", t => { + t.deepEqual(saturateHex("#a3a3b3", -10), "#a3a3b2"); +}); + +test("#saturateHex() => Returns a less saturated version of the given hex color for negative values. #2", t => { + t.deepEqual(saturateHex("#ddace3", -90), "#c9c4ca"); +}); + +test("#saturateHsv() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => saturateHsv(null)); +}); + +test("#saturateHsv() => Throws an exception if the first argument is not a proper HSV/HSB color.", t => { + t.throws(() => saturateHsv("hsb()")); +}); + +test("#saturateHsv() => Returns a saturated version of the given HSV/HSB color. #1", t => { + t.deepEqual(saturateHsv("hsb(290, 10, 79)"), "hsb(290, 11, 79)"); +}); + +test("#saturateHsv() => Returns a saturated version of the given HSV/HSB color. #2", t => { + t.deepEqual(saturateHsv("hsb(290, 10, 79)", 10), "hsb(290, 11, 79)"); +}); + +test("#saturateHsv() => Returns a saturated version of the given HSV/HSB color. #3", t => { + t.deepEqual(saturateHsv("hsb(290, 10, 79)", 90), "hsb(290, 19, 79)"); +}); + +test("#saturateHsv() => Returns a less saturated version of the given HSV/HSB color for negative values. #1", t => { + t.deepEqual(saturateHsv("hsb(120, 40, 80)", -10), "hsb(120, 36, 80)"); +}); + +test("#saturateHsv() => Returns a less saturated version of the given HSV/HSB color for negative values. #2", t => { + t.deepEqual(saturateHsv("hsb(120, 40, 80)", -90), "hsb(120, 4, 80)"); +}); + +test("#saturate() => Returns a saturated version of a given RGB color. #1", t => { + t.deepEqual(saturate("rgb(23, 30, 129)"), "rgb(18, 25, 135)"); +}); + +test("#saturate() => Returns a saturated version of a given RGB color. #2", t => { + t.deepEqual(saturate("rgb(77, 147, 210)", 10), "rgb(69, 148, 217)"); +}); + +test("#saturate() => Returns a less saturated version of a given RGB color for negative values. #1", t => { + t.deepEqual(saturate("rgb(77, 147, 210)", -10), "rgb(82, 147, 203)"); +}); + +test("#saturate() => Returns a less saturated version of a given RGB color for negative values. #2", t => { + t.deepEqual(saturate("rgb(23, 30, 129)", -90), "rgb(71, 72, 82)"); +}); + +test("#saturate() => Returns a saturated version of a given HSL color. #1", t => { + t.deepEqual(saturate("hsl(50, 50, 80)"), "hsl(50, 55%, 80%)"); +}); + +test("#saturate() => Returns a saturated version of a given HSL color. #2", t => { + t.deepEqual(saturate("hsl(50, 50, 80)", 10), "hsl(50, 55%, 80%)"); +}); + +test("#saturate() => Returns a saturated version of a given HSL color. #3", t => { + t.deepEqual(saturate("hsl(50, 50, 80)", 100), "hsl(50, 100%, 80%)"); +}); + +test("#saturate() => Returns a less saturated version of a given HSL color for negative values. #1", t => { + t.deepEqual(saturate("hsl(50, 50, 80)", -10), "hsl(50, 45%, 80%)"); +}); + +test("#saturate() => Returns a less saturated version of a given HSL color for negative values. #2", t => { + t.deepEqual(saturate("hsl(50, 50, 80)", -95), "hsl(50, 2.5%, 80%)"); +}); + +test("#saturate() => Never goes beyond 100% for HSL colors", t => { + t.deepEqual(saturate("hsl(50, 50, 80)", 100000), "hsl(50, 100%, 80%)"); +}); + +test("#saturate() => Never goes below 0% for HSL colors", t => { + t.deepEqual(saturate("hsl(50, 50, 80)", -100000), "hsl(50, 0%, 80%)"); +}); + +test("#saturate() => Returns a saturated version of a given HSV/HSB color. #1", t => { + t.deepEqual(saturate("hsb(290, 10, 79)"), "hsb(290, 11, 79)"); +}); + +test("#saturate() => Returns a saturated version of a given HSV/HSB color. #2", t => { + t.deepEqual(saturate("hsb(290, 10, 79)", 10), "hsb(290, 11, 79)"); +}); + +test("#saturate() => Returns a saturated version of a given HSV/HSB color. #3", t => { + t.deepEqual(saturate("hsb(290, 10, 79)", 90), "hsb(290, 19, 79)"); +}); + +test("#saturate() => Returns a less saturated version of a given HSV/HSB color for negative values. #1", t => { + t.deepEqual(saturate("hsb(120, 40, 80)", -10), "hsb(120, 36, 80)"); +}); + +test("#saturate() => Returns a less saturated version of a given HSV/HSB color for negative values. #2", t => { + t.deepEqual(saturate("hsb(120, 40, 80)", -90), "hsb(120, 4, 80)"); +}); + +test("#saturate() => Returns a saturated version of a given RGBa color. #1", t => { + t.deepEqual(saturate("rgba(23, 30, 129, 1)"), "rgba(18, 25, 135, 1)"); +}); + +test("#saturate() => Returns a saturated version of a given RGBa color. #2", t => { + t.deepEqual(saturate("rgba(77, 147, 210, 0.5)", 10), "rgba(69, 148, 217, 0.5)"); +}); + +test("#saturate() => Returns a less saturated version of a given RGBa color for negative values. #1", t => { + t.deepEqual(saturate("rgba(77, 147, 210, 0)", -10), "rgba(82, 147, 203, 0)"); +}); + +test("#saturate() => Returns a less saturated version of a given RGBa color for negative values. #2", t => { + t.deepEqual(saturate("rgba(23, 30, 129, 0.2)", -90), "rgba(71, 72, 82, 0.2)"); +}); + +test("#saturate() => Returns a saturated version of a given HSLa color. #1", t => { + t.deepEqual(saturate("hsla(50, 50, 80, 1)"), "hsla(50, 55%, 80%, 1)"); +}); + +test("#saturate() => Returns a saturated version of a given HSLa color. #2", t => { + t.deepEqual(saturate("hsla(50, 50, 80, 0.5)", 10), "hsla(50, 55%, 80%, 0.5)"); +}); + +test("#saturate() => Returns a saturated version of a given HSLa color. #3", t => { + t.deepEqual(saturate("hsla(50, 50, 80, 0)", 100), "hsla(50, 100%, 80%, 0)"); +}); + +test("#saturate() => Returns a less saturated version of a given HSLa color for negative values. #1", t => { + t.deepEqual(saturate("hsla(50, 50, 80, 1)", -10), "hsla(50, 45%, 80%, 1)"); +}); + +test("#saturate() => Returns a less saturated version of a given HSLa color for negative values. #2", t => { + t.deepEqual(saturate("hsla(50, 50, 80, 0.6)", -95), "hsla(50, 2.5%, 80%, 0.6)"); +}); + +test("#saturate() => Never goes beyond 100% for HSLa colors", t => { + t.deepEqual(saturate("hsla(50, 50, 80, 1)", 100000), "hsla(50, 100%, 80%, 1)"); +}); + +test("#saturate() => Never goes below 0% for HSLa colors", t => { + t.deepEqual(saturate("hsla(50, 50, 80, 0.2)", -100000), "hsla(50, 0%, 80%, 0.2)"); +}); + +test("#saturate() => Returns a saturated version of a given hex color. #1", t => { + t.deepEqual(saturate("#a3a3b3"), "#a2a2b4"); +}); + +test("#saturate() => Returns a saturated version of a given hex color. #2", t => { + t.deepEqual(saturate("#ddace3"), "#dfa8e6"); +}); + +test("#saturate() => Returns a less saturated version of a given hex color for negative values. #1", t => { + t.deepEqual(saturate("#a3a3b3", -10), "#a3a3b2"); +}); + +test("#saturate() => Returns a less saturated version of a given hex color for negative values. #2", t => { + t.deepEqual(saturate("#ddace3", -90), "#c9c4ca"); +}); + +test("#lightenHsl() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => lightenHsl(null)); +}); + +test("#lightenHsl() => Throws an exception if the first argument is not a proper HSL color.", t => { + t.throws(() => lightenHsl("hsl()")); +}); + +test("#lightenHsl() => Returns a lightened version of the given HSL color. #1", t => { + t.deepEqual(lightenHsl("hsl(50, 50, 80)"), "hsl(50, 50%, 88%)"); +}); + +test("#lightenHsl() => Returns a lightened version of the given HSL color. #2", t => { + t.deepEqual(lightenHsl("hsl(50, 50, 80)", 10), "hsl(50, 50%, 88%)"); +}); + +test("#lightenHsl() => Returns a lightened version of the given HSL color. #3", t => { + t.deepEqual(lightenHsl("hsl(50, 50, 10)", 100), "hsl(50, 50%, 20%)"); +}); + +test("#lightenHsl() => Returns a less lightened version of the given HSL color for negative values. #1", t => { + t.deepEqual(lightenHsl("hsl(50, 50, 80)", -10), "hsl(50, 50%, 72%)"); +}); + +test("#lightenHsl() => Returns a less lightened version of the given HSL color for negative values. #2", t => { + t.deepEqual(lightenHsl("hsl(50, 50, 80)", -95), "hsl(50, 50%, 4%)"); +}); + +test("#lightenHsl() => Never goes beyond 100%", t => { + t.deepEqual(lightenHsl("hsl(50, 50, 80)", 100000), "hsl(50, 50%, 100%)"); +}); + +test("#lightenHsl() => Never goes below 0%", t => { + t.deepEqual(lightenHsl("hsl(50, 50, 80)", -100000), "hsl(50, 50%, 0%)"); +}); + +test("#lightenHsla() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => lightenHsla(null)); +}); + +test("#lightenHsla() => Throws an exception if the first argument is not a proper HSLa color.", t => { + t.throws(() => lightenHsla("hsla()")); +}); + +test("#lightenHsla() => Returns a lightened version of the given HSLa color. #1", t => { + t.deepEqual(lightenHsla("hsla(50, 50, 80, 1)"), "hsla(50, 50%, 88%, 1)"); +}); + +test("#lightenHsla() => Returns a lightened version of the given HSLa color. #2", t => { + t.deepEqual(lightenHsla("hsla(50, 50, 80, 0.5)", 10), "hsla(50, 50%, 88%, 0.5)"); +}); + +test("#lightenHsla() => Returns a lightened version of the given HSLa color. #3", t => { + t.deepEqual(lightenHsla("hsla(50, 50, 10, 0)", 100), "hsla(50, 50%, 20%, 0)"); +}); + +test("#lightenHsla() => Returns a less lightened version of the given HSLa color for negative values. #1", t => { + t.deepEqual(lightenHsla("hsla(50, 50, 80, 1)", -10), "hsla(50, 50%, 72%, 1)"); +}); + +test("#lightenHsla() => Returns a less lightened version of the given HSLa color for negative values. #2", t => { + t.deepEqual(lightenHsla("hsla(50, 50, 80, 0.6)", -95), "hsla(50, 50%, 4%, 0.6)"); +}); + +test("#lightenHsla() => Never goes beyond 100%", t => { + t.deepEqual(lightenHsla("hsla(50, 50, 80, 1)", 100000), "hsla(50, 50%, 100%, 1)"); +}); + +test("#lightenHsla() => Never goes below 0%", t => { + t.deepEqual(lightenHsla("hsla(50, 50, 80, 0.2)", -100000), "hsla(50, 50%, 0%, 0.2)"); +}); + +test("#lightenRgb() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => lightenRgb(null)); +}); + +test("#lightenRgb() => Throws an exception if the first argument is not a proper RGB color.", t => { + t.throws(() => lightenRgb("rgb()")); +}); + +test("#lightenRgb() => Returns a lightened version of the given RGB color. #1", t => { + t.deepEqual(lightenRgb("rgb(23, 30, 129)"), "rgb(25, 33, 143)"); +}); + +test("#lightenRgb() => Returns a lightened version of the given RGB color. #2", t => { + t.deepEqual(lightenRgb("rgb(231, 175, 212)", 10), "rgb(241, 208, 230)"); +}); + +test("#lightenRgb() => Returns a less lightened version of the given RGB color for negative values. #1", t => { + t.deepEqual(lightenRgb("rgb(231, 175, 212)", -10), "rgb(222, 145, 196)"); +}); + +test("#lightenRgb() => Returns a less lightened version of the given RGB color for negative values. #2", t => { + t.deepEqual(lightenRgb("rgb(23, 30, 129)", -90), "rgb(2, 3, 13)"); +}); + +test("#lightenRgba() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => lightenRgba(null)); +}); + +test("#lightenRgba() => Throws an exception if the first argument is not a proper RGBa color.", t => { + t.throws(() => lightenRgba("rgba()")); +}); + +test("#lightenRgba() => Returns a lightened version of the given RGBa color. #1", t => { + t.deepEqual(lightenRgba("rgba(23, 30, 129, 1)"), "rgba(25, 33, 143, 1)"); +}); + +test("#lightenRgba() => Returns a lightened version of the given RGBa color. #2", t => { + t.deepEqual(lightenRgba("rgba(231, 175, 212, 0.5)", 10), "rgba(241, 208, 230, 0.5)"); +}); + +test("#lightenRgba() => Returns a less lightened version of the given RGBa color for negative values. #1", t => { + t.deepEqual(lightenRgba("rgba(231, 175, 212, 0)", -10), "rgba(222, 145, 196, 0)"); +}); + +test("#lightenRgba() => Returns a less lightened version of the given RGBa color for negative values. #2", t => { + t.deepEqual(lightenRgba("rgba(23, 30, 129, 0.2)", -90), "rgba(2, 3, 13, 0.2)"); +}); + +test("#lightenHex() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => lightenHex(null)); +}); + +test("#lightenHex() => Throws an exception if the first argument is not a proper hex color.", t => { + t.throws(() => lightenHex("foobar")); +}); + +test("#lightenHex() => Returns a lightened version of the given hex color. #1", t => { + t.deepEqual(lightenHex("#af0"), "#b2ff1a"); +}); + +test("#lightenHex() => Returns a lightened version of the given hex color. #2", t => { + t.deepEqual(lightenHex("#ddaced"), "#eacdf4"); +}); + +test("#lightenHex() => Returns a less lightened version of the given hex color for negative values. #1", t => { + t.deepEqual(lightenHex("#af0", -10), "#99e600"); +}); + +test("#lightenHex() => Returns a less lightened version of the given hex color for negative values. #2", t => { + t.deepEqual(lightenHex("#ddaced", -90), "#1b0721"); +}); + +test("#lightenHsv() => Throws an exception if the first argument is not of type 'string'", t => { + // @ts-ignore + t.throws(() => lightenHsv(null)); +}); + +test("#lightenHsv() => Throws an exception if the first argument is not a proper HSV/HSB color.", t => { + t.throws(() => lightenHsv("hsb()")); +}); + +test("#lightenHsv() => Returns a lightened version of the given HSV/HSB color. #1", t => { + t.deepEqual(lightenHsv("hsb(290, 10, 84)"), "hsb(291, 6, 91)"); +}); + +test("#lightenHsv() => Returns a lightened version of the given HSV/HSB color. #2", t => { + t.deepEqual(lightenHsv("hsb(359, 12, 53)", 10), "hsb(0, 10, 58)"); +}); + +test("#lightenHsv() => Returns a lightened version of the given HSV/HSB color. #3", t => { + t.deepEqual(lightenHsv("hsb(290, 10, 32)", 90), "hsb(286, 9, 60)"); +}); + +test("#lightenHsv() => Returns a less lightened version of the given HSV/HSB color for negative values. #1", t => { + t.deepEqual(lightenHsv("hsb(290, 10, 84)", -10), "hsb(292, 15, 78)"); +}); + +test("#lightenHsv() => Returns a less lightened version of the given HSV/HSB color for negative values. #2", t => { + t.deepEqual(lightenHsv("hsb(359, 12, 53)", -90), "hsb(0, 14, 5)"); +}); + +test("#lighten() => Returns a lightened version of a given RGBa color. #1", t => { + t.deepEqual(lighten("rgba(23, 30, 129, 1)"), "rgba(25, 33, 143, 1)"); +}); + +test("#lighten() => Returns a lightened version of a given RGBa color. #2", t => { + t.deepEqual(lighten("rgba(231, 175, 212, 0.5)", 10), "rgba(241, 208, 230, 0.5)"); +}); + +test("#lighten() => Returns a less lightened version of a given RGBa color for negative values. #1", t => { + t.deepEqual(lighten("rgba(231, 175, 212, 0)", -10), "rgba(222, 145, 196, 0)"); +}); + +test("#lighten() => Returns a less lightened version of a given RGBa color for negative values. #2", t => { + t.deepEqual(lighten("rgba(23, 30, 129, 0.2)", -90), "rgba(2, 3, 13, 0.2)"); +}); + +test("#lighten() => Returns a lightened version of a given HSLa color. #1", t => { + t.deepEqual(lighten("hsla(50, 50, 80, 1)"), "hsla(50, 50%, 88%, 1)"); +}); + +test("#lighten() => Returns a lightened version of a given HSLa color. #2", t => { + t.deepEqual(lighten("hsla(50, 50, 80, 0.5)", 10), "hsla(50, 50%, 88%, 0.5)"); +}); + +test("#lighten() => Returns a lightened version of a given HSLa color. #3", t => { + t.deepEqual(lighten("hsla(50, 50, 10, 0)", 100), "hsla(50, 50%, 20%, 0)"); +}); + +test("#lighten() => Returns a less lightened version of a given HSLa color for negative values. #1", t => { + t.deepEqual(lighten("hsla(50, 50, 80, 1)", -10), "hsla(50, 50%, 72%, 1)"); +}); + +test("#lighten() => Returns a less lightened version of a given HSLa color for negative values. #2", t => { + t.deepEqual(lighten("hsla(50, 50, 80, 0.6)", -95), "hsla(50, 50%, 4%, 0.6)"); +}); + +test("#lighten() => Never goes beyond 100% for HSLa values", t => { + t.deepEqual(lighten("hsla(50, 50, 80, 1)", 100000), "hsla(50, 50%, 100%, 1)"); +}); + +test("#lighten() => Never goes below 0% for HSLa values", t => { + t.deepEqual(lighten("hsla(50, 50, 80, 0.2)", -100000), "hsla(50, 50%, 0%, 0.2)"); +}); + +test("#lighten() => Returns a lightened version of a given RGB color. #1", t => { + t.deepEqual(lighten("rgb(23, 30, 129)"), "rgb(25, 33, 143)"); +}); + +test("#lighten() => Returns a lightened version of a given RGB color. #2", t => { + t.deepEqual(lighten("rgb(231, 175, 212)", 10), "rgb(241, 208, 230)"); +}); + +test("#lighten() => Returns a less lightened version of a given RGB color for negative values. #1", t => { + t.deepEqual(lighten("rgb(231, 175, 212)", -10), "rgb(222, 145, 196)"); +}); + +test("#lighten() => Returns a less lightened version of a given RGB color for negative values. #2", t => { + t.deepEqual(lighten("rgb(23, 30, 129)", -90), "rgb(2, 3, 13)"); +}); + +test("#lighten() => Returns a lightened version of a given HSL color. #1", t => { + t.deepEqual(lighten("hsl(50, 50, 80)"), "hsl(50, 50%, 88%)"); +}); + +test("#lighten() => Returns a lightened version of a given HSL color. #2", t => { + t.deepEqual(lighten("hsl(50, 50, 80)", 10), "hsl(50, 50%, 88%)"); +}); + +test("#lighten() => Returns a lightened version of a given HSL color. #3", t => { + t.deepEqual(lighten("hsl(50, 50, 10)", 100), "hsl(50, 50%, 20%)"); +}); + +test("#lighten() => Returns a less lightened version of a given HSL color for negative values. #1", t => { + t.deepEqual(lighten("hsl(50, 50, 80)", -10), "hsl(50, 50%, 72%)"); +}); + +test("#lighten() => Returns a less lightened version of a given HSL color for negative values. #2", t => { + t.deepEqual(lighten("hsl(50, 50, 80)", -95), "hsl(50, 50%, 4%)"); +}); + +test("#lighten() => Never goes beyond 100% for HSL values", t => { + t.deepEqual(lighten("hsl(50, 50, 80)", 100000), "hsl(50, 50%, 100%)"); +}); + +test("#lighten() => Never goes below 0% for HSL values", t => { + t.deepEqual(lighten("hsl(50, 50, 80)", -100000), "hsl(50, 50%, 0%)"); +}); + +test("#lighten() => Returns a lightened version of a given HSV/HSB color. #1", t => { + t.deepEqual(lighten("hsb(290, 10, 84)"), "hsb(291, 6, 91)"); +}); + +test("#lighten() => Returns a lightened version of a given HSV/HSB color. #2", t => { + t.deepEqual(lighten("hsb(359, 12, 53)", 10), "hsb(0, 10, 58)"); +}); + +test("#lighten() => Returns a lightened version of a given HSV/HSB color. #3", t => { + t.deepEqual(lighten("hsb(290, 10, 32)", 90), "hsb(286, 9, 60)"); +}); + +test("#lighten() => Returns a less lightened version of a given HSV/HSB color for negative values. #1", t => { + t.deepEqual(lighten("hsb(290, 10, 84)", -10), "hsb(292, 15, 78)"); +}); + +test("#lighten() => Returns a less lightened version of a given HSV/HSB color for negative values. #2", t => { + t.deepEqual(lighten("hsb(359, 12, 53)", -90), "hsb(0, 14, 5)"); +}); + +test("#lighten() => Returns a lightened version of a given hex color. #1", t => { + t.deepEqual(lighten("#af0"), "#b2ff1a"); +}); + +test("#lighten() => Returns a lightened version of a given hex color. #2", t => { + t.deepEqual(lighten("#ddaced"), "#eacdf4"); +}); + +test("#lighten() => Returns a less lightened version of a given hex color for negative values. #1", t => { + t.deepEqual(lighten("#af0", -10), "#99e600"); +}); + +test("#lighten() => Returns a less lightened version of a given hex color for negative values. #2", t => { + t.deepEqual(lighten("#ddaced", -90), "#1b0721"); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #1", t => { + t.true(isLight("#4286f4")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #2", t => { + t.true(isLight("#165ac9")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #3", t => { + t.false(isLight("#0c3e8e")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #4", t => { + t.true(isLight("rgb(159, 188, 234)")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #5", t => { + t.true(isLight("rgb(124, 134, 150)")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #6", t => { + t.false(isLight("rgb(85, 92, 104)")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #7", t => { + t.true(isLight("hsl(0, 0%, 100%)")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #8", t => { + t.true(isLight("hsl(300, 100%, 50%)")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #9", t => { + t.false(isLight("hsl(40, 100%, 30%)")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #10", t => { + t.true(isLight("darkseagreen")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #11", t => { + t.true(isLight("floralwhite")); +}); + +test("#isLight() => correctly determines bright from dark colors, no matter the input. #12", t => { + t.false(isLight("darkslateblue")); +}); \ No newline at end of file diff --git a/tsconfig.dist.json b/tsconfig.dist.json new file mode 100644 index 0000000..66e7898 --- /dev/null +++ b/tsconfig.dist.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "include": [ + "src/**/*.*" + ], + "compilerOptions": { + "declaration": true + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..8b6fc7b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "./node_modules/@wessberg/ts-config/tsconfig/tsconfig.json" +} \ No newline at end of file diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..bc52a83 --- /dev/null +++ b/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "./node_modules/@wessberg/ts-config/tslint/tslint.json" +} \ No newline at end of file