diff --git a/src/gradient/parser/parseColorStops.ts b/src/gradient/parser/parseColorStops.ts index bff727e3c5d..182ac0ceb81 100644 --- a/src/gradient/parser/parseColorStops.ts +++ b/src/gradient/parser/parseColorStops.ts @@ -1,6 +1,5 @@ import { Color } from "../../color"; import { parsePercent } from "../../parser/percent"; -import { ifNaN } from "../../util/internals"; const RE_KEY_VALUE_PAIRS = /\s*;\s*/; const RE_KEY_VALUE = /\s*:\s*/; @@ -29,16 +28,16 @@ function parseColorStop(el: SVGStopElement, multiplier: number) { const color = new Color(colorValue || el.getAttribute('stop-color') || 'rgb(0,0,0)'); return { - offset: parsePercent(el.getAttribute('offset'), 0), + offset: parsePercent(el.getAttribute('offset')) || 0, color: color.toRgb(), - opacity: ifNaN(parseFloat(opacity || el.getAttribute('stop-opacity') || ''), 1) * color.getAlpha() * multiplier + opacity: (parseFloat(opacity || el.getAttribute('stop-opacity') || '') || 1) * color.getAlpha() * multiplier }; } export function parseColorStops(el: SVGGradientElement, opacityAttr: string | null) { const colorStops = [], colorStopEls = el.getElementsByTagName('stop'), - multiplier = parsePercent(opacityAttr, 1); + multiplier = parsePercent(opacityAttr) || 1; for (let i = colorStopEls.length; i--;) { colorStops.push(parseColorStop(colorStopEls[i], multiplier)); } diff --git a/src/parser/percent.ts b/src/parser/percent.ts index 3f8d27d545a..edd0717def2 100644 --- a/src/parser/percent.ts +++ b/src/parser/percent.ts @@ -1,5 +1,4 @@ -import { clamp } from "../util"; -import { ifNaN } from "../util/internals"; +import { capValue } from "../util/misc/capValue"; const RE_PERCENT = /^(\d+\.\d+)%|(\d+)%$/; @@ -13,11 +12,11 @@ export function isPercent(value: string | null) { * @param valueIfNaN * @returns ∈ [0, 1] */ -export function parsePercent(value: string | number | null | undefined, valueIfNaN?: number) { +export function parsePercent(value: string | number | null | undefined) { const parsed = typeof value === 'number' ? value : typeof value === 'string' ? parseFloat(value) / (isPercent(value) ? 100 : 1) : NaN; - return clamp(0, ifNaN(parsed, valueIfNaN), 1) + return capValue(0, parsed, 1) } diff --git a/src/util/internals/ifNaN.ts b/src/util/internals/ifNaN.ts deleted file mode 100644 index 70c2d109396..00000000000 --- a/src/util/internals/ifNaN.ts +++ /dev/null @@ -1,10 +0,0 @@ - -/** - * - * @param value value to check if NaN - * @param [valueIfNaN] - * @returns `fallback` is `value is NaN - */ -export const ifNaN = (value: number, valueIfNaN?: number) => { - return isNaN(value) && typeof valueIfNaN === 'number' ? valueIfNaN : value; -} \ No newline at end of file diff --git a/src/util/internals/index.ts b/src/util/internals/index.ts index e7de3661173..4a3d190d949 100644 --- a/src/util/internals/index.ts +++ b/src/util/internals/index.ts @@ -1,3 +1,2 @@ export { getRandomInt } from './getRandomInt'; -export { ifNaN } from './ifNaN'; export { removeFromArray } from './removeFromArray';