Comparing version 0.9.0 to 0.10.0
@@ -7,2 +7,3 @@ import { IHsl } from '../models/colors.model'; | ||
declare function hexToHsl(str: string): IHsl | null; | ||
declare function rgbToHsl(r: number, g: number, b: number): IHsl | null; | ||
declare const colors: { | ||
@@ -14,3 +15,4 @@ hexToHsl: typeof hexToHsl; | ||
rgbToHex: typeof rgbToHex; | ||
rgbToHsl: typeof rgbToHsl; | ||
}; | ||
export { colors }; |
@@ -69,37 +69,6 @@ "use strict"; | ||
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)) | ||
}; | ||
const r = parseInt(result[1], 16); | ||
const g = parseInt(result[2], 16); | ||
const b = parseInt(result[3], 16); | ||
return rgbToHsl(r, g, b); | ||
} | ||
@@ -114,2 +83,39 @@ else { | ||
} | ||
function rgbToHsl(r, g, b) { | ||
if (typeof r !== 'number' || typeof g !== 'number' || typeof b !== 'number') { | ||
return null; | ||
} | ||
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)) | ||
}; | ||
} | ||
const colors = { | ||
@@ -120,4 +126,5 @@ hexToHsl, | ||
luminance, | ||
rgbToHex | ||
rgbToHex, | ||
rgbToHsl | ||
}; | ||
exports.colors = colors; |
{ | ||
"name": "stringman", | ||
"version": "0.9.0", | ||
"version": "0.10.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", |
@@ -53,2 +53,4 @@ # Stringman | ||
- luminance => Takes a hex color value and a percentage and returns a hex color string that is the result of lightening or darkening the original color by the percentage. Does not work on black or white (yet)! | ||
- hexToHsl => Takes a string representing a hex color code and returns the hsl equivalent in an object with keys for h, s, and l | ||
- rgbToHsl => Takes each of the 3 rgb number values as individual arguments and returns the hsl equivalent in an object with keys for h, s, and l | ||
@@ -55,0 +57,0 @@ #### email module |
29973
651
120