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

common.utils.colors #2623

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
6 changes: 6 additions & 0 deletions src/foundry/common/types.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ declare global {
height: number;
};

type RGBColorVector = [r: number, g: number, b: number];
type HSVColorVector = [h: number, s: number, v: number];
type HSLColorVector = [h: number, s: number, l: number];

type ColorSource = number | RGBColorVector | string | Color;

/** A Client Setting */
interface SettingConfig<T = unknown> {
/** A unique machine-readable id for the setting */
Expand Down
57 changes: 51 additions & 6 deletions src/foundry/common/utils/color.d.mts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
type RGBColorVector = [r: number, g: number, b: number];
type HSVColorVector = [h: number, s: number, v: number];

export default Color;
/**
* A representation of a color in hexadecimal format.
* This class provides methods for transformations and manipulations of colors.
*/
// @ts-expect-error 2417: Override of Color.fromString does not match Number.fromString
declare class Color extends Number {
/**
* Is this a valid color?
*/
get valid(): boolean;

/**
* A CSS-compatible color string.
* If this color is not valid, the empty string is returned.
* An alias for Color#toString.
*/
get css(): string;
Expand Down Expand Up @@ -51,13 +54,38 @@ declare class Color extends Number {

/**
* The color represented as an HSV array.
* Conversion formula adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Conversion formula adapted from {@link http://en.wikipedia.org/wiki/HSV_color_space}.
* Assumes r, g, and b are contained in the set [0, 1] and returns h, s, and v in the set [0, 1].
*/
get hsv(): HSVColorVector;

/**
* The color represented as an HSL array.
* Assumes r, g, and b are contained in the set [0, 1] and returns h, s, and l in the set [0, 1].
*/
get hsl(): HSLColorVector;

/**
* The color represented as a linear RGB array.
* Assumes r, g, and b are contained in the set [0, 1] and returns linear r, g, and b in the set [0, 1].
* {@link https://en.wikipedia.org/wiki/SRGB#Transformation}
*/
get linear(): RGBColorVector;

override toString(): string;

/**
* Serialize the Color.
* @returns The color as a CSS string
*/
toJSON(): string;

/**
* Returns the color as a CSS string.
* @returns The color as a CSS string
*/
toHTML(): string;

/**
* Test whether this color equals some other color
* @param other - Some other color or hex number
Expand Down Expand Up @@ -219,7 +247,7 @@ declare class Color extends Number {
* @param color - A color input
* @returns The hex color instance or NaN
*/
static from(color: null | string | number | RGBColorVector | Color): Color | number;
static from(color: ColorSource | null | undefined): Color;

/**
* Create a Color instance from a color string which either includes or does not include a leading #.
Expand All @@ -237,10 +265,27 @@ declare class Color extends Number {

/**
* Create a Color instance from an HSV array.
* Conversion formula adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Conversion formula adapted from {@link http://en.wikipedia.org/wiki/HSV_color_space}.
* Assumes h, s, and v are contained in the set [0, 1].
* @param hsv - An HSV tuple
* @returns The hex color instance
*/
static fromHSV(hsv: HSVColorVector): Color;

/**
* Create a Color instance from an HSL array.
* Assumes h, s, and l are contained in the set [0, 1].
* @param hsl - An HSL tuple
* @returns The hex color instance
*/
static fromHSL(hsl: HSLColorVector): Color;

/**
* Create a Color instance (sRGB) from a linear rgb array.
* Assumes r, g, and b are contained in the set [0, 1].
* {@link https://en.wikipedia.org/wiki/SRGB#Transformation}
* @param linear - The linear rgb array
* @returns The hex color instance
*/
static fromLinearRGB(linear: RGBColorVector): Color;
}
Loading