From ad9a0ab78e6223551fe9bb1003e7e8c735b294a5 Mon Sep 17 00:00:00 2001 From: Marina Miyaoka Date: Sat, 19 Oct 2019 00:42:21 +0300 Subject: [PATCH] chore: Initial commit. --- .gitignore | 3 ++ .npmignore | 3 ++ LICENSE | 21 +++++++++ README.md | 36 +++++++++++++++ index.d.ts | 11 +++++ index.js | 106 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 11 +++++ prettier.config.js | 21 +++++++++ tsconfig.json | 11 +++++ yarn.lock | 4 ++ 10 files changed, 227 insertions(+) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.d.ts create mode 100644 index.js create mode 100644 package.json create mode 100644 prettier.config.js create mode 100644 tsconfig.json create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c15c11 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +node_modules/ +*.iml diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..9c15c11 --- /dev/null +++ b/.npmignore @@ -0,0 +1,3 @@ +.idea/ +node_modules/ +*.iml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..533807c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 ripreact + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a08d595 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# @ripreact/hsl + +> Minimal (≈560 bytes minigzip) [HSLᵤᵥ](http://hsluv.org) implementation with +> TypeScript support. + +```shell script +yarn add @ripreact/hsl +``` + +## Usage + +```javascript +// const { hsl } = require('@ripreact/hsl'); +import { hsl } from '@ripreact/hsl'; + +console.log(hsl(0, 100, 50, 0.5)); // → `rgba(234,0,100,0.5)` +``` + +## Environment + +This module uses some ES6+ features: + +- arrow functions, +- array and object destructuring, +- default parameters, +- exponentiation operator, +- template literals, +- `Array#{map,reduce}`. + +If you need old environments support, you should transpile this module and add +polyfills when needed. When using webpack and Babel, you can add +`include: [require.resolve('@ripreact/hsl')]` to Babel’s rule. + +## License + +MIT diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..1bb27aa --- /dev/null +++ b/index.d.ts @@ -0,0 +1,11 @@ +declare module '@ripreact/hsl' { + /** + * Minimal HSLᵤᵥ implementation. Returns `rgba(r, g, b, a)` string. + * + * @param h HSLᵤᵥ hue. + * @param s HSLᵤᵥ saturation. + * @param l HSLᵤᵥ lightness. + * @param a Alpha. + */ + export function hsl(h: number, s: number, l: number, a?: number): string; +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..1c058cf --- /dev/null +++ b/index.js @@ -0,0 +1,106 @@ +/** + * Minimal HSLᵤᵥ implementation. Returns `rgba(r,g,b,a)` string. + * + * @param {number} h HSLᵤᵥ hue. + * @param {number} s HSLᵤᵥ saturation. + * @param {number} l HSLᵤᵥ lightness. + * @param {number} [a] Alpha. + */ +module.exports.hsl = (h, s, l, a = 1) => { + /** @type {number} */ + let j; + + /** @type {number} */ + let bottom; + + /** @type {number} */ + let length; + + /** @type {number[][]} */ + const M = [ + [3.240969941904521, -1.537383177570093, -0.498610760293], + [-0.96924363628087, 1.87596750150772, 0.041555057407175], + [0.055630079696993, -0.20397695888897, 1.056971514242878], + ]; + + /** + * @param {number} c + * @returns {number} + */ + const fromLinear = c => (c <= 0.0031308 ? 12.92 * c : 1.055 * c ** 0.416666666666666685 - 0.055); + + const { sin, cos, min } = Math; + + /** @type {number} */ + const hrad = (h * Math.PI) / 180; + + /** @type {number[][]} */ + const bounds = []; + + /** @type {number} */ + const sub1 = (l + 16) ** 3 / 1560896; + + /** @type {number} */ + const sub2 = sub1 > 0.0088564516 ? sub1 : l / 903.2962962; + + const _ = M.map( + /** + * @param {number} a + * @param {number} b + * @param {number} c + */ + ([a, b, c]) => { + for (j = 0; j < 2; j++) { + bottom = (632260 * c - 126452 * b) * sub2 + 126452 * j; + + bounds.push([ + ((284517 * a - 94839 * c) * sub2) / bottom, + ((838422 * c + 769860 * b + 731718 * a) * l * sub2 - 769860 * j * l) / bottom, + ]); + } + }, + ); + + /** @type {number} */ + const c = bounds.reduce( + /** + * @param {number} result + * @param {number} a + * @param {number} b + * @returns {number} + */ + (result, [a, b]) => { + length = b / (sin(hrad) - a * cos(hrad)); + + return length >= 0 ? min(result, (length / 100) * s) : result; + }, + Infinity, + ); + + /** @type {number} */ + const varU = (cos(hrad) * c) / (13 * l) + 0.19783000664283; + + /** @type {number} */ + const varV = (sin(hrad) * c) / (13 * l) + 0.46831999493879; + + /** @type {number} */ + const y = l <= 8 ? l / 903.2962962 : ((l + 16) / 116) ** 3; + + /** @type {number} */ + const x = -(9 * y * varU) / ((varU - 4) * varV - varU * varV); + + return `rgba(${M.map( + /** + * @param {number} a + * @param {number} b + * @param {number} c + * @returns {number} + */ + ([a, b, c]) => { + return Math.round( + Math.max(0, min(fromLinear(a * x + b * y + (c * (9 * y - 15 * varV * y - varV * x)) / (3 * varV)), 1)) * + 255, + ); + }, + )},${a})`; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..abc0558 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "@ripreact/hsl", + "version": "1.0.0", + "description": "Minimal HSLᵤᵥ implementation.", + "main": "index.js", + "types": "index.d.ts", + "repository": "https://github.com/ripreact/hsl", + "author": "Marina Miyaoka (https://twitter.com/miyaokamarina)", + "license": "MIT", + "devDependencies": {} +} diff --git a/prettier.config.js b/prettier.config.js new file mode 100644 index 0000000..8e7dc04 --- /dev/null +++ b/prettier.config.js @@ -0,0 +1,21 @@ +module.exports = { + endOfLine: 'lf', + htmlWhitespaceSensitivity: 'ignore', + jsxSingleQuote: true, + printWidth: 120, + quoteProps: 'consistent', + semi: true, + singleQuote: true, + tabWidth: 4, + trailingComma: 'all', + arrowParens: 'avoid', + proseWrap: 'always', + overrides: [ + { + files: ['*.md'], + options: { + printWidth: 80 + } + } + ] +}; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..0693718 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es3", + "module": "commonjs", + "strict": true, + "moduleResolution": "node", + "esModuleInterop": true, + "noUnusedLocals": true, + "noUnusedParameters": true + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..fb57ccd --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + +