@palett/dye-factory
Advanced tools
Comparing version 0.8.2 to 0.8.3
@@ -59,2 +59,69 @@ 'use strict'; | ||
const rgbToInt = ([r, g, b]) => ((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF); | ||
/** | ||
* @param {[number,number,number]} rgb | ||
* @returns {string} | ||
*/ | ||
const rgbToHex = rgb => '#' + rgbToInt(rgb).toString(16).toUpperCase().padStart(6, '0'); | ||
const bound = ([r, g, b]) => { | ||
let ma = r, | ||
mi = r; | ||
if (g > r) { | ||
ma = g; | ||
} else { | ||
mi = g; | ||
} | ||
if (b > ma) ma = b; | ||
if (b < mi) mi = b; | ||
return { | ||
max: ma, | ||
sum: ma + mi, | ||
dif: ma - mi | ||
}; | ||
}; | ||
const hue = (r, g, b, max, dif) => { | ||
if (dif === 0) return 0; | ||
switch (max) { | ||
case r: | ||
return ((g - b) / dif + (g < b ? 6 : 0)) % 6; | ||
case g: | ||
return (b - r) / dif + 2; | ||
case b: | ||
return (r - g) / dif + 4; | ||
} | ||
}; | ||
const THOUSAND$1 = 1000; | ||
/** | ||
* !dif: dif===0 | ||
* @param {number} r - [0,255] | ||
* @param {number} g - [0,255] | ||
* @param {number} b - [0,255] | ||
* @returns {[number,number,number]} [Hue([0,360]), Saturation([0,100]), Lightness([0,100])] | ||
*/ | ||
function rgbToHsl([r, g, b]) { | ||
r /= 255; | ||
g /= 255; | ||
b /= 255; | ||
const { | ||
max, | ||
sum, | ||
dif | ||
} = bound([r, g, b]); | ||
let h = hue(r, g, b, max, dif) * 60, | ||
s = !dif ? 0 : sum > 1 ? dif / (2 - sum) : dif / sum, | ||
l = sum / 2; | ||
return [round(h), round(s * THOUSAND$1) / 10, round(l * THOUSAND$1) / 10]; | ||
} | ||
const diluteHex = (hex, hi) => { | ||
@@ -85,2 +152,44 @@ hi = hi || hex.length; | ||
* | ||
* @param {string} hex | ||
* @returns {number[]} | ||
*/ | ||
function hexToRgb(hex) { | ||
const int = hexToInt(hex); | ||
return [int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF]; | ||
} | ||
const THOUSAND = 1000; | ||
/** | ||
* !dif: dif===0 | ||
* @param {number} int | ||
* @returns {[number,number,number]} [Hue([0,360]), Saturation([0,100]), Lightness([0,100])] | ||
*/ | ||
function intToHsl(int) { | ||
let r = int >> 16 & 0xFF, | ||
g = int >> 8 & 0xFF, | ||
b = int & 0xFF; | ||
r /= 255; | ||
g /= 255; | ||
b /= 255; | ||
const { | ||
max, | ||
sum, | ||
dif | ||
} = bound([r, g, b]); | ||
let h = hue(r, g, b, max, dif) * 60, | ||
s = !dif ? 0 : sum > 1 ? dif / (2 - sum) : dif / sum, | ||
l = sum / 2; | ||
return [round(h), round(s * THOUSAND) / 10, round(l * THOUSAND) / 10]; | ||
} | ||
const hexToHsl = hex => { | ||
var _ref, _hex; | ||
return _ref = (_hex = hex, hexToInt(_hex)), intToHsl(_ref); | ||
}; | ||
/** | ||
* | ||
* @param {number} n | ||
@@ -117,2 +226,39 @@ * @param {number} h | ||
const hslToHex = hsl => { | ||
var _ref, _hsl; | ||
return _ref = (_hsl = hsl, hslToRgb(_hsl)), rgbToHex(_ref); | ||
}; | ||
/** | ||
* | ||
* @param {number} h | ||
* @param {number} s | ||
* @param {number} l | ||
* @returns {number} | ||
*/ | ||
function hslToInt([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) & 0xFF) << 16) + ((round(g * 0xFF) & 0xFF) << 8) + (round(b * 0xFF) & 0xFF); | ||
} | ||
const intToRgb = n => [n >> 16 & 0xFF, n >> 8 & 0xFF, n & 0xFF]; | ||
const intToHex = int => '#' + int.toString(16).toUpperCase().padStart(6, '0'); | ||
const oneself = x => x; | ||
var _class, _temp, _class2, _temp2, _class3, _temp3, _class4, _temp4; | ||
(_temp = _class = class Rgb {}, _class.rgb = oneself, _class.hex = rgbToHex, _class.hsl = rgbToHsl, _class.int = rgbToInt, _temp); | ||
(_temp2 = _class2 = class Rgb {}, _class2.rgb = hexToRgb, _class2.hex = oneself, _class2.hsl = hexToHsl, _class2.int = hexToInt, _temp2); | ||
(_temp3 = _class3 = class Rgb {}, _class3.rgb = hslToRgb, _class3.hex = hslToHex, _class3.hsl = oneself, _class3.int = hslToInt, _temp3); | ||
(_temp4 = _class4 = class Rgb {}, _class4.rgb = intToRgb, _class4.hex = intToHex, _class4.hsl = intToHsl, _class4.int = oneself, _temp4); | ||
function excite() { | ||
@@ -204,4 +350,4 @@ if (this.head.length) this.head += ';'; | ||
return { | ||
head: this.head, | ||
tail: this.tail | ||
head: this.head ?? '', | ||
tail: this.tail ?? '' | ||
}; | ||
@@ -281,3 +427,5 @@ } | ||
make(color) { | ||
const local = (this == null ? void 0 : this.slice()) ?? DyeFab.shallow(); | ||
var _this$slice; | ||
const local = (this == null ? void 0 : (_this$slice = this.slice) == null ? void 0 : _this$slice.call(this)) ?? DyeFab.shallow(); | ||
if (color) this.encolor.call(local, color); | ||
@@ -287,2 +435,10 @@ return DyeFab.prototype.render.bind(local); | ||
render(color, text) { | ||
var _this$slice2; | ||
const local = (this == null ? void 0 : (_this$slice2 = this.slice) == null ? void 0 : _this$slice2.call(this)) ?? DyeFab.shallow(); | ||
if (color) this.encolor.call(local, color); | ||
return DyeFab.prototype.render.call(local, text); | ||
} | ||
} | ||
@@ -289,0 +445,0 @@ |
@@ -55,2 +55,69 @@ import { RGB, HEX, HSL, INT } from '@palett/enum-color-space'; | ||
const rgbToInt = ([r, g, b]) => ((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF); | ||
/** | ||
* @param {[number,number,number]} rgb | ||
* @returns {string} | ||
*/ | ||
const rgbToHex = rgb => '#' + rgbToInt(rgb).toString(16).toUpperCase().padStart(6, '0'); | ||
const bound = ([r, g, b]) => { | ||
let ma = r, | ||
mi = r; | ||
if (g > r) { | ||
ma = g; | ||
} else { | ||
mi = g; | ||
} | ||
if (b > ma) ma = b; | ||
if (b < mi) mi = b; | ||
return { | ||
max: ma, | ||
sum: ma + mi, | ||
dif: ma - mi | ||
}; | ||
}; | ||
const hue = (r, g, b, max, dif) => { | ||
if (dif === 0) return 0; | ||
switch (max) { | ||
case r: | ||
return ((g - b) / dif + (g < b ? 6 : 0)) % 6; | ||
case g: | ||
return (b - r) / dif + 2; | ||
case b: | ||
return (r - g) / dif + 4; | ||
} | ||
}; | ||
const THOUSAND$1 = 1000; | ||
/** | ||
* !dif: dif===0 | ||
* @param {number} r - [0,255] | ||
* @param {number} g - [0,255] | ||
* @param {number} b - [0,255] | ||
* @returns {[number,number,number]} [Hue([0,360]), Saturation([0,100]), Lightness([0,100])] | ||
*/ | ||
function rgbToHsl([r, g, b]) { | ||
r /= 255; | ||
g /= 255; | ||
b /= 255; | ||
const { | ||
max, | ||
sum, | ||
dif | ||
} = bound([r, g, b]); | ||
let h = hue(r, g, b, max, dif) * 60, | ||
s = !dif ? 0 : sum > 1 ? dif / (2 - sum) : dif / sum, | ||
l = sum / 2; | ||
return [round(h), round(s * THOUSAND$1) / 10, round(l * THOUSAND$1) / 10]; | ||
} | ||
const diluteHex = (hex, hi) => { | ||
@@ -81,2 +148,44 @@ hi = hi || hex.length; | ||
* | ||
* @param {string} hex | ||
* @returns {number[]} | ||
*/ | ||
function hexToRgb(hex) { | ||
const int = hexToInt(hex); | ||
return [int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF]; | ||
} | ||
const THOUSAND = 1000; | ||
/** | ||
* !dif: dif===0 | ||
* @param {number} int | ||
* @returns {[number,number,number]} [Hue([0,360]), Saturation([0,100]), Lightness([0,100])] | ||
*/ | ||
function intToHsl(int) { | ||
let r = int >> 16 & 0xFF, | ||
g = int >> 8 & 0xFF, | ||
b = int & 0xFF; | ||
r /= 255; | ||
g /= 255; | ||
b /= 255; | ||
const { | ||
max, | ||
sum, | ||
dif | ||
} = bound([r, g, b]); | ||
let h = hue(r, g, b, max, dif) * 60, | ||
s = !dif ? 0 : sum > 1 ? dif / (2 - sum) : dif / sum, | ||
l = sum / 2; | ||
return [round(h), round(s * THOUSAND) / 10, round(l * THOUSAND) / 10]; | ||
} | ||
const hexToHsl = hex => { | ||
var _ref, _hex; | ||
return _ref = (_hex = hex, hexToInt(_hex)), intToHsl(_ref); | ||
}; | ||
/** | ||
* | ||
* @param {number} n | ||
@@ -113,2 +222,39 @@ * @param {number} h | ||
const hslToHex = hsl => { | ||
var _ref, _hsl; | ||
return _ref = (_hsl = hsl, hslToRgb(_hsl)), rgbToHex(_ref); | ||
}; | ||
/** | ||
* | ||
* @param {number} h | ||
* @param {number} s | ||
* @param {number} l | ||
* @returns {number} | ||
*/ | ||
function hslToInt([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) & 0xFF) << 16) + ((round(g * 0xFF) & 0xFF) << 8) + (round(b * 0xFF) & 0xFF); | ||
} | ||
const intToRgb = n => [n >> 16 & 0xFF, n >> 8 & 0xFF, n & 0xFF]; | ||
const intToHex = int => '#' + int.toString(16).toUpperCase().padStart(6, '0'); | ||
const oneself = x => x; | ||
var _class, _temp, _class2, _temp2, _class3, _temp3, _class4, _temp4; | ||
(_temp = _class = class Rgb {}, _class.rgb = oneself, _class.hex = rgbToHex, _class.hsl = rgbToHsl, _class.int = rgbToInt, _temp); | ||
(_temp2 = _class2 = class Rgb {}, _class2.rgb = hexToRgb, _class2.hex = oneself, _class2.hsl = hexToHsl, _class2.int = hexToInt, _temp2); | ||
(_temp3 = _class3 = class Rgb {}, _class3.rgb = hslToRgb, _class3.hex = hslToHex, _class3.hsl = oneself, _class3.int = hslToInt, _temp3); | ||
(_temp4 = _class4 = class Rgb {}, _class4.rgb = intToRgb, _class4.hex = intToHex, _class4.hsl = intToHsl, _class4.int = oneself, _temp4); | ||
function excite() { | ||
@@ -200,4 +346,4 @@ if (this.head.length) this.head += ';'; | ||
return { | ||
head: this.head, | ||
tail: this.tail | ||
head: this.head ?? '', | ||
tail: this.tail ?? '' | ||
}; | ||
@@ -277,3 +423,5 @@ } | ||
make(color) { | ||
const local = (this == null ? void 0 : this.slice()) ?? DyeFab.shallow(); | ||
var _this$slice; | ||
const local = (this == null ? void 0 : (_this$slice = this.slice) == null ? void 0 : _this$slice.call(this)) ?? DyeFab.shallow(); | ||
if (color) this.encolor.call(local, color); | ||
@@ -283,4 +431,12 @@ return DyeFab.prototype.render.bind(local); | ||
render(color, text) { | ||
var _this$slice2; | ||
const local = (this == null ? void 0 : (_this$slice2 = this.slice) == null ? void 0 : _this$slice2.call(this)) ?? DyeFab.shallow(); | ||
if (color) this.encolor.call(local, color); | ||
return DyeFab.prototype.render.call(local, text); | ||
} | ||
} | ||
export { DyeFab, DyeFactory, enstyle, pushHex, pushHsl, pushInt, pushRgb, selectEncolor }; |
{ | ||
"name": "@palett/dye-factory", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"description": "A colorant to string", | ||
@@ -20,4 +20,4 @@ "main": "dist/index.cjs.js", | ||
"@ject/oneself": "^0.0.12", | ||
"@palett/enum-color-space": "^0.8.2", | ||
"@palett/util-ansi": "^0.8.2" | ||
"@palett/enum-color-space": "^0.8.3", | ||
"@palett/util-ansi": "^0.8.3" | ||
}, | ||
@@ -42,3 +42,3 @@ "repository": { | ||
"homepage": "https://github.com/hoyeungw/palett/dye#readme", | ||
"gitHead": "ec8c56219ee71eea3504a63e1f7bd862dfd85d4a" | ||
"gitHead": "e806c589dc4793f9c1e6eeb945647604672079b5" | ||
} |
23535
683
Updated@palett/util-ansi@^0.8.3