UNPKG

toosoon-utils

Version:
106 lines (105 loc) 3.94 kB
import type { ColorRepresentation } from '../../types'; import Color, { type ColorInterpolationsParameters, type ColorInterpolation } from './Color'; import ColorScale, { type ColorScaleProcessingParameters } from './ColorScale'; /** * Parameters used for color palette generation */ export type ColorPaletteGenerationParameters = { /** * Pre-processing parameters, for processing the color palette base color before generation */ preprocessing?: ColorScaleProcessingParameters; /** * Post-processing parameters, for processing the generated color scale after generation */ postprocessing?: ColorScaleProcessingParameters; }; /** * Interface representing a color palette generator */ export type ColorPaletteGenerator<I extends ColorInterpolation = ColorInterpolation> = { /** * Type of interpolation used for generation */ interpolation: I; /** * Amount of colors to generate */ length: number; /** * Target color to interpolate towards */ target?: Color | null; /** * Target color processing parameters, for processing the target color before generation */ processing?: ColorScaleProcessingParameters; /** * Generation parameters & Interpolation parameters */ params?: ColorPaletteGenerationParameters & ColorInterpolationsParameters[I]; }; /** * Utility class for generating color palettes * * @exports * @class ColorPalette */ export default class ColorPalette { readonly isColorPalette = true; readonly type: string; protected _base: Color; /** * Object containing this color palette color scales */ scales: Record<string, ColorScale>; /** * Object containing this color palette generators */ generators: Record<string, ColorPaletteGenerator>; /** * @param {Color|ColorRepresentation} color Base color * @param {object} generators Object containing generators */ constructor(color: Color | ColorRepresentation, generators?: Record<string, ColorPaletteGenerator>); /** * Pick an interpolated color from one of this color palette color scale * * @param {string} key Name of the color scale * @param {number} t Normalized time value to interpolate * @returns {Color} Interpolated color */ getColor(key: string, t: number): Color; /** * Add a given generator to this color palette * * @param {string} key Name of the generator * @param {ColorPaletteGenerator} generator Generator to add */ subscribe<I extends ColorInterpolation>(key: string, generator: ColorPaletteGenerator<I>): void; /** * Generate a color scale * * @param {ColorPaletteGenerator} generator Generator interface to use for generation * @param {ColorInterpolation} generator.interpolation Type of interpolation used for generation * @param {number} generator.length Amount of colors to generate * @param {Color} [generator.target] Target color to interpolate towards * @param {ColorScaleProcessingParameters} [generator.processing] Target color processing parameters, for processing the target color before generation * @param {object} [generator.params] Interpolation parameters * @returns {ColorScale} Generated color scale */ generate(generator: ColorPaletteGenerator): ColorScale; /** * Update this color palette color scales */ update(): void; /** * Base color of this color palette */ set base(color: Color | ColorRepresentation); get base(): Color; } export declare const LightScaleGenerator: ColorPaletteGenerator; export declare const DarkScaleGenerator: ColorPaletteGenerator; export declare const SequentialLightGenerator: ColorPaletteGenerator; export declare const SequentialDarkGenerator: ColorPaletteGenerator;