UNPKG

pencil.js

Version:

Nice modular interactive 2D drawing library.

119 lines (118 loc) 3.75 kB
/** * Color class * @class */ export default class Color { /** * Return an instance from a generic definition * @param {ColorDefinition} colorDefinition - Any valid color definition (see constructor) * @return {Color} */ static from(...colorDefinition: ColorDefinition): Color; /** * @typedef {Color|String|Number|Array<Number>} ColorDefinition */ /** * Color constructor * @param {ColorDefinition} colorDefinition - Many types accepted (other Color instance, color name, hex string, hex number, red/green/blue/alpha value) * @example * new Color("indigo"); // All CSS color names * new Color("#123456"); // Hex string definition * new Color("#123"); // Hex shorthand string definition, #123 <=> #112233 * new Color(0x123456); // Hex number definition * new Color(0.1, 0.2, 0.3); // Red, Green, Blue definition * Every definition can have one more optional parameter for alpha (opacity) * new Color("violet", 0.5); */ constructor(...colorDefinition: ColorDefinition); red: number; green: number; blue: number; alpha: number; /** * Create a new copy of this color * @return {Color} */ clone(): Color; /** * Return an array with red, green and blue value * @example [0.1, 0.2, 0.3] * @return {Array<Number>} */ get array(): number[]; /** * Return hexadecimal rgb notation * @example "#123456" * @return {String} */ get rgb(): string; /** * Return rgba notation * @example "rgba(10,20,30,0.5)" * @return {String} */ get rgba(): string; /** * Return the closest CSS color name * @example "aliceblue" * @return {String} */ get name(): string; /** * Change this values * @param {ColorDefinition} colorDefinition - Any supported color definition (see constructor) * @return {Color} Itself */ set(...colorDefinition: ColorDefinition): Color; /** * Change to its greyscale value * @return {Color} Itself */ grey(): Color; /** * Change hue value (0 = red, 0.5 = blue, 1 = red, 1.5 = blue ...) * @param {Number} value - Any value between 0 and 1 * @return {Color} Itself */ hue(value: number): Color; /** * Change saturation value (0 = grey, 1 = pure color) * @param {Number} value - Any value between 0 and 1 * @return {Color} Itself */ saturation(value: number): Color; /** * Change lightness value (0 = black, 0.5 = pure color, 1 = white) * @param {Number} value - Any value between 0 and 1 * @return {Color} Itself */ lightness(value: number): Color; /** * Invert the color value * @return {Color} Itself */ reverse(): Color; /** * Restrict color's channels to set amount of value * @param {Number} number - Number of allowed value on each channel * @return {Color} Itself */ level(number: number): Color; /** * Change the color toward another color * @param {ColorDefinition} colorDefinition - Any other color * @param {Number} ratio - Ratio of distance to move (0 = no change, 0.5 = equal mix, 1 = same as target color) * @return {Color} Itself */ lerp(colorDefinition: ColorDefinition, ratio: number): Color; /** * @return {String} */ toString(): string; /** * Return a json ready array * @return {Array<Number>} */ toJSON(): Array<number>; } export type ColorDefinition = Color | string | number | Array<number>;