Comparing version 0.8.1 to 0.9.0
@@ -0,1 +1,2 @@ | ||
import { IHsl } from '../models/colors.model'; | ||
declare function rgbToHex(r: number | string, g: number | string, b: number | string): string; | ||
@@ -5,3 +6,5 @@ declare function isHex(color: string): boolean; | ||
declare function luminance(color: string, percent: number): string | null; | ||
declare function hexToHsl(str: string): IHsl | null; | ||
declare const colors: { | ||
hexToHsl: typeof hexToHsl; | ||
hexToRgb: typeof hexToRgb; | ||
@@ -8,0 +11,0 @@ isHex: typeof isHex; |
@@ -64,3 +64,53 @@ "use strict"; | ||
} | ||
function hexToHsl(str) { | ||
if (isHex(str)) { | ||
const hex = str.charAt(0) === '#' ? str.slice(1) : str; | ||
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); | ||
if (result && result.length) { | ||
let r = parseInt(result[1], 16); | ||
let g = parseInt(result[2], 16); | ||
let b = parseInt(result[3], 16); | ||
r /= 255; | ||
g /= 255; | ||
b /= 255; | ||
const max = Math.max(r, g, b); | ||
const min = Math.min(r, g, b); | ||
let h; | ||
let s; | ||
const l = (max + min) / 2; | ||
if (max === min) { | ||
h = s = 0; | ||
} | ||
else { | ||
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; | ||
} | ||
h = h ? h / 6 : 0; | ||
} | ||
return { | ||
h: parseFloat((h * 360).toFixed(1)), | ||
l: parseFloat((l * 100).toFixed(1)), | ||
s: parseFloat((s * 100).toFixed(1)) | ||
}; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
const colors = { | ||
hexToHsl, | ||
hexToRgb, | ||
@@ -67,0 +117,0 @@ isHex, |
{ | ||
"name": "stringman", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Stringman does string manipulation. Do anything from lightening color codes to swapping email address in a string!", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -9,7 +9,8 @@ # Stringman | ||
- validate whether string is url | ||
- return text from inside of parenthesis | ||
- validate whether string is a valid semantic version | ||
- validate whether string is url, email, ip address, fraction, semantic version, and more | ||
- return text from inside or outside of parenthesis | ||
- remove an email address from a string | ||
- convert a hex color to rgb and vice versa | ||
- remove extra whitespace, line breaks, tabs, etc from a string | ||
- validate passwords with given parameters | ||
- much more | ||
@@ -25,2 +26,12 @@ | ||
## Example usage | ||
The documentation has examples for every single method, but here is a simple example to get an idea of how to use Stringman: | ||
```js | ||
import { parens } from 'stringman'; // const parens = require('stringman').parens; | ||
const noParens = parens.remove('this will come back (and this will be removed)'); | ||
console.log(noParens); // 'this will come back' | ||
``` | ||
## Documentation | ||
@@ -36,3 +47,3 @@ | ||
Stringman is written in modules with a variety of functionality and that can be imported individually. See the documentation for more details and usage examples. | ||
Stringman is written in modules with a variety of functionality and that can be imported individually. See the documentation in the link above for more details and usage examples. | ||
@@ -39,0 +50,0 @@ #### colors module |
29650
32
642
118