@ant-design/fast-color
Advanced tools
Comparing version 1.2.4 to 1.2.5
@@ -64,12 +64,15 @@ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
if (typeof input === 'string') { | ||
const trimed = input.trim(); | ||
if (trimed[0] === '#') { | ||
this.fromHexString(trimed); | ||
} else if (trimed.startsWith('rgb')) { | ||
this.fromRgbString(trimed); | ||
} else if (trimed.startsWith('hsl')) { | ||
this.fromHslString(trimed); | ||
} else if (trimed.startsWith('hsv') || trimed.startsWith('hsb')) { | ||
this.fromHsvString(trimed); | ||
const trimStr = input.trim(); | ||
function matchPrefix(prefix) { | ||
return trimStr.startsWith(prefix); | ||
} | ||
if (/^#?[A-F\d]{3,8}$/i.test(trimStr)) { | ||
this.fromHexString(trimStr); | ||
} else if (matchPrefix('rgb')) { | ||
this.fromRgbString(trimStr); | ||
} else if (matchPrefix('hsl')) { | ||
this.fromHslString(trimStr); | ||
} else if (matchPrefix('hsv') || matchPrefix('hsb')) { | ||
this.fromHsvString(trimStr); | ||
} | ||
} else if ('r' in input && 'g' in input && 'b' in input) { | ||
@@ -355,15 +358,19 @@ this.r = input.r; | ||
} | ||
fromHexString(trimed) { | ||
if (trimed.length < 6) { | ||
fromHexString(trimStr) { | ||
const withoutPrefix = trimStr.replace('#', ''); | ||
function connectNum(index1, index2) { | ||
return parseInt(withoutPrefix[index1] + withoutPrefix[index2 || index1], 16); | ||
} | ||
if (withoutPrefix.length < 6) { | ||
// #rgb or #rgba | ||
this.r = parseInt(trimed[1] + trimed[1], 16); | ||
this.g = parseInt(trimed[2] + trimed[2], 16); | ||
this.b = parseInt(trimed[3] + trimed[3], 16); | ||
this.a = trimed[4] ? parseInt(trimed[4] + trimed[4], 16) / 255 : 1; | ||
this.r = connectNum(0); | ||
this.g = connectNum(1); | ||
this.b = connectNum(2); | ||
this.a = withoutPrefix[3] ? connectNum(3) / 255 : 1; | ||
} else { | ||
// #rrggbb or #rrggbbaa | ||
this.r = parseInt(trimed[1] + trimed[2], 16); | ||
this.g = parseInt(trimed[3] + trimed[4], 16); | ||
this.b = parseInt(trimed[5] + trimed[6], 16); | ||
this.a = trimed[8] ? parseInt(trimed[7] + trimed[8], 16) / 255 : 1; | ||
this.r = connectNum(0, 1); | ||
this.g = connectNum(2, 3); | ||
this.b = connectNum(4, 5); | ||
this.a = withoutPrefix[6] ? connectNum(6, 7) / 255 : 1; | ||
} | ||
@@ -370,0 +377,0 @@ } |
@@ -19,2 +19,7 @@ export interface RGB { | ||
} | ||
export type ColorInput = string | RGB | HSL | HSV; | ||
export type OptionalA<T extends { | ||
a: number; | ||
}> = Omit<T, 'a'> & { | ||
a?: number; | ||
}; | ||
export type ColorInput = string | OptionalA<RGB> | OptionalA<HSL> | OptionalA<HSV>; |
@@ -71,12 +71,15 @@ "use strict"; | ||
if (typeof input === 'string') { | ||
const trimed = input.trim(); | ||
if (trimed[0] === '#') { | ||
this.fromHexString(trimed); | ||
} else if (trimed.startsWith('rgb')) { | ||
this.fromRgbString(trimed); | ||
} else if (trimed.startsWith('hsl')) { | ||
this.fromHslString(trimed); | ||
} else if (trimed.startsWith('hsv') || trimed.startsWith('hsb')) { | ||
this.fromHsvString(trimed); | ||
const trimStr = input.trim(); | ||
function matchPrefix(prefix) { | ||
return trimStr.startsWith(prefix); | ||
} | ||
if (/^#?[A-F\d]{3,8}$/i.test(trimStr)) { | ||
this.fromHexString(trimStr); | ||
} else if (matchPrefix('rgb')) { | ||
this.fromRgbString(trimStr); | ||
} else if (matchPrefix('hsl')) { | ||
this.fromHslString(trimStr); | ||
} else if (matchPrefix('hsv') || matchPrefix('hsb')) { | ||
this.fromHsvString(trimStr); | ||
} | ||
} else if ('r' in input && 'g' in input && 'b' in input) { | ||
@@ -362,15 +365,19 @@ this.r = input.r; | ||
} | ||
fromHexString(trimed) { | ||
if (trimed.length < 6) { | ||
fromHexString(trimStr) { | ||
const withoutPrefix = trimStr.replace('#', ''); | ||
function connectNum(index1, index2) { | ||
return parseInt(withoutPrefix[index1] + withoutPrefix[index2 || index1], 16); | ||
} | ||
if (withoutPrefix.length < 6) { | ||
// #rgb or #rgba | ||
this.r = parseInt(trimed[1] + trimed[1], 16); | ||
this.g = parseInt(trimed[2] + trimed[2], 16); | ||
this.b = parseInt(trimed[3] + trimed[3], 16); | ||
this.a = trimed[4] ? parseInt(trimed[4] + trimed[4], 16) / 255 : 1; | ||
this.r = connectNum(0); | ||
this.g = connectNum(1); | ||
this.b = connectNum(2); | ||
this.a = withoutPrefix[3] ? connectNum(3) / 255 : 1; | ||
} else { | ||
// #rrggbb or #rrggbbaa | ||
this.r = parseInt(trimed[1] + trimed[2], 16); | ||
this.g = parseInt(trimed[3] + trimed[4], 16); | ||
this.b = parseInt(trimed[5] + trimed[6], 16); | ||
this.a = trimed[8] ? parseInt(trimed[7] + trimed[8], 16) / 255 : 1; | ||
this.r = connectNum(0, 1); | ||
this.g = connectNum(2, 3); | ||
this.b = connectNum(4, 5); | ||
this.a = withoutPrefix[6] ? connectNum(6, 7) / 255 : 1; | ||
} | ||
@@ -377,0 +384,0 @@ } |
@@ -19,2 +19,7 @@ export interface RGB { | ||
} | ||
export type ColorInput = string | RGB | HSL | HSV; | ||
export type OptionalA<T extends { | ||
a: number; | ||
}> = Omit<T, 'a'> & { | ||
a?: number; | ||
}; | ||
export type ColorInput = string | OptionalA<RGB> | OptionalA<HSL> | OptionalA<HSV>; |
{ | ||
"name": "@ant-design/fast-color", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "fast and small color class", | ||
@@ -5,0 +5,0 @@ "engines": { |
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
41409
1260