Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow getColor to work with an array of color types #453

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/getColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import parse from "./parse.js";

/**
* Resolves a color reference (object or string) to a plain color object
* @param {Color | {space, coords, alpha} | string} color
* @returns {{space, coords, alpha}}
* @param {Color | {space, coords, alpha} | string | Array<Color | {space, coords, alpha} | string> } color
* @returns {{space, coords, alpha} | Array<{space, coords, alpha}}>
*/
export default function getColor (color) {
if (Array.isArray(color)) {
jgerigmeyer marked this conversation as resolved.
Show resolved Hide resolved
return color.map(getColor);
}

if (!color) {
throw new TypeError("Empty color reference");
}
Expand Down
1 change: 1 addition & 0 deletions types/src/getColor.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PlainColorObject, ColorTypes } from "./color.js";

export default function getColor (color: ColorTypes): PlainColorObject;
export default function getColor (color: ColorTypes[]): PlainColorObject[];
7 changes: 5 additions & 2 deletions types/test/getColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ import getColor from "colorjs.io/src/getColor";
// @ts-expect-error
getColor();

getColor("red");
getColor(new Color("red"));
getColor("red"); // $ExpectType PlainColorObject
getColor(new Color("red")); // $ExpectType PlainColorObject

getColor(["red", "blue"]); // $ExpectType PlainColorObject[]