@palett/fluo
Advanced tools
Comparing version 0.2.7 to 0.2.9
@@ -13,4 +13,148 @@ 'use strict'; | ||
const max = (a, b) => a > b ? a : b; | ||
const min = (a, b) => a < b ? a : b; | ||
const nullish = x => x === null || x === void 0; | ||
/** | ||
* | ||
* @param {number} x | ||
* @returns {number} | ||
*/ | ||
/** | ||
* | ||
* @param {number} x | ||
* @returns {number} | ||
*/ | ||
const round = x => x + (x > 0 ? 0.5 : -0.5) << 0; | ||
/** | ||
* | ||
* @param {number} n | ||
* @param {number} h | ||
* @param {number} a | ||
* @param {number} l | ||
* @returns {number} | ||
*/ | ||
const hf = (n, h, a, l) => { | ||
const k = (n + h / 30) % 12; | ||
return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); | ||
}; | ||
/** | ||
* | ||
* @param {number} h | ||
* @param {number} s | ||
* @param {number} l | ||
* @returns {number[]} | ||
*/ | ||
function hslToRgb([h, s, l]) { | ||
s /= 100; | ||
l /= 100; | ||
const a = s * Math.min(l, 1 - l), | ||
r = hf(0, h, a, l), | ||
g = hf(8, h, a, l), | ||
b = hf(4, h, a, l); | ||
return [round(r * 0xFF), round(g * 0xFF), round(b * 0xFF)]; // return [r * 0xFF & 0xFF, g * 0xFF & 0xFF, b * 0xFF & 0xFF] | ||
} | ||
const ESC = '\u001b'; | ||
const L = ESC + '['; | ||
const R = 'm'; | ||
const SC = ';'; | ||
const FORE = '38;2'; | ||
const CLR_FORE = '39'; | ||
// black: 30, | ||
// Red: 31, | ||
// Green: 32, | ||
// Yellow: 33, | ||
// Blue: 34, | ||
// magenta: 35, | ||
// Cyan: 36, | ||
// white: 37, | ||
// Grey: 90, | ||
// } | ||
const BOLD = '1'; | ||
const ITALIC = '3'; | ||
const UNDERLINE = '4'; | ||
const INVERSE = '7'; | ||
const CLR_BOLD = '22'; | ||
const CLR_ITALIC = '23'; | ||
const CLR_UNDERLINE = '24'; | ||
const CLR_INVERSE = '27'; | ||
const Effects = { | ||
bold: [BOLD, CLR_BOLD], | ||
italic: [ITALIC, CLR_ITALIC], | ||
underline: [UNDERLINE, CLR_UNDERLINE], | ||
inverse: [INVERSE, CLR_INVERSE] | ||
}; | ||
/** | ||
* | ||
* @param {string} code | ||
* @returns {string} | ||
*/ | ||
const brt = code => L + code + R; | ||
/** | ||
* | ||
* @param {number[]} rgb - array of three integers, each from 0 to 255 | ||
* @returns {string} | ||
*/ | ||
const rgbToAnsi = rgb => FORE + SC + rgb[0] + SC + rgb[1] + SC + rgb[2]; | ||
/** | ||
* | ||
* @param {string} tx | ||
* @returns {string} | ||
*/ | ||
function codedDyer(tx) { | ||
const { | ||
h, | ||
t | ||
} = this; | ||
return brt(h) + tx + brt(t); | ||
} | ||
const parseEffects = effects => { | ||
let h = '', | ||
t = ''; | ||
if (effects.length) { | ||
let l, r; | ||
for (let e of effects) if (e in Effects && ([l, r] = Effects[e])) h += SC + l, t += SC + r; | ||
} | ||
return { | ||
h, | ||
t | ||
}; | ||
}; | ||
/*** | ||
* | ||
* @param {number[]} rgb | ||
* @param {...string} [effects] | ||
* @returns {function(string):string} | ||
*/ | ||
const Dye = (rgb, ...effects) => { | ||
const config = parseEffects(effects); | ||
config.h += SC + rgbToAnsi(rgb), config.t += SC + CLR_FORE; | ||
return codedDyer.bind(config); | ||
}; | ||
/** | ||
* | ||
* @param {Object} bound | ||
@@ -22,2 +166,3 @@ * @param {number} [bound.min] - if min: if dif, return {min,dif}; if max, return calculated {min,dif} | ||
*/ | ||
const boundToLeap = bound => { | ||
@@ -30,8 +175,8 @@ let { | ||
if (pass(min)) { | ||
if (pass(dif)) return { | ||
if (!nullish(min)) { | ||
if (!nullish(dif)) return { | ||
min, | ||
dif | ||
}; | ||
if (pass(max)) return { | ||
if (!nullish(max)) return { | ||
min, | ||
@@ -42,4 +187,4 @@ dif: max - min | ||
if (pass(dif)) { | ||
if (pass(max)) return { | ||
if (!nullish(dif)) { | ||
if (!nullish(max)) return { | ||
min: max - dif, | ||
@@ -54,3 +199,3 @@ dif | ||
if (pass(max)) return { | ||
if (!nullish(max)) return { | ||
min: 0, | ||
@@ -64,34 +209,79 @@ dif: max | ||
}; | ||
/** | ||
* Create a dye from a hsl array | ||
* @param {[number,number,number]} hsl | ||
* @returns {function} | ||
*/ | ||
const pass = x => x !== void 0 && x !== null; // const MIN = 'min', DIF = 'dif', MAX = 'max' | ||
// export const boundToLeapDev = b => { | ||
// let min, max, dif | ||
// if (MIN in b) { | ||
// if (DIF in b) return { min, dif } = b, { min, dif } | ||
// if (MAX in b) return { min, max } = b, { min, dif: max - min } | ||
// } | ||
// if (DIF in b) { | ||
// if (MAX in b) return { max, dif } = b, { min: max - dif, dif } | ||
// return { dif } = b, { min: 0, dif } | ||
// } | ||
// if (MAX in b) return { max } = b, { min: 0, dif: max } | ||
// return { min: 0, dif: 0 } | ||
// } | ||
const FluoNumber = (bound, preset = presets.PLANET, colorant = true) => { | ||
var _cleap$min; | ||
const hslToDye = hsl => { | ||
var _ref, _hsl; | ||
return _ref = (_hsl = hsl, hslToRgb(_hsl)), Dye(_ref); | ||
}; | ||
const leverage = ([h, s, l], base) => [h / base, s / base, l / base]; | ||
const scale = (x, min$1, lever, base, ceil) => min((max(x, min$1) - min$1) * lever + base, ceil); | ||
const projector = function (x) { | ||
var _ref; | ||
const { | ||
min: m, | ||
lever: [rH, rS, rL], | ||
base: [mH, mS, mL] | ||
} = this; | ||
return _ref = [scale(x, m, rH, mH, 360), scale(x, m, rS, mS, 100), scale(x, m, rL, mL, 100)], hslToDye(_ref); | ||
}; | ||
/** | ||
* | ||
* @param {{[min]:number,[max]:number,[dif]:number}} bound | ||
* @param {{max:*,min:*}} preset | ||
* @returns {function(*):function} | ||
* @constructor | ||
*/ | ||
const Projector = (bound, preset) => { | ||
var _bound, _preset; | ||
if (!bound) return void 0; | ||
bound = (_bound = bound, boundToLeap(_bound)); | ||
/** @type {{min:number[],dif:number[]}} */ | ||
const leap = (_preset = preset, utilFluo.presetToLeap(_preset)); | ||
if (!bound.dif) { | ||
const dye = hslToDye(leap.min); | ||
return () => dye; | ||
} | ||
return projector.bind({ | ||
min: bound.min, | ||
lever: leverage(leap.dif, bound.dif), | ||
base: leap.min | ||
}); | ||
}; | ||
const Colorant = (bound, preset = presets.PLANET) => { | ||
const vleap = boundToLeap(bound), | ||
cleap = utilFluo.presetToLeap(preset), | ||
prime = utilFluo.presetToFlat(preset); | ||
let dye; | ||
return vleap.dif && cleap.dif.some(n => !!n) ? (dye = utilFluo.BlendDye(vleap, cleap), colorant ? x => numStrict.isNumeric(x) ? dye(x) : prime : x => { | ||
let dye = Projector(vleap, preset); | ||
return x => numStrict.isNumeric(x) ? dye(x) : prime; | ||
}; | ||
const Pigment = (bound, preset = presets.PLANET) => { | ||
const vleap = boundToLeap(bound), | ||
prime = utilFluo.presetToFlat(preset); | ||
let dye = Projector(vleap, preset); | ||
return x => { | ||
var _x, _x2; | ||
return numStrict.isNumeric(x) ? (_x = x, dye(x)(_x)) : (_x2 = x, prime(_x2)); | ||
}) : (dye = (_cleap$min = cleap.min, utilFluo.hslToDye(_cleap$min)), colorant ? x => numStrict.isNumeric(x) ? dye : prime : x => { | ||
var _x3, _x4; | ||
}; | ||
}; | ||
return numStrict.isNumeric(x) ? (_x3 = x, dye(_x3)) : (_x4 = x, prime(_x4)); | ||
}); | ||
const FluoNumber = (bound, preset = presets.PLANET, colorant = true) => { | ||
return colorant ? Colorant(bound, preset) : Pigment(bound, preset); | ||
}; | ||
@@ -98,0 +288,0 @@ |
@@ -5,8 +5,152 @@ export { fluoVector } from '@palett/fluo-vector'; | ||
import { PLANET } from '@palett/presets'; | ||
import { presetToLeap, presetToFlat, BlendDye, hslToDye } from '@palett/util-fluo'; | ||
import { presetToFlat, presetToLeap } from '@palett/util-fluo'; | ||
import { isNumeric } from '@typen/num-strict'; | ||
export { COLUMNWISE, POINTWISE, ROWWISE } from '@vect/enum-matrix-directions'; | ||
const max = (a, b) => a > b ? a : b; | ||
const min = (a, b) => a < b ? a : b; | ||
const nullish = x => x === null || x === void 0; | ||
/** | ||
* | ||
* @param {number} x | ||
* @returns {number} | ||
*/ | ||
/** | ||
* | ||
* @param {number} x | ||
* @returns {number} | ||
*/ | ||
const round = x => x + (x > 0 ? 0.5 : -0.5) << 0; | ||
/** | ||
* | ||
* @param {number} n | ||
* @param {number} h | ||
* @param {number} a | ||
* @param {number} l | ||
* @returns {number} | ||
*/ | ||
const hf = (n, h, a, l) => { | ||
const k = (n + h / 30) % 12; | ||
return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); | ||
}; | ||
/** | ||
* | ||
* @param {number} h | ||
* @param {number} s | ||
* @param {number} l | ||
* @returns {number[]} | ||
*/ | ||
function hslToRgb([h, s, l]) { | ||
s /= 100; | ||
l /= 100; | ||
const a = s * Math.min(l, 1 - l), | ||
r = hf(0, h, a, l), | ||
g = hf(8, h, a, l), | ||
b = hf(4, h, a, l); | ||
return [round(r * 0xFF), round(g * 0xFF), round(b * 0xFF)]; // return [r * 0xFF & 0xFF, g * 0xFF & 0xFF, b * 0xFF & 0xFF] | ||
} | ||
const ESC = '\u001b'; | ||
const L = ESC + '['; | ||
const R = 'm'; | ||
const SC = ';'; | ||
const FORE = '38;2'; | ||
const CLR_FORE = '39'; | ||
// black: 30, | ||
// Red: 31, | ||
// Green: 32, | ||
// Yellow: 33, | ||
// Blue: 34, | ||
// magenta: 35, | ||
// Cyan: 36, | ||
// white: 37, | ||
// Grey: 90, | ||
// } | ||
const BOLD = '1'; | ||
const ITALIC = '3'; | ||
const UNDERLINE = '4'; | ||
const INVERSE = '7'; | ||
const CLR_BOLD = '22'; | ||
const CLR_ITALIC = '23'; | ||
const CLR_UNDERLINE = '24'; | ||
const CLR_INVERSE = '27'; | ||
const Effects = { | ||
bold: [BOLD, CLR_BOLD], | ||
italic: [ITALIC, CLR_ITALIC], | ||
underline: [UNDERLINE, CLR_UNDERLINE], | ||
inverse: [INVERSE, CLR_INVERSE] | ||
}; | ||
/** | ||
* | ||
* @param {string} code | ||
* @returns {string} | ||
*/ | ||
const brt = code => L + code + R; | ||
/** | ||
* | ||
* @param {number[]} rgb - array of three integers, each from 0 to 255 | ||
* @returns {string} | ||
*/ | ||
const rgbToAnsi = rgb => FORE + SC + rgb[0] + SC + rgb[1] + SC + rgb[2]; | ||
/** | ||
* | ||
* @param {string} tx | ||
* @returns {string} | ||
*/ | ||
function codedDyer(tx) { | ||
const { | ||
h, | ||
t | ||
} = this; | ||
return brt(h) + tx + brt(t); | ||
} | ||
const parseEffects = effects => { | ||
let h = '', | ||
t = ''; | ||
if (effects.length) { | ||
let l, r; | ||
for (let e of effects) if (e in Effects && ([l, r] = Effects[e])) h += SC + l, t += SC + r; | ||
} | ||
return { | ||
h, | ||
t | ||
}; | ||
}; | ||
/*** | ||
* | ||
* @param {number[]} rgb | ||
* @param {...string} [effects] | ||
* @returns {function(string):string} | ||
*/ | ||
const Dye = (rgb, ...effects) => { | ||
const config = parseEffects(effects); | ||
config.h += SC + rgbToAnsi(rgb), config.t += SC + CLR_FORE; | ||
return codedDyer.bind(config); | ||
}; | ||
/** | ||
* | ||
* @param {Object} bound | ||
@@ -18,2 +162,3 @@ * @param {number} [bound.min] - if min: if dif, return {min,dif}; if max, return calculated {min,dif} | ||
*/ | ||
const boundToLeap = bound => { | ||
@@ -26,8 +171,8 @@ let { | ||
if (pass(min)) { | ||
if (pass(dif)) return { | ||
if (!nullish(min)) { | ||
if (!nullish(dif)) return { | ||
min, | ||
dif | ||
}; | ||
if (pass(max)) return { | ||
if (!nullish(max)) return { | ||
min, | ||
@@ -38,4 +183,4 @@ dif: max - min | ||
if (pass(dif)) { | ||
if (pass(max)) return { | ||
if (!nullish(dif)) { | ||
if (!nullish(max)) return { | ||
min: max - dif, | ||
@@ -50,3 +195,3 @@ dif | ||
if (pass(max)) return { | ||
if (!nullish(max)) return { | ||
min: 0, | ||
@@ -60,36 +205,81 @@ dif: max | ||
}; | ||
/** | ||
* Create a dye from a hsl array | ||
* @param {[number,number,number]} hsl | ||
* @returns {function} | ||
*/ | ||
const pass = x => x !== void 0 && x !== null; // const MIN = 'min', DIF = 'dif', MAX = 'max' | ||
// export const boundToLeapDev = b => { | ||
// let min, max, dif | ||
// if (MIN in b) { | ||
// if (DIF in b) return { min, dif } = b, { min, dif } | ||
// if (MAX in b) return { min, max } = b, { min, dif: max - min } | ||
// } | ||
// if (DIF in b) { | ||
// if (MAX in b) return { max, dif } = b, { min: max - dif, dif } | ||
// return { dif } = b, { min: 0, dif } | ||
// } | ||
// if (MAX in b) return { max } = b, { min: 0, dif: max } | ||
// return { min: 0, dif: 0 } | ||
// } | ||
const FluoNumber = (bound, preset = PLANET, colorant = true) => { | ||
var _cleap$min; | ||
const hslToDye = hsl => { | ||
var _ref, _hsl; | ||
return _ref = (_hsl = hsl, hslToRgb(_hsl)), Dye(_ref); | ||
}; | ||
const leverage = ([h, s, l], base) => [h / base, s / base, l / base]; | ||
const scale = (x, min$1, lever, base, ceil) => min((max(x, min$1) - min$1) * lever + base, ceil); | ||
const projector = function (x) { | ||
var _ref; | ||
const { | ||
min: m, | ||
lever: [rH, rS, rL], | ||
base: [mH, mS, mL] | ||
} = this; | ||
return _ref = [scale(x, m, rH, mH, 360), scale(x, m, rS, mS, 100), scale(x, m, rL, mL, 100)], hslToDye(_ref); | ||
}; | ||
/** | ||
* | ||
* @param {{[min]:number,[max]:number,[dif]:number}} bound | ||
* @param {{max:*,min:*}} preset | ||
* @returns {function(*):function} | ||
* @constructor | ||
*/ | ||
const Projector = (bound, preset) => { | ||
var _bound, _preset; | ||
if (!bound) return void 0; | ||
bound = (_bound = bound, boundToLeap(_bound)); | ||
/** @type {{min:number[],dif:number[]}} */ | ||
const leap = (_preset = preset, presetToLeap(_preset)); | ||
if (!bound.dif) { | ||
const dye = hslToDye(leap.min); | ||
return () => dye; | ||
} | ||
return projector.bind({ | ||
min: bound.min, | ||
lever: leverage(leap.dif, bound.dif), | ||
base: leap.min | ||
}); | ||
}; | ||
const Colorant = (bound, preset = PLANET) => { | ||
const vleap = boundToLeap(bound), | ||
cleap = presetToLeap(preset), | ||
prime = presetToFlat(preset); | ||
let dye; | ||
return vleap.dif && cleap.dif.some(n => !!n) ? (dye = BlendDye(vleap, cleap), colorant ? x => isNumeric(x) ? dye(x) : prime : x => { | ||
let dye = Projector(vleap, preset); | ||
return x => isNumeric(x) ? dye(x) : prime; | ||
}; | ||
const Pigment = (bound, preset = PLANET) => { | ||
const vleap = boundToLeap(bound), | ||
prime = presetToFlat(preset); | ||
let dye = Projector(vleap, preset); | ||
return x => { | ||
var _x, _x2; | ||
return isNumeric(x) ? (_x = x, dye(x)(_x)) : (_x2 = x, prime(_x2)); | ||
}) : (dye = (_cleap$min = cleap.min, hslToDye(_cleap$min)), colorant ? x => isNumeric(x) ? dye : prime : x => { | ||
var _x3, _x4; | ||
}; | ||
}; | ||
return isNumeric(x) ? (_x3 = x, dye(_x3)) : (_x4 = x, prime(_x4)); | ||
}); | ||
const FluoNumber = (bound, preset = PLANET, colorant = true) => { | ||
return colorant ? Colorant(bound, preset) : Pigment(bound, preset); | ||
}; | ||
export { FluoNumber }; |
{ | ||
"name": "@palett/fluo", | ||
"version": "0.2.7", | ||
"version": "0.2.9", | ||
"description": "A color converter", | ||
@@ -19,7 +19,7 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"@palett/fluo-entries": "^0.2.7", | ||
"@palett/fluo-matrix": "^0.2.7", | ||
"@palett/fluo-vector": "^0.2.7", | ||
"@palett/presets": "^0.2.7", | ||
"@palett/util-fluo": "^0.2.7", | ||
"@palett/fluo-entries": "^0.2.9", | ||
"@palett/fluo-matrix": "^0.2.9", | ||
"@palett/fluo-vector": "^0.2.9", | ||
"@palett/presets": "^0.2.9", | ||
"@palett/util-fluo": "^0.2.9", | ||
"@typen/num-strict": "^0.1.4", | ||
@@ -49,3 +49,3 @@ "@vect/enum-matrix-directions": "^0.2.4" | ||
"homepage": "https://github.com/hoyeungw/palett/fluo#readme", | ||
"gitHead": "1f38065d2a1c31ff1ee0f7c8ab1119b965acd4a7" | ||
"gitHead": "f1b67679b402cc3f37c5aadcb21a4eb4a5daa251" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16138
518
1
Updated@palett/fluo-entries@^0.2.9
Updated@palett/fluo-matrix@^0.2.9
Updated@palett/fluo-vector@^0.2.9
Updated@palett/presets@^0.2.9
Updated@palett/util-fluo@^0.2.9