colors-convert
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -7,5 +7,10 @@ # Changelog | ||
- | ||
## 1.0.3 | ||
- | ||
## 1.4.0 | ||
- **[add]** add the following functions: `isRgbString`, `isRgbaString`, `isCmykString`, `isHslString`, `isHslaString`, `stringToColor` | ||
## 1.3.0 | ||
- **[fix]** `#00000` is not a valid hex color | ||
@@ -12,0 +17,0 @@ - **[add]** add the following functions: `colorToHex`, `color2rgba`, `rgbaToHex`, `cmykToRgba`, `cmykToHsla`, `hexToHsla`, `rgbToHsla`, `rgbaToHsla`, `hslToRgba`, `colorToCmyk`, `colorToHsl`, `hslToHsla`, `colorToHsla`, `hslaStringToObject`, `hexToRgb`, `hexToRgba`, `shortToLongHex` |
@@ -145,3 +145,3 @@ import { orRegex } from '../utils/regex-utils'; | ||
* | ||
* ✓ 'cmyk(0, 0, 0, 5)', 'cmyk(0,0, 0, 1)', 'cmyk(0, 0, 0, 0)', 'cmyk(255, 0, 4, 0)', 'cmyk(244, 0, 300, 0)' | ||
* ✓ 'cmyk(0, 0, 0, 5)', 'cmyk(0,0, 0, 1)', 'cmyk(0, 0, 0, 0)', 'cmyk(255, 0, 4, 0)', 'cmyk(244, 0, 300, 0)' | ||
* ✗ '(-1, 0, 0, 0)' | ||
@@ -148,0 +148,0 @@ */ |
@@ -1,3 +0,3 @@ | ||
export { isHex, isRgb, isRgba, isCmyk, isHsl, isHsla, isColor } from './lib/types/isType'; | ||
export { color2string, colorToString, color2cssString, colorToCssString, } from './lib/color-models/color'; | ||
export { isHex, isRgb, isRgbString, isRgba, isRgbaString, isCmyk, isCmykString, isHsl, isHslString, isHsla, isHslaString, isColor, } from './lib/types/isType'; | ||
export { color2string, colorToString, color2cssString, colorToCssString, stringToColor, } from './lib/color-models/color'; | ||
export { hex2rgbOrRgba, hexToRgbOrRgba, hexToRgb, hex2rgba, hexToRgba, hex2hexWithAlpha, hexToHexWithAlpha, hex2cmyk, hexToCmyk, hex2hsl, hexToHsl, hexToHsla, shortToLongHex, colorToHex, } from './lib/color-models/hex'; | ||
@@ -4,0 +4,0 @@ export { rgb2hex, rgbToHex, rgb2cmyk, rgbToCmyk, rgb2hsl, rgbToHsl, rgbToHsla, rgb2rgba, rgbToRgba, color2rgb, colorToRgb, rgbString2Object, rgbStringToObject, } from './lib/color-models/rgb'; |
@@ -1,3 +0,3 @@ | ||
export { isHex, isRgb, isRgba, isCmyk, isHsl, isHsla, isColor } from './lib/types/isType'; | ||
export { color2string, colorToString, color2cssString, colorToCssString, } from './lib/color-models/color'; | ||
export { isHex, isRgb, isRgbString, isRgba, isRgbaString, isCmyk, isCmykString, isHsl, isHslString, isHsla, isHslaString, isColor, } from './lib/types/isType'; | ||
export { color2string, colorToString, color2cssString, colorToCssString, stringToColor, } from './lib/color-models/color'; | ||
export { hex2rgbOrRgba, hexToRgbOrRgba, hexToRgb, hex2rgba, hexToRgba, hex2hexWithAlpha, hexToHexWithAlpha, hex2cmyk, hexToCmyk, hex2hsl, hexToHsl, hexToHsla, shortToLongHex, colorToHex, } from './lib/color-models/hex'; | ||
@@ -4,0 +4,0 @@ export { rgb2hex, rgbToHex, rgb2cmyk, rgbToCmyk, rgb2hsl, rgbToHsl, rgbToHsla, rgb2rgba, rgbToRgba, color2rgb, colorToRgb, rgbString2Object, rgbStringToObject, } from './lib/color-models/rgb'; |
@@ -28,1 +28,7 @@ import { Color } from '../types/types'; | ||
export declare function color2cssString(color: Color): string; | ||
/** | ||
* Check if a string is a valid color and if so, transform it to the right Color. | ||
* @param string to convert to a Color | ||
* @returns Color | ||
*/ | ||
export declare function stringToColor(stringColor: string): Color; |
@@ -1,2 +0,2 @@ | ||
import { isHex, isRgb, isRgba, isCmyk, isHsl, isColor } from '../types/isType'; | ||
import { isHex, isRgb, isRgba, isCmyk, isHsl, isColor, isRgbString, isRgbaString, isCmykString, isHslString, isHslaString, } from '../types/isType'; | ||
import { toUpper } from 'lodash'; | ||
@@ -6,2 +6,7 @@ import { notValidColorMessage } from '../../utils/logs-utils'; | ||
import { DELETE_VERSION_2, DEPRECATE_VERSION_2 } from '../../constants/constants'; | ||
import { rgbStringToObject } from './rgb'; | ||
import { rgbaStringToObject } from './rgba'; | ||
import { cmykStringToObject } from './cmyk'; | ||
import { hslStringToObject } from './hsl'; | ||
import { hslaStringToObject } from './hsla'; | ||
/** | ||
@@ -69,2 +74,24 @@ * Convert a color to a string format. | ||
} | ||
/** | ||
* Check if a string is a valid color and if so, transform it to the right Color. | ||
* @param string to convert to a Color | ||
* @returns Color | ||
*/ | ||
export function stringToColor(stringColor) { | ||
if (typeof stringColor !== 'string') | ||
throw new Error(stringToColor + ": \"" + stringColor + "\" is not a string."); | ||
if (isHex(stringColor)) | ||
return toUpper(stringColor); | ||
if (isRgbString(stringColor)) | ||
return rgbStringToObject(stringColor); | ||
if (isRgbaString(stringColor)) | ||
return rgbaStringToObject(stringColor); | ||
if (isCmykString(stringColor)) | ||
return cmykStringToObject(stringColor); | ||
if (isHslString(stringColor)) | ||
return hslStringToObject(stringColor); | ||
if (isHslaString(stringColor)) | ||
return hslaStringToObject(stringColor); | ||
throw new Error(stringToColor + ": \"" + stringColor + "\" is not a valid color string."); | ||
} | ||
//# sourceMappingURL=color.js.map |
@@ -1,2 +0,3 @@ | ||
import { colorToString, colorToCssString } from '../../index'; | ||
import { colorToString, colorToCssString, stringToColor, isHex } from '../../index'; | ||
import { isCmyk, isHsl, isHsla, isRgb, isRgba } from '../types/isType'; | ||
//////////////////////////////////////////////////////// | ||
@@ -26,2 +27,15 @@ // colorToString | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// stringToColor | ||
//////////////////////////////////////////////////////// | ||
test("stringToColor", function () { | ||
expect(isHex(stringToColor('#000000'))).toBe(true); | ||
expect(isHex(stringToColor('#ffF000'))).toBe(true); | ||
expect(isRgb(stringToColor('rgb(0, 0, 0)'))).toBe(true); | ||
expect(isRgba(stringToColor('rgba(0, 0, 0, 1)'))).toBe(true); | ||
expect(isCmyk(stringToColor('cmyk(0, 0, 0, 0)'))).toBe(true); | ||
expect(isHsl(stringToColor('hsl(0, 0%, 0%)'))).toBe(true); | ||
expect(isHsla(stringToColor('hsla(0, 0%, 0%, 0)'))).toBe(true); | ||
expect(function () { return stringToColor('#'); }).toThrowError(); | ||
}); | ||
//# sourceMappingURL=color.test.js.map |
@@ -62,3 +62,3 @@ var __assign = (this && this.__assign) || function () { | ||
throw new Error(notValidHexMessage('hexToRgb', hex)); | ||
var _a = hex2rgba(hex), r = _a.r, g = _a.g, b = _a.b; | ||
var _a = hexToRgba(hex), r = _a.r, g = _a.g, b = _a.b; | ||
return { r: r, g: g, b: b }; | ||
@@ -65,0 +65,0 @@ } |
@@ -42,3 +42,3 @@ import { HSL, RGB, CMYK, HEX, HSLA, RGBA, Color } from '../types/types'; | ||
* - 322, 79%, 52%, 0.5 (short format) -> { h: 322, s: 79, l: 52, a: 0.5 } | ||
* - hsls(322, 79%, 52%, 0.5) (long format) -> { h: 322, s: 79, l: 52, a: 0.5 }. | ||
* - hsla(322, 79%, 52%, 0.5) (long format) -> { h: 322, s: 79, l: 52, a: 0.5 }. | ||
* @param hsls string to convert to hsla object | ||
@@ -45,0 +45,0 @@ * @returns hsla object |
@@ -104,3 +104,3 @@ var __assign = (this && this.__assign) || function () { | ||
* - 322, 79%, 52%, 0.5 (short format) -> { h: 322, s: 79, l: 52, a: 0.5 } | ||
* - hsls(322, 79%, 52%, 0.5) (long format) -> { h: 322, s: 79, l: 52, a: 0.5 }. | ||
* - hsla(322, 79%, 52%, 0.5) (long format) -> { h: 322, s: 79, l: 52, a: 0.5 }. | ||
* @param hsls string to convert to hsla object | ||
@@ -107,0 +107,0 @@ * @returns hsla object |
@@ -18,2 +18,10 @@ import { HEX, RGB, RGBA, CMYK, HSL, HSLA, Color } from './types'; | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 255, 0, 255 (short format) | ||
* - rgb(255, 0, 255) (long format). | ||
* @param rgbString rgb string color to check to be a valid rgb string | ||
* @returns true if rgbString is a valid format, false otherwise | ||
*/ | ||
export declare function isRgbString(rgbString: string): rgbString is string; | ||
/** | ||
* Accept an object like this {r, g, b, a} with r, g, b numeric values in [0, 255] and a in [0, 1]. | ||
@@ -25,2 +33,10 @@ * @param color color to check if it is in the right rgba format | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 255, 0, 255 (short format) | ||
* - rgba(255, 0, 255) (long format). | ||
* @param rgbaString rgba string color to check to be a valid rgba string | ||
* @returns true if rgbaString is a valid format, false otherwise | ||
*/ | ||
export declare function isRgbaString(rgbaString: string): rgbaString is string; | ||
/** | ||
* Accept an object like this {c, m, y, k} with c, m, y, k numeric values in [0, 100]. | ||
@@ -32,2 +48,10 @@ * @param color color to check if it is in the right cmyk format | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 0, 50, 20, 100 (short format) | ||
* - cmyk(0, 50, 20, 100) (long format). | ||
* @param cmykString cmyk string color to check to be a valid cmyk string | ||
* @returns true if cmykString is a valid format, false otherwise | ||
*/ | ||
export declare function isCmykString(cmykString: string): cmykString is string; | ||
/** | ||
* Accept hsl colors with: | ||
@@ -42,2 +66,10 @@ * - h (hue): [0-359]° | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 322, 79%, 52% (short format) | ||
* - hsl(322, 79%, 52%) (long format). | ||
* @param hslString hsl string color to check to be a valid hsl string | ||
* @returns true if hslString is a valid format, false otherwise | ||
*/ | ||
export declare function isHslString(hslString: string): hslString is string; | ||
/** | ||
* Accept hsla colors with: | ||
@@ -53,2 +85,10 @@ * - h (hue): [0-359]° | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 322, 79%, 52%, 0.5 (short format) | ||
* - hsla(322, 79%, 52%, 0.5) (long format). | ||
* @param hslaString hsl string color to check to be a valid hsla string | ||
* @returns true if hslaString is a valid format, false otherwise | ||
*/ | ||
export declare function isHslaString(hslaString: string): hslaString is string; | ||
/** | ||
* Return true if color is hex, rgb, rgba, cmyk, hls or hsla, false otherwise | ||
@@ -55,0 +95,0 @@ * @param color color to check if it is in the right Color format |
import { between } from '../../utils/math-utils'; | ||
import { sameContent } from '../../utils/utils'; | ||
import { HEX_REGEX } from '../../constants/regex'; | ||
import { CMYK_REGEX, HEX_REGEX, HSLA_REGEX, HSL_REGEX, RGBA_REGEX, RGB_REGEX, } from '../../constants/regex'; | ||
/** | ||
@@ -35,2 +35,18 @@ * Accept: | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 255, 0, 255 (short format) | ||
* - rgb(255, 0, 255) (long format). | ||
* @param rgbString rgb string color to check to be a valid rgb string | ||
* @returns true if rgbString is a valid format, false otherwise | ||
*/ | ||
export function isRgbString(rgbString) { | ||
// check short and long formats | ||
var isShortFormat = RGB_REGEX.short.test(rgbString); | ||
var isLongFormat = RGB_REGEX.long.test(rgbString); | ||
if (!isShortFormat && !isLongFormat) | ||
return false; | ||
else | ||
return true; | ||
} | ||
/** | ||
* Accept an object like this {r, g, b, a} with r, g, b numeric values in [0, 255] and a in [0, 1]. | ||
@@ -54,2 +70,18 @@ * @param color color to check if it is in the right rgba format | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 255, 0, 255 (short format) | ||
* - rgba(255, 0, 255) (long format). | ||
* @param rgbaString rgba string color to check to be a valid rgba string | ||
* @returns true if rgbaString is a valid format, false otherwise | ||
*/ | ||
export function isRgbaString(rgbaString) { | ||
// check short and long formats | ||
var isShortFormat = RGBA_REGEX.short.test(rgbaString); | ||
var isLongFormat = RGBA_REGEX.long.test(rgbaString); | ||
if (!isShortFormat && !isLongFormat) | ||
return false; | ||
else | ||
return true; | ||
} | ||
/** | ||
* Accept an object like this {c, m, y, k} with c, m, y, k numeric values in [0, 100]. | ||
@@ -74,2 +106,18 @@ * @param color color to check if it is in the right cmyk format | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 0, 50, 20, 100 (short format) | ||
* - cmyk(0, 50, 20, 100) (long format). | ||
* @param cmykString cmyk string color to check to be a valid cmyk string | ||
* @returns true if cmykString is a valid format, false otherwise | ||
*/ | ||
export function isCmykString(cmykString) { | ||
// check short and long formats | ||
var isShortFormat = CMYK_REGEX.short.test(cmykString); | ||
var isLongFormat = CMYK_REGEX.long.test(cmykString); | ||
if (!isShortFormat && !isLongFormat) | ||
return false; | ||
else | ||
return true; | ||
} | ||
/** | ||
* Accept hsl colors with: | ||
@@ -97,2 +145,18 @@ * - h (hue): [0-359]° | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 322, 79%, 52% (short format) | ||
* - hsl(322, 79%, 52%) (long format). | ||
* @param hslString hsl string color to check to be a valid hsl string | ||
* @returns true if hslString is a valid format, false otherwise | ||
*/ | ||
export function isHslString(hslString) { | ||
// check short and long formats | ||
var isShortFormat = HSL_REGEX.short.test(hslString); | ||
var isLongFormat = HSL_REGEX.long.test(hslString); | ||
if (!isShortFormat && !isLongFormat) | ||
return false; | ||
else | ||
return true; | ||
} | ||
/** | ||
* Accept hsla colors with: | ||
@@ -118,2 +182,18 @@ * - h (hue): [0-359]° | ||
/** | ||
* Check if a string is in these two formats: | ||
* - 322, 79%, 52%, 0.5 (short format) | ||
* - hsla(322, 79%, 52%, 0.5) (long format). | ||
* @param hslaString hsl string color to check to be a valid hsla string | ||
* @returns true if hslaString is a valid format, false otherwise | ||
*/ | ||
export function isHslaString(hslaString) { | ||
// check short and long formats | ||
var isShortFormat = HSLA_REGEX.short.test(hslaString); | ||
var isLongFormat = HSLA_REGEX.long.test(hslaString); | ||
if (!isShortFormat && !isLongFormat) | ||
return false; | ||
else | ||
return true; | ||
} | ||
/** | ||
* Return true if color is hex, rgb, rgba, cmyk, hls or hsla, false otherwise | ||
@@ -120,0 +200,0 @@ * @param color color to check if it is in the right Color format |
@@ -1,2 +0,2 @@ | ||
import { isHex, isRgb, isRgba, isCmyk, isHsl, isHsla, isColor } from '../../index'; | ||
import { isHex, isRgb, isRgbString, isRgba, isRgbaString, isCmyk, isCmykString, isHsl, isHslString, isHsla, isHslaString, isColor, } from '../../index'; | ||
//////////////////////////////////////////////////////// | ||
@@ -37,2 +37,10 @@ // isHex | ||
//////////////////////////////////////////////////////// | ||
// isRgbString | ||
//////////////////////////////////////////////////////// | ||
test("isRgbString", function () { | ||
expect(isRgbString('255, 0, 255')).toBe(true); | ||
expect(isRgbString('rgb(255, 0, 255)')).toBe(true); | ||
expect(isRgbString('')).toBe(false); | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// isRgba | ||
@@ -53,2 +61,10 @@ //////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////// | ||
// isRgbaString | ||
//////////////////////////////////////////////////////// | ||
test("isRgbaString", function () { | ||
expect(isRgbaString('255, 0, 255, 1')).toBe(true); | ||
expect(isRgbaString('rgba(255, 0, 255, 1)')).toBe(true); | ||
expect(isRgbaString('')).toBe(false); | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// isCmyk | ||
@@ -70,2 +86,10 @@ //////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////// | ||
// isCmykString | ||
//////////////////////////////////////////////////////// | ||
test("isCmykString", function () { | ||
expect(isCmykString('0, 50, 20, 100')).toBe(true); | ||
expect(isCmykString('cmyk(0, 50, 20, 100)')).toBe(true); | ||
expect(isCmykString('')).toBe(false); | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// isHsl | ||
@@ -83,2 +107,10 @@ //////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////// | ||
// isHslString | ||
//////////////////////////////////////////////////////// | ||
test("isHslString", function () { | ||
expect(isHslString('322, 79%, 52%')).toBe(true); | ||
expect(isHslString('hsl(322, 79%, 52%)')).toBe(true); | ||
expect(isHslString('')).toBe(false); | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// isHsla | ||
@@ -96,2 +128,10 @@ //////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////// | ||
// isHslaString | ||
//////////////////////////////////////////////////////// | ||
test("isHslaString", function () { | ||
expect(isHslaString('322, 79%, 52%, 0.5')).toBe(true); | ||
expect(isHslaString('hsla(322, 79%, 52%, 0.5)')).toBe(true); | ||
expect(isHslaString('')).toBe(false); | ||
}); | ||
//////////////////////////////////////////////////////// | ||
// isColor | ||
@@ -98,0 +138,0 @@ //////////////////////////////////////////////////////// |
{ | ||
"name": "colors-convert", | ||
"description": "A simple color library", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"repository": { | ||
@@ -68,2 +68,2 @@ "type": "git", | ||
} | ||
} | ||
} |
@@ -54,2 +54,3 @@ <div align="center" style="text-align: center"> | ||
A valid rgba color is an object like this `{r, g, b, a}` with `r, g, b` numeric values in `[0, 255]` and `a` in `[0, 1]`. | ||
#### Hsl | ||
@@ -56,0 +57,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
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
4188950
22716
122