diff --git a/src/add-sample.ts b/src/add-sample.ts index 4326a80..b078203 100644 --- a/src/add-sample.ts +++ b/src/add-sample.ts @@ -1,4 +1,4 @@ -import { loadImage, srgbToOklab } from './utils' +import { isImageElement, loadImage, srgbToOklab } from './utils' import type { Context } from './context' interface AddSampleOptions { @@ -35,7 +35,7 @@ export function addSample( options, ) }) - } else if (source instanceof HTMLImageElement) { + } else if (isImageElement(source)) { if (!context2d) return context2d.canvas.width = source.width context2d.canvas.height = source.height diff --git a/src/create-palette.ts b/src/create-palette.ts index eef7efa..8278357 100644 --- a/src/create-palette.ts +++ b/src/create-palette.ts @@ -10,7 +10,7 @@ import type { Context } from './context' import type { Palette } from './palette' import type { Options } from './options' -export function createPalette(options: Options | Context): Palette { +export function createPalette(options?: Options | Context): Palette { const context = orCreateContext(options) const { diff --git a/src/utils.ts b/src/utils.ts index ee75797..ee79f84 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import type { ColorSample, Oklab, Sort } from './types' export const IN_BROWSER = typeof window !== 'undefined' +export const isImageElement = (node: any): node is HTMLImageElement => node && typeof node === 'object' && node.nodeType === 1 && node.tagName === 'IMG' const K = (1 << 16) - 1 const K2 = K * K