colors-convert
Advanced tools
Comparing version 1.1.5 to 1.2.0
export { isHex, isRgb, isRgba, isCmyk, isHsl, isColor } from './types/isType'; | ||
export { color2string, color2cssString } from './lib/color'; | ||
export { hex2rgbOrRgba, hex2rgba, hex2hexWithAlpha, hex2cmyk, hex2hsl } from './lib/hex'; | ||
export { rgb2hex, rgb2cmyk, rgb2hsl, rgba2rgb, rgb2rgba, color2rgb } from './lib/rgb'; | ||
export { cmyk2rgb, cmyk2hex, cmyk2hsl } from './lib/cmyk'; | ||
export { hsl2hex, hsl2rgb, hsl2cmyk } from './lib/hsl'; | ||
export { rgb2hex, rgb2cmyk, rgb2hsl, rgba2rgb, rgb2rgba, color2rgb, rgbString2Object, rgbaString2Object, } from './lib/rgb'; | ||
export { cmyk2rgb, cmyk2hex, cmyk2hsl, cmykString2Object } from './lib/cmyk'; | ||
export { hsl2hex, hsl2rgb, hsl2cmyk, hslString2Object } from './lib/hsl'; | ||
export { getRandomColor } from './lib/random'; | ||
export { mix } from './lib/mix'; | ||
export { name } from './lib/name'; |
export { isHex, isRgb, isRgba, isCmyk, isHsl, isColor } from './types/isType'; | ||
export { color2string, color2cssString } from './lib/color'; | ||
export { hex2rgbOrRgba, hex2rgba, hex2hexWithAlpha, hex2cmyk, hex2hsl } from './lib/hex'; | ||
export { rgb2hex, rgb2cmyk, rgb2hsl, rgba2rgb, rgb2rgba, color2rgb } from './lib/rgb'; | ||
export { cmyk2rgb, cmyk2hex, cmyk2hsl } from './lib/cmyk'; | ||
export { hsl2hex, hsl2rgb, hsl2cmyk } from './lib/hsl'; | ||
export { rgb2hex, rgb2cmyk, rgb2hsl, rgba2rgb, rgb2rgba, color2rgb, rgbString2Object, rgbaString2Object, } from './lib/rgb'; | ||
export { cmyk2rgb, cmyk2hex, cmyk2hsl, cmykString2Object } from './lib/cmyk'; | ||
export { hsl2hex, hsl2rgb, hsl2cmyk, hslString2Object } from './lib/hsl'; | ||
export { getRandomColor } from './lib/random'; | ||
@@ -8,0 +8,0 @@ export { mix } from './lib/mix'; |
@@ -5,1 +5,2 @@ import { CMYK, HEX, RGB, HSL } from '../types/types'; | ||
export declare function cmyk2hsl(cmyk: CMYK): HSL; | ||
export declare function cmykString2Object(cmykString: string): CMYK; |
@@ -37,2 +37,42 @@ import { applyFnToEachObjValue } from './utils'; | ||
} | ||
// Covert 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} | ||
export function cmykString2Object(cmykString) { | ||
if (typeof cmykString !== 'string') { | ||
throw new Error(cmykString + " is not a string."); | ||
} | ||
var errorMessage = cmykString + " is not a valid format. The accepted formats are 'c, m, y, k' and 'cmyk(c, m, y, k)' with c, m, y, k in [0, 100]."; | ||
// check short and long formats | ||
var regexShortFormat = /^(([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*([0-9]+))/gi; | ||
var regexLongFormat = /^((cmyk(\s)*\()(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*(\)))/gi; | ||
var isShortFormat = regexShortFormat.test(cmykString); | ||
var isLongFormat = regexLongFormat.test(cmykString); | ||
if (!isShortFormat && !isLongFormat) { | ||
throw new Error(errorMessage); | ||
} | ||
var cmykStringCleanShortFormat = isShortFormat | ||
? cmykString | ||
: fromLongToShortCmykFormat(cmykString); | ||
var cmykObject = shortCmykFormatToRgbObject(cmykStringCleanShortFormat); | ||
if (isCmyk(cmykObject)) { | ||
return cmykObject; | ||
} | ||
else { | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
// Convert a string in format '0, 50, 20, 100' (short format) to a RGB object {c: 0, m: 50, y: 20, k: 100} | ||
function shortCmykFormatToRgbObject(cmykString) { | ||
// split by comma, remove white spaces, convert to number | ||
var values = cmykString.split(',').map(function (v) { return Number(v.trim()); }); | ||
return { c: values[0], m: values[1], y: values[2], k: values[3] }; | ||
} | ||
function fromLongToShortCmykFormat(cmykStringLongFormat) { | ||
var cmykStringShortFormat = cmykStringLongFormat | ||
.replace('cmyk', '') | ||
.replace('(', '') | ||
.replace(')', ''); | ||
return cmykStringShortFormat; | ||
} | ||
//# sourceMappingURL=cmyk.js.map |
@@ -5,1 +5,2 @@ import { HSL, RGB, CMYK, HEX } from '../types/types'; | ||
export declare function hsl2cmyk(hsl: HSL): CMYK; | ||
export declare function hslString2Object(hslString: string): HSL; |
@@ -69,2 +69,40 @@ 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 } | ||
export function hslString2Object(hslString) { | ||
if (typeof hslString !== 'string') { | ||
throw new Error(hslString + " is not a string."); | ||
} | ||
var errorMessage = hslString + " is not a valid format. The accepted formats are 'h\u00B0, s%, l%' and 'hsl(h\u00B0, s%, l%)' with h in [0, 359] and s, l in [0, 100]."; | ||
// 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 isShortFormat = regexShortFormat.test(hslString); | ||
var isLongFormat = regexLongFormat.test(hslString); | ||
if (!isShortFormat && !isLongFormat) { | ||
throw new Error(errorMessage); | ||
} | ||
var hslStringCleanShortFormat = isShortFormat ? hslString : fromLongToShortFormat(hslString); | ||
var hslObject = shortHslFormatToHslObject(hslStringCleanShortFormat); | ||
if (isHsl(hslObject)) { | ||
return hslObject; | ||
} | ||
else { | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
// Convert a string in format '322°, 79%, 52%' (short format) to an HSL object { h: 322, s: 79, l: 52 } | ||
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)); }); | ||
return { h: values[0], s: values[1], l: values[2] }; | ||
} | ||
function fromLongToShortFormat(hslStringLongFormat) { | ||
var hslStringShortFormat = hslStringLongFormat | ||
.replace('hsl', '') | ||
.replace('(', '') | ||
.replace(')', ''); | ||
return hslStringShortFormat; | ||
} | ||
//# sourceMappingURL=hsl.js.map |
@@ -8,1 +8,3 @@ import { RGB, RGBA, CMYK, HEX, HSL, Color } from '../types/types'; | ||
export declare function color2rgb(color: Color): RGB; | ||
export declare function rgbString2Object(rgbString: string): RGB; | ||
export declare function rgbaString2Object(rgbaString: string): RGBA; |
@@ -109,2 +109,81 @@ import { round } from 'lodash'; | ||
} | ||
// Covert 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} | ||
export function rgbString2Object(rgbString) { | ||
if (typeof rgbString !== 'string') { | ||
throw new Error(rgbString + " is not a string."); | ||
} | ||
var errorMessage = rgbString + " is not a valid format. The accepted formats are 'r, g, b' and 'rgb(r, g, b)' with r, g, b in [0, 255]."; | ||
// check short and long formats | ||
var regexShortFormat = /^(([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*([0-9]+))/gi; | ||
var regexLongFormat = /^((rgb(\s)*\()(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*(\)))/gi; | ||
var isShortFormat = regexShortFormat.test(rgbString); | ||
var isLongFormat = regexLongFormat.test(rgbString); | ||
if (!isShortFormat && !isLongFormat) { | ||
throw new Error(errorMessage); | ||
} | ||
var rgbStringCleanShortFormat = isShortFormat ? rgbString : fromLongToShortRgbFormat(rgbString); | ||
var rgbObject = shortRgbFormatToRgbObject(rgbStringCleanShortFormat); | ||
if (isRgb(rgbObject)) { | ||
return rgbObject; | ||
} | ||
else { | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
// Convert a string in format '255, 0, 255' (short format) to a RGB object {r: 255, g: 0, b: 255} | ||
function shortRgbFormatToRgbObject(rgbString) { | ||
// split by comma, remove white spaces, convert to number | ||
var values = rgbString.split(',').map(function (v) { return Number(v.trim()); }); | ||
return { r: values[0], g: values[1], b: values[2] }; | ||
} | ||
function fromLongToShortRgbFormat(rgbStringLongFormat) { | ||
var rgbStringShortFormat = rgbStringLongFormat | ||
.replace('rgb', '') | ||
.replace('(', '') | ||
.replace(')', ''); | ||
return rgbStringShortFormat; | ||
} | ||
// TODO: chanhe 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} | ||
export function rgbaString2Object(rgbaString) { | ||
if (typeof rgbaString !== 'string') { | ||
throw new Error(rgbaString + " is not a string."); | ||
} | ||
var errorMessage = rgbaString + " is not a valid format. The accepted formats are 'r, g, b, a' and 'rgba(r, g, b, a)' with r, g, b in [0, 255] and a in [0, 1]."; | ||
// check short and long formats | ||
var regexShortFormat = /^(([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*((0(\.\d+)?|1(\.0+)?)))/gi; | ||
var regexLongFormat = /^((rgba(\s)*\()(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*,(\s)*([0-9]+)(\s)*, (\s)*((0(\.\d+)?|1(\.0+)?))(\s)*(\)))/gi; | ||
var isShortFormat = regexShortFormat.test(rgbaString); | ||
var isLongFormat = regexLongFormat.test(rgbaString); | ||
if (!isShortFormat && !isLongFormat) { | ||
throw new Error(errorMessage); | ||
} | ||
var rgbaStringCleanShortFormat = isShortFormat | ||
? rgbaString | ||
: fromLongToShortRgbaFormat(rgbaString); | ||
var rgbaObject = shortRgbaFormatToRgbObject(rgbaStringCleanShortFormat); | ||
if (isRgba(rgbaObject)) { | ||
return rgbaObject; | ||
} | ||
else { | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
// Convert a string in format '255, 0, 255, 0.5' (short format) to a RGB object {r: 255, g: 0, b: 255, a: 0.5} | ||
function shortRgbaFormatToRgbObject(rgbaString) { | ||
// split by comma, remove white spaces, convert to number | ||
var values = rgbaString.split(',').map(function (v) { return Number(v.trim()); }); | ||
return { r: values[0], g: values[1], b: values[2], a: values[3] }; | ||
} | ||
function fromLongToShortRgbaFormat(rgbaStringLongFormat) { | ||
var rgbaStringShortFormat = rgbaStringLongFormat | ||
.replace('rgba', '') | ||
.replace('(', '') | ||
.replace(')', ''); | ||
return rgbaStringShortFormat; | ||
} | ||
//# sourceMappingURL=rgb.js.map |
@@ -1,2 +0,2 @@ | ||
import { cmyk2rgb, cmyk2hex, cmyk2hsl } from '../index'; | ||
import { cmyk2rgb, cmyk2hex, cmyk2hsl, cmykString2Object } from '../index'; | ||
//////////////////////////////////////////////////////// | ||
@@ -33,2 +33,15 @@ // cmyk2rgb | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// cmykString2Object | ||
//////////////////////////////////////////////////////// | ||
test("cmykString2Object", function () { | ||
expect(cmykString2Object('0, 50, 20, 100')).toEqual({ c: 0, m: 50, y: 20, k: 100 }); | ||
expect(cmykString2Object('0,50, 20, 100')).toEqual({ c: 0, m: 50, y: 20, k: 100 }); | ||
expect(cmykString2Object('0, 50, 20, 100')).toEqual({ c: 0, m: 50, y: 20, k: 100 }); | ||
expect(cmykString2Object('cmyk(0, 50, 20, 100)')).toEqual({ c: 0, m: 50, y: 20, k: 100 }); | ||
expect(cmykString2Object('cmyk(0,50, 20, 100)')).toEqual({ c: 0, m: 50, y: 20, k: 100 }); | ||
expect(cmykString2Object('cmyk(0, 50, 20, 100)')).toEqual({ c: 0, m: 50, y: 20, k: 100 }); | ||
expect(function () { return cmykString2Object('600, 50, 20, 100'); }).toThrow(); | ||
expect(function () { return cmykString2Object('cmyk(600, 50, 20, 100)'); }).toThrow(); | ||
}); | ||
//# sourceMappingURL=cmyk.test.js.map |
@@ -1,2 +0,2 @@ | ||
import { hsl2hex, hsl2rgb, hsl2cmyk } from '../index'; | ||
import { hsl2hex, hsl2rgb, hsl2cmyk, hslString2Object } from '../index'; | ||
//////////////////////////////////////////////////////// | ||
@@ -48,2 +48,15 @@ // hsl2hex | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// hslString2Object | ||
//////////////////////////////////////////////////////// | ||
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(); | ||
}); | ||
//# sourceMappingURL=hsl.test.js.map |
@@ -1,2 +0,2 @@ | ||
import { rgb2hex, rgb2cmyk, rgb2hsl, rgba2rgb, rgb2rgba, color2rgb } from '../index'; | ||
import { rgb2hex, rgb2cmyk, rgb2hsl, rgba2rgb, rgb2rgba, color2rgb, rgbString2Object, rgbaString2Object, } from '../index'; | ||
//////////////////////////////////////////////////////// | ||
@@ -54,2 +54,28 @@ // rgb2hex | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// rgbString2Object | ||
//////////////////////////////////////////////////////// | ||
test("rgbString2Object", function () { | ||
expect(rgbString2Object('255, 0, 255')).toEqual({ r: 255, g: 0, b: 255 }); | ||
expect(rgbString2Object('255,0, 255')).toEqual({ r: 255, g: 0, b: 255 }); | ||
expect(rgbString2Object('255, 0, 255')).toEqual({ r: 255, g: 0, b: 255 }); | ||
expect(rgbString2Object('rgb(255, 0, 255)')).toEqual({ r: 255, g: 0, b: 255 }); | ||
expect(rgbString2Object('rgb(255,0, 255)')).toEqual({ r: 255, g: 0, b: 255 }); | ||
expect(rgbString2Object('rgb(255, 0, 255)')).toEqual({ r: 255, g: 0, b: 255 }); | ||
expect(function () { return rgbString2Object('600, 0, 255'); }).toThrow(); | ||
expect(function () { return rgbString2Object('rgb(600, 0, 255)'); }).toThrow(); | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// rgbaString2Object | ||
//////////////////////////////////////////////////////// | ||
test("rgbaString2Object", function () { | ||
expect(rgbaString2Object('255, 0, 255, 0.5')).toEqual({ r: 255, g: 0, b: 255, a: 0.5 }); | ||
expect(rgbaString2Object('255,0, 255, 0.5')).toEqual({ r: 255, g: 0, b: 255, a: 0.5 }); | ||
expect(rgbaString2Object('255, 0, 255, 0.5')).toEqual({ r: 255, g: 0, b: 255, a: 0.5 }); | ||
expect(rgbaString2Object('rgba(255, 0, 255, 0.5)')).toEqual({ r: 255, g: 0, b: 255, a: 0.5 }); | ||
expect(rgbaString2Object('rgba(255,0, 255, 0.5)')).toEqual({ r: 255, g: 0, b: 255, a: 0.5 }); | ||
expect(rgbaString2Object('rgba(255, 0, 255, 0.5)')).toEqual({ r: 255, g: 0, b: 255, a: 0.5 }); | ||
expect(function () { return rgbaString2Object('600, 0, 255, 0.5'); }).toThrow(); | ||
expect(function () { return rgbaString2Object('rgba(600, 0, 255, 0.5)'); }).toThrow(); | ||
}); | ||
//# sourceMappingURL=rgb.test.js.map |
{ | ||
"name": "colors-convert", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
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
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
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
3951134
19510