@palett/convert
Advanced tools
Comparing version 0.8.2 to 0.8.3
@@ -6,2 +6,3 @@ 'use strict'; | ||
var math = require('@aryth/math'); | ||
var oneself = require('@ject/oneself'); | ||
@@ -51,3 +52,3 @@ const rgbToInt = ([r, g, b]) => ((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF); | ||
const THOUSAND = 1000; | ||
const THOUSAND$1 = 1000; | ||
/** | ||
@@ -73,3 +74,3 @@ * !dif: dif===0 | ||
l = sum / 2; | ||
return [math.round(h), math.round(s * THOUSAND) / 10, math.round(l * THOUSAND) / 10]; | ||
return [math.round(h), math.round(s * THOUSAND$1) / 10, math.round(l * THOUSAND$1) / 10]; | ||
} | ||
@@ -112,6 +113,31 @@ | ||
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 [math.round(h), math.round(s * THOUSAND) / 10, math.round(l * THOUSAND) / 10]; | ||
} | ||
const hexToHsl = hex => { | ||
var _ref, _hex; | ||
return _ref = (_hex = hex, hexToRgb(_hex)), rgbToHsl(_ref); | ||
return _ref = (_hex = hex, hexToInt(_hex)), intToHsl(_ref); | ||
}; | ||
@@ -156,4 +182,32 @@ | ||
/** | ||
* | ||
* @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 ((math.round(r * 0xFF) & 0xFF) << 16) + ((math.round(g * 0xFF) & 0xFF) << 8) + (math.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'); | ||
var _class, _temp, _class2, _temp2, _class3, _temp3, _class4, _temp4; | ||
class Conv {} | ||
Conv.rgb = (_temp = _class = class Rgb {}, _class.rgb = oneself.oneself, _class.hex = rgbToHex, _class.hsl = rgbToHsl, _class.int = rgbToInt, _temp); | ||
Conv.hex = (_temp2 = _class2 = class Rgb {}, _class2.rgb = hexToRgb, _class2.hex = oneself.oneself, _class2.hsl = hexToHsl, _class2.int = hexToInt, _temp2); | ||
Conv.hsl = (_temp3 = _class3 = class Rgb {}, _class3.rgb = hslToRgb, _class3.hex = hslToHex, _class3.hsl = oneself.oneself, _class3.int = hslToInt, _temp3); | ||
Conv.int = (_temp4 = _class4 = class Rgb {}, _class4.rgb = intToRgb, _class4.hex = intToHex, _class4.hsl = intToHsl, _class4.int = oneself.oneself, _temp4); | ||
exports.Conv = Conv; | ||
exports.hexToHsl = hexToHsl; | ||
@@ -163,3 +217,6 @@ exports.hexToInt = hexToInt; | ||
exports.hslToHex = hslToHex; | ||
exports.hslToInt = hslToInt; | ||
exports.hslToRgb = hslToRgb; | ||
exports.intToHex = intToHex; | ||
exports.intToHsl = intToHsl; | ||
exports.intToRgb = intToRgb; | ||
@@ -166,0 +223,0 @@ exports.rgbToHex = rgbToHex; |
import { round } from '@aryth/math'; | ||
import { oneself } from '@ject/oneself'; | ||
@@ -46,3 +47,3 @@ const rgbToInt = ([r, g, b]) => ((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF); | ||
const THOUSAND = 1000; | ||
const THOUSAND$1 = 1000; | ||
/** | ||
@@ -68,3 +69,3 @@ * !dif: dif===0 | ||
l = sum / 2; | ||
return [round(h), round(s * THOUSAND) / 10, round(l * THOUSAND) / 10]; | ||
return [round(h), round(s * THOUSAND$1) / 10, round(l * THOUSAND$1) / 10]; | ||
} | ||
@@ -107,6 +108,31 @@ | ||
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, hexToRgb(_hex)), rgbToHsl(_ref); | ||
return _ref = (_hex = hex, hexToInt(_hex)), intToHsl(_ref); | ||
}; | ||
@@ -151,4 +177,31 @@ | ||
/** | ||
* | ||
* @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]; | ||
export { hexToHsl, hexToInt, hexToRgb, hslToHex, hslToRgb, intToRgb, rgbToHex, rgbToHsl, rgbToInt }; | ||
const intToHex = int => '#' + int.toString(16).toUpperCase().padStart(6, '0'); | ||
var _class, _temp, _class2, _temp2, _class3, _temp3, _class4, _temp4; | ||
class Conv {} | ||
Conv.rgb = (_temp = _class = class Rgb {}, _class.rgb = oneself, _class.hex = rgbToHex, _class.hsl = rgbToHsl, _class.int = rgbToInt, _temp); | ||
Conv.hex = (_temp2 = _class2 = class Rgb {}, _class2.rgb = hexToRgb, _class2.hex = oneself, _class2.hsl = hexToHsl, _class2.int = hexToInt, _temp2); | ||
Conv.hsl = (_temp3 = _class3 = class Rgb {}, _class3.rgb = hslToRgb, _class3.hex = hslToHex, _class3.hsl = oneself, _class3.int = hslToInt, _temp3); | ||
Conv.int = (_temp4 = _class4 = class Rgb {}, _class4.rgb = intToRgb, _class4.hex = intToHex, _class4.hsl = intToHsl, _class4.int = oneself, _temp4); | ||
export { Conv, hexToHsl, hexToInt, hexToRgb, hslToHex, hslToInt, hslToRgb, intToHex, intToHsl, intToRgb, rgbToHex, rgbToHsl, rgbToInt }; |
{ | ||
"name": "@palett/convert", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"description": "A color converter among RGB, HEX and HSL.", | ||
@@ -18,3 +18,4 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"@aryth/math": "^0.3.19" | ||
"@aryth/math": "^0.3.19", | ||
"@ject/oneself": "^0.0.12" | ||
}, | ||
@@ -44,3 +45,3 @@ "repository": { | ||
"homepage": "https://github.com/hoyeungw/palett/rgb#readme", | ||
"gitHead": "ec8c56219ee71eea3504a63e1f7bd862dfd85d4a" | ||
"gitHead": "e806c589dc4793f9c1e6eeb945647604672079b5" | ||
} |
12531
350
2
+ Added@ject/oneself@^0.0.12
+ Added@ject/oneself@0.0.12(transitive)