colors-convert
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -70,4 +70,4 @@ import { isHsl } from '../types/isType'; | ||
// Covert a string in these two formats to an hsl object: | ||
// - 322°, 79%, 52% (short format) -> { h: 322, s: 79, l: 52 } | ||
// - hsl(322°, 79%, 52%) (long format) -> { h: 322, s: 79, l: 52 } | ||
// - 322, 79%, 52% (short format) -> { h: 322, s: 79, l: 52 } | ||
// - hsl(322, 79%, 52%) (long format) -> { h: 322, s: 79, l: 52 } | ||
export function hslString2Object(hslString) { | ||
@@ -79,4 +79,4 @@ if (typeof hslString !== 'string') { | ||
// check short and long formats | ||
var regexShortFormat = /^(([0-9]+°)(\s)*,(\s)*([0-9]+%)(\s)*,(\s)*([0-9]+%))/gi; | ||
var regexLongFormat = /^((hsl(\s)*\()(\s)*([0-9]+°)(\s)*,(\s)*([0-9]+%)(\s)*,(\s)*([0-9]+%)(\s)*(\)))/gi; | ||
var regexShortFormat = /^(([0-9]+)(\s)*,(\s)*([0-9]+%)(\s)*,(\s)*([0-9]+%))/gi; | ||
var regexLongFormat = /^((hsl(\s)*\()(\s)*([0-9]+)(\s)*,(\s)*([0-9]+%)(\s)*,(\s)*([0-9]+%)(\s)*(\)))/gi; | ||
var isShortFormat = regexShortFormat.test(hslString); | ||
@@ -98,4 +98,9 @@ var isLongFormat = regexLongFormat.test(hslString); | ||
function shortHslFormatToHslObject(hslString) { | ||
// split by comma, remove white spaces, remove last char, convert to number | ||
var values = hslString.split(',').map(function (v) { return Number(v.trim().slice(0, -1)); }); | ||
// split by comma, remove white spaces, remove last char (except for h), convert to number | ||
var values = hslString.split(',').map(function (v, i) { | ||
if (i === 0) | ||
return Number(v.trim()); | ||
else | ||
return Number(v.trim().slice(0, -1)); | ||
}); | ||
return { h: values[0], s: values[1], l: values[2] }; | ||
@@ -102,0 +107,0 @@ } |
@@ -147,6 +147,6 @@ import { round } from 'lodash'; | ||
} | ||
// TODO: chanhe regex to accept also a = .4 | ||
// TODO: change regex to accept also a = .4 | ||
// Covert a string in these two formats to a rgba object: | ||
// - 255, 0, 255, 0.5 (short format) -> {r: 255, g: 0, b: 255, a: 0.5} | ||
// - rgb(255, 0, 255, 0.5) (long format) -> {r: 255, g: 0, b: 255, a: 0.5} | ||
// - rgba(255, 0, 255, 0.5) (long format) -> {r: 255, g: 0, b: 255, a: 0.5} | ||
export function rgbaString2Object(rgbaString) { | ||
@@ -153,0 +153,0 @@ if (typeof rgbaString !== 'string') { |
@@ -52,11 +52,11 @@ import { hsl2hex, hsl2rgb, hsl2cmyk, hslString2Object } from '../index'; | ||
test("hslString2Object", function () { | ||
expect(hslString2Object('322°, 79%, 52%')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('322°,79%, 52%')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('322°, 79%, 52%')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('hsl(322°, 79%, 52%)')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('hsl(322°,79%, 52%)')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('hsl(322°, 79%, 52%)')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(function () { return hslString2Object('600°, 79%, 52%'); }).toThrow(); | ||
expect(function () { return hslString2Object('hsl(600°, 79%, 52%)'); }).toThrow(); | ||
expect(hslString2Object('322, 79%, 52%')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('322,79%, 52%')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('322, 79%, 52%')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('hsl(322, 79%, 52%)')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('hsl(322,79%, 52%)')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(hslString2Object('hsl(322, 79%, 52%)')).toEqual({ h: 322, s: 79, l: 52 }); | ||
expect(function () { return hslString2Object('600, 79%, 52%'); }).toThrow(); | ||
expect(function () { return hslString2Object('hsl(600, 79%, 52%)'); }).toThrow(); | ||
}); | ||
//# sourceMappingURL=hsl.test.js.map |
{ | ||
"name": "colors-convert", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
@@ -114,2 +114,22 @@ # Colors convert 🍭🍬 | ||
- **`rgbString2Object(string): rgb`**: Coverts a string in these two formats to a rgb object: | ||
- `255, 0, 255` (short format) -> `{r: 255, g: 0, b: 255}` | ||
- `rgb(255, 0, 255)` (long format) -> `{r: 255, g: 0, b: 255}` | ||
- **`rgbaString2Object(string): rgba`**: Coverts a string in these two formats to a rgba object: | ||
- `255, 0, 255, 0.5` (short format) -> `{r: 255, g: 0, b: 255, a: 0.5}` | ||
- `rgba(255, 0, 255, 0.5)` (long format) -> `{r: 255, g: 0, b: 255, a: 0.5}` | ||
- **`cmykString2Object(string): cmyk`**: Converts a string in these two formats to a cmyk object: | ||
- `0, 50, 20, 100` (short format) -> `{c: 0, m: 50, y: 20, k: 100}` | ||
- `cmyk(0, 50, 20, 100)` (long format) -> `{c: 0, m: 50, y: 20, k: 100}` | ||
- **`hslString2Object(string): hsl`**: Converts a string in these two formats to an hsl object: | ||
- `322, 79%, 52%` (short format) -> `{ h: 322, s: 79, l: 52 }` | ||
- `hsl(322, 79%, 52%)` (long format) -> `{ h: 322, s: 79, l: 52 }` | ||
--- | ||
@@ -116,0 +136,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3952233
19515
191