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

[types] Fix signature of steps and mix functions #323

Merged
merged 2 commits into from
Aug 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import deltaEMethods, {
deltaEITP,
deltaEOK,
} from "./deltaE/index";
import { mix, range, steps } from "./interpolation";
import { range, Range, MixOptions, StepsOptions } from "./interpolation";
import { getLuminance } from "./luminance";
import { lighten, darken } from "./variations";

Expand Down Expand Up @@ -67,12 +67,31 @@ declare module "./color" {
static deltaEMethods: typeof deltaEMethods;

// interpolation
mix: ToColorPrototype<typeof mix>;
// These signatures should always match those in interpolation.d.ts,
// including the static versions
mix(color2: ColorTypes, options?: MixOptions): Color;
mix(color2: ColorTypes, p: number, options?: MixOptions): Color;
range: ToColorPrototype<typeof range>;
steps: ToColorPrototype<typeof steps>;
static mix: typeof mix;
steps(color2: ColorTypes, options?: StepsOptions): Color[];

static mix(
color1: ColorTypes,
color2: ColorTypes,
options?: MixOptions
): Color;
static mix(
color1: ColorTypes,
color2: ColorTypes,
p: number,
options?: MixOptions
): Color;
static range: typeof range;
static steps: typeof steps;
static steps(
color1: ColorTypes,
color2: ColorTypes,
options?: StepsOptions
): Color[];
static steps(range: Range, options?: StepsOptions): Color[];

// luminance
get luminance(): ReturnType<typeof getLuminance>;
Expand Down
17 changes: 16 additions & 1 deletion types/test/interpolation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Color from "colorjs.io/src/color";
import Color from "colorjs.io/src";
import { sRGB, sRGB_Linear } from "colorjs.io/src/index-fn";
import {
isRange,
Expand Down Expand Up @@ -47,6 +47,16 @@ mix("red", "blue", {
premultiplied: true,
});

// Test mix on Color class
Color.mix("red", "blue"); // $ExpectType Color
Color.mix("red", "blue", 123); // $ExpectType Color
Color.mix("red", "blue", { space: "srgb" }); // $ExpectType Color
Color.mix("red", "blue", 123, { space: "srgb" }); // $ExpectType Color
new Color("red").mix("blue"); // $ExpectType Color
new Color("red").mix("blue", 123); // $ExpectType Color
new Color("red").mix("blue", { space: "srgb" }); // $ExpectType Color
new Color("red").mix("blue", 123, { space: "srgb" }); // $ExpectType Color

steps("red", "blue"); // $ExpectType PlainColorObject[]
// $ExpectType PlainColorObject[]
steps("red", "blue", {
Expand All @@ -64,6 +74,11 @@ steps(r, {
maxSteps: 100,
});

// Test steps on Color class
Color.steps(Color.range("red", "blue")); // $ExpectType Color[]
Color.steps("red", "blue"); // $ExpectType Color[]
new Color("red").steps("blue"); // $ExpectType Color[]

// @ts-expect-error
steps(r, "blue"); // $ExpectType PlainColorObject[]

Expand Down