Resolve canvasGradient is undefined in node (#10328)

* Resolve canvasgradient is not defined in node

* Remove trailing white space

* export isPaternOrGradient helper with typings

* fix lint failure, single qoute

* Allow for string inputs too to function
This commit is contained in:
Jacco van den Berg 2022-05-03 14:21:43 +02:00 committed by GitHub
parent a9765042f1
commit 2c268f0943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -1,6 +1,13 @@
import colorLib from '@kurkle/color';
const isPatternOrGradient = (value) => value instanceof CanvasGradient || value instanceof CanvasPattern;
export function isPatternOrGradient(value) {
if (value && typeof value === 'object') {
const type = value.toString();
return type === '[object CanvasPattern]' || type === '[object CanvasGradient]';
}
return false;
}
export function color(value) {
return isPatternOrGradient(value) ? value : colorLib(value);

View File

@ -1,3 +1,5 @@
import { AnyObject } from '../basic';
export function color(value: CanvasGradient): CanvasGradient;
export function color(value: CanvasPattern): CanvasPattern;
export function color(
@ -8,6 +10,8 @@ export function color(
| [number, number, number, number]
): ColorModel;
export function isPatternOrGradient(value: string | AnyObject): boolean;
export interface ColorModel {
rgbString(): string;
hexString(): string;