ansimax 1.3.4 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -131,8 +131,43 @@ interface RGB {
131
131
  g: number;
132
132
  b: number;
133
133
  }
134
+ /**
135
+ * Type guard: true when `n` is a finite number (rejects NaN, ±Infinity,
136
+ * non-numbers). Useful for input validation.
137
+ *
138
+ * @since 1.3.5
139
+ */
140
+ declare const isFiniteNumber: (n: unknown) => n is number;
141
+ /**
142
+ * Coerce any value to a safe integer. Handles non-numbers, NaN, Infinity,
143
+ * and floats. Consolidates the `Math.max(0, Math.floor(Number(x) || 0))`
144
+ * pattern that appears across the codebase.
145
+ *
146
+ * @param value - Any value (will be coerced via `Number()`).
147
+ * @param fallback - Returned when `value` is non-finite. Default `0`.
148
+ * @param min - Lower bound (inclusive). Default `-Infinity`.
149
+ * @param max - Upper bound (inclusive). Default `Infinity`.
150
+ *
151
+ * @example
152
+ * ```ts
153
+ * safeInt('abc') // → 0
154
+ * safeInt(3.7) // → 3
155
+ * safeInt(-5, 0, 0, 100) // → 0 (clamped to min)
156
+ * safeInt(NaN, 50) // → 50 (fallback)
157
+ * safeInt(null, 1) // → 1 (fallback — null is not a real number)
158
+ * ```
159
+ *
160
+ * @since 1.3.5
161
+ */
162
+ declare const safeInt: (value: unknown, fallback?: number, min?: number, max?: number) => number;
134
163
  declare const clamp: (n: number, min: number, max: number) => number;
135
164
  declare const lerp: (a: number, b: number, t: number) => number;
165
+ /**
166
+ * Clamp + round a number to the 0–255 byte range. Exported as of v1.3.5.
167
+ *
168
+ * @since 1.3.5
169
+ */
170
+ declare const clampByte: (v: number) => number;
136
171
  /** Returns true when a string is a valid 3- or 6-digit hex color. */
137
172
  declare const isHexColor: (hex: string) => boolean;
138
173
  /**
@@ -142,8 +177,112 @@ declare const isHexColor: (hex: string) => boolean;
142
177
  declare const hexToRgb: (hex: string) => RGB;
143
178
  /** Converts R, G, B values to a hex string. Values are clamped to 0–255. */
144
179
  declare const rgbToHex: (r: number, g: number, b: number) => string;
145
- /** Linearly interpolates between two RGB colors. t is clamped to [0, 1]. */
146
- declare const lerpColor: (a: RGB, b: RGB, t: number) => RGB;
180
+ interface HSL {
181
+ /** Hue in degrees, 0–360 (wraps; 360 ≡ 0). */
182
+ h: number;
183
+ /** Saturation in [0, 1] (0 = grayscale, 1 = pure color). */
184
+ s: number;
185
+ /** Lightness in [0, 1] (0 = black, 0.5 = pure, 1 = white). */
186
+ l: number;
187
+ }
188
+ /**
189
+ * Convert RGB (0–255) to HSL.
190
+ *
191
+ * @example
192
+ * rgbToHsl({ r: 255, g: 0, b: 0 }) // → { h: 0, s: 1, l: 0.5 }
193
+ * rgbToHsl({ r: 0, g: 255, b: 0 }) // → { h: 120, s: 1, l: 0.5 }
194
+ *
195
+ * @since 1.3.5
196
+ */
197
+ declare const rgbToHsl: (rgb: RGB) => HSL;
198
+ /**
199
+ * Convert HSL to RGB (0–255). Hue wraps modulo 360.
200
+ *
201
+ * @example
202
+ * hslToRgb({ h: 0, s: 1, l: 0.5 }) // → { r: 255, g: 0, b: 0 }
203
+ * hslToRgb({ h: 240, s: 1, l: 0.5 }) // → { r: 0, g: 0, b: 255 }
204
+ *
205
+ * @since 1.3.5
206
+ */
207
+ declare const hslToRgb: (hsl: HSL) => RGB;
208
+ interface Oklab {
209
+ /** Perceptual lightness in [0, 1]. */
210
+ L: number;
211
+ /** Green↔Red axis, roughly [-0.4, 0.4]. */
212
+ a: number;
213
+ /** Blue↔Yellow axis, roughly [-0.4, 0.4]. */
214
+ b: number;
215
+ }
216
+ /**
217
+ * Convert RGB (0–255) to Oklab. Perceptually uniform — interpolating in
218
+ * this space produces smoother gradients than naive RGB.
219
+ *
220
+ * @since 1.3.5
221
+ */
222
+ declare const rgbToOklab: (rgb: RGB) => Oklab;
223
+ /**
224
+ * Convert Oklab back to RGB (0–255). Out-of-gamut values are clamped.
225
+ *
226
+ * @since 1.3.5
227
+ */
228
+ declare const oklabToRgb: (oklab: Oklab) => RGB;
229
+ /** Color space to interpolate in. Default `'rgb'` for backward compatibility. */
230
+ type ColorSpace = 'rgb' | 'hsl' | 'oklab';
231
+ /**
232
+ * Linearly interpolate between two RGB colors. `t` is clamped to [0, 1].
233
+ *
234
+ * **v1.3.5+**: Accepts an optional 4th argument `space` to control which
235
+ * color space the interpolation happens in. `'oklab'` produces the
236
+ * smoothest, most perceptually uniform gradients but is ~3× slower than
237
+ * naive RGB. `'hsl'` is useful for hue rotation.
238
+ *
239
+ * @param a - Start color (RGB, 0–255).
240
+ * @param b - End color (RGB, 0–255).
241
+ * @param t - Mixing factor in [0, 1] (clamped).
242
+ * @param space - Interpolation space. Default `'rgb'` (backward-compat).
243
+ *
244
+ * @example
245
+ * ```ts
246
+ * lerpColor(red, blue, 0.5); // naive RGB midpoint
247
+ * lerpColor(red, blue, 0.5, 'oklab'); // perceptual midpoint
248
+ * lerpColor(red, blue, 0.5, 'hsl'); // through purple via hue
249
+ * ```
250
+ */
251
+ declare const lerpColor: (a: RGB, b: RGB, t: number, space?: ColorSpace) => RGB;
252
+ /**
253
+ * Semantic alias for `lerpColor`. Reads more naturally for the "blend
254
+ * two colors" use case, especially with a named color space.
255
+ *
256
+ * @example
257
+ * ```ts
258
+ * mixColors('#ff0000', '#0000ff', 0.5, 'oklab');
259
+ * // Accepts hex strings OR RGB objects
260
+ * ```
261
+ *
262
+ * @since 1.3.5
263
+ */
264
+ declare const mixColors: (a: RGB | string, b: RGB | string, t: number, space?: ColorSpace) => RGB;
265
+ /**
266
+ * Quantize a color to N levels per channel. Useful for palette
267
+ * reduction, posterization effects, or matching a constrained color
268
+ * palette (e.g., 16-color terminals).
269
+ *
270
+ * Mathematically: maps each channel to the nearest of `levels` evenly
271
+ * spaced values in [0, 255]. With `levels=2` you get pure on/off per
272
+ * channel (8 colors total). With `levels=4` you get a 64-color palette.
273
+ *
274
+ * @param color - Input RGB (0–255).
275
+ * @param levels - Number of discrete levels per channel (≥2, default `4`).
276
+ *
277
+ * @example
278
+ * ```ts
279
+ * quantizeColor({ r: 100, g: 150, b: 200 }, 4);
280
+ * // → snaps each channel to nearest of [0, 85, 170, 255]
281
+ * ```
282
+ *
283
+ * @since 1.3.5
284
+ */
285
+ declare const quantizeColor: (color: RGB, levels?: number) => RGB;
147
286
  /**
148
287
  * Multi-stop gradient interpolation. Given a list of color stops and t in [0, 1],
149
288
  * returns the interpolated RGB. Equivalent to a CSS `linear-gradient` sampler.
@@ -157,7 +296,7 @@ declare const lerpColor: (a: RGB, b: RGB, t: number) => RGB;
157
296
  * gradientColor([red, blue], -1) → red (t clamped to 0)
158
297
  * gradientColor([red, blue], 99) → blue (t clamped to 1)
159
298
  */
160
- declare const gradientColor: (colors: RGB[], t: number) => RGB;
299
+ declare const gradientColor: (colors: RGB[], t: number, space?: ColorSpace) => RGB;
161
300
  declare const rgbTo256: (r: number, g: number, b: number) => number;
162
301
  declare const stripAnsi$2: (str: string) => string;
163
302
  /**
@@ -365,17 +504,23 @@ declare const padBoth: (str: string, width: number, ch?: string) => string;
365
504
  * @param start - Start hex color (e.g. `'#ff0000'`).
366
505
  * @param end - End hex color (e.g. `'#0000ff'`).
367
506
  * @param count - Number of stops (>= 2; clamped if smaller).
507
+ * @param space - **v1.3.5+** Color space for interpolation:
508
+ * `'rgb'` (default, fast), `'hsl'` (hue rotation),
509
+ * or `'oklab'` (perceptually uniform).
368
510
  * @returns Array of hex strings, including both endpoints.
369
511
  *
370
512
  * @example
371
513
  * ```ts
372
514
  * import { gradientStops } from 'ansimax';
373
515
  *
516
+ * // Naive RGB (default)
374
517
  * const stops = gradientStops('#ff0000', '#0000ff', 5);
375
- * // → ['#ff0000', '#bf003f', '#7f007f', '#3f00bf', '#0000ff']
518
+ *
519
+ * // Perceptually uniform (smoother visual transition)
520
+ * const smooth = gradientStops('#ff0000', '#0000ff', 5, 'oklab');
376
521
  * ```
377
522
  */
378
- declare const gradientStops: (start: string, end: string, count: number) => string[];
523
+ declare const gradientStops: (start: string, end: string, count: number, space?: ColorSpace) => string[];
379
524
  /**
380
525
  * Escape a string for safe use inside a regular expression literal.
381
526
  * Escapes all 12 regex meta-characters: `. * + ? ^ $ { } ( ) | [ ] \`.
@@ -1376,7 +1521,7 @@ declare const images: {
1376
1521
  createCanvas: (width: number, height: number, fillColor?: Pixel) => Canvas;
1377
1522
  colors: {
1378
1523
  hex: (h: unknown) => RGB | null;
1379
- lerp: (a: RGB, b: RGB, t: number) => RGB;
1524
+ lerp: (a: RGB, b: RGB, t: number, space?: ColorSpace) => RGB;
1380
1525
  blend: (fg: Pixel, bg: RGB) => RGB;
1381
1526
  };
1382
1527
  clearAnsiCache: () => void;
@@ -2706,6 +2851,41 @@ declare const json: {
2706
2851
  pretty: (value: unknown, opts?: PrettyOptions) => string;
2707
2852
  };
2708
2853
 
2854
+ type EasingFunction = (t: number) => number;
2855
+ /**
2856
+ * Union of all built-in easing names in the comprehensive Robert Penner
2857
+ * library. Provides autocompletion and prevents typos when looking up
2858
+ * functions in `easings`.
2859
+ *
2860
+ * **Note**: This is the v1.3.5 extended library. The original `EasingName`
2861
+ * from the gradient module is a smaller union (5 values) and is
2862
+ * preserved for backward compatibility.
2863
+ *
2864
+ * @since 1.3.5
2865
+ */
2866
+ type EasingLibraryName = 'linear' | 'easeInQuad' | 'easeOutQuad' | 'easeInOutQuad' | 'easeInCubic' | 'easeOutCubic' | 'easeInOutCubic' | 'easeInQuart' | 'easeOutQuart' | 'easeInOutQuart' | 'easeInQuint' | 'easeOutQuint' | 'easeInOutQuint' | 'easeInSine' | 'easeOutSine' | 'easeInOutSine' | 'easeInExpo' | 'easeOutExpo' | 'easeInOutExpo' | 'easeInCirc' | 'easeOutCirc' | 'easeInOutCirc' | 'easeInBack' | 'easeOutBack' | 'easeInOutBack' | 'easeInElastic' | 'easeOutElastic' | 'easeInOutElastic' | 'easeInBounce' | 'easeOutBounce' | 'easeInOutBounce';
2867
+ /**
2868
+ * A library of named easing functions. Each maps `t ∈ [0, 1]` to an
2869
+ * eased value, typically also in `[0, 1]` (back/elastic briefly
2870
+ * overshoot by design).
2871
+ *
2872
+ * Typed as `Record<EasingLibraryName, EasingFunction>` so all 31 keys are
2873
+ * known to TypeScript — autocompletion + no `possibly undefined` errors
2874
+ * when accessing standard names.
2875
+ *
2876
+ * @since 1.3.5
2877
+ */
2878
+ declare const easings: Record<EasingLibraryName, EasingFunction>;
2879
+ /**
2880
+ * Resolve an easing reference to a function. Accepts a function (returned
2881
+ * as-is), a named string in `easings` (typed `EasingName` for autocomplete,
2882
+ * but any string is allowed at runtime with linear fallback), or
2883
+ * `undefined`/invalid input (returns `linear`).
2884
+ *
2885
+ * @since 1.3.5
2886
+ */
2887
+ declare const resolveEasingByName: (e: EasingLibraryName | string | EasingFunction | undefined | null) => EasingFunction;
2888
+
2709
2889
  declare const ansimax: {
2710
2890
  color: {
2711
2891
  black: ColorFn;
@@ -2866,7 +3046,7 @@ declare const ansimax: {
2866
3046
  createCanvas: (width: number, height: number, fillColor?: Pixel) => Canvas;
2867
3047
  colors: {
2868
3048
  hex: (h: unknown) => RGB | null;
2869
- lerp: (a: RGB, b: RGB, t: number) => RGB;
3049
+ lerp: (a: RGB, b: RGB, t: number, space?: ColorSpace) => RGB;
2870
3050
  blend: (fg: Pixel, bg: RGB) => RGB;
2871
3051
  };
2872
3052
  clearAnsiCache: () => void;
@@ -2874,4 +3054,4 @@ declare const ansimax: {
2874
3054
  configure: (opts?: AnsimaxConfig, meta?: ConfigureOptions) => void;
2875
3055
  };
2876
3056
 
2877
- export { ASCII_RAMPS, type Alignment, type AnimateGradientController, type AnimateGradientOptions, type AnimationHooks, type AnimationSpeed, type AnsiCode, type AnsimaxConfig, type AsciiRamp, BEL, BG, type BadgeOptions, type BallOptions, type BannerOptions, type BoxOptions, type BoxStyle, type BreatheOptions, DEFAULTS as CONFIG_DEFAULTS, CSI, type Canvas, type CanvasRenderOptions, type CenterOptions, type ColorChain, type ColorFn, type ColorLevel, type ColorMode, type ColorSupport, type ColumnsOptions, type ConfigChangeListener, type ConfigKey, type ConfigKeyListener, type ConfigureOptions, type CountdownOptions, type CustomOptions, DEFAULT_TERM_COLS, DEFAULT_TERM_ROWS, type DebounceOptions, type DiffType, type Dimensions, type DividerOptions, type DotsOptions, ESC, type EasingFn, type EasingName, type EraseMode, FG, FRAME_MS, type FadeOptions, type FigletFont, type FigletOptions, type FontMap, type FontName, type FrameCallback, type FrameHandle, type FrameOptions, type FromImageOptions, type GlitchOptions, type Glyph, type GradientOptions, type GradientRectOptions, type GridOptions, type HsplitOptions, type PrettyOptions as JsonPrettyOptions, type LineDiff, type LiveController, type LiveOptions, type LoadingBarOptions, type LogoOptions, MENU_CANCELLED, type MemoizeOptions, type MenuInput, type MenuOptions, type MenuOutput, type MenuResult, type MultiLoader, type MultiLoaderItem, OSC, type OnResizeOptions, type OutputBuffer, type ParallelOptions, type ParallelStep, type Pixel, type PixelGrid, type PlayController, type PlayOptions, type PresetName, type ProgressAnimateOptions, type ProgressBarOptions, type ProgressOptions, type PulseOptions, type RGB, type RGBA, type RegisterFontOptions, type RenderOptions$1 as RenderOptions, type ResizeListener, type ReusableGradient, type RevealOptions, SPINNERS, SPRITES, ST, STYLE, type SectionOptions, type SleepOptions, type SlideOptions, type SpinOptions, type SpinnerType, type StatusOptions, type StatusType, type StopFn, type StreamOptions, type TableBorderStyle, type TableOptions, type Task, type TaskResult, type TasksOptions, type Theme, type BannerOpts as ThemeBannerOpts, type ThemeChangeListener, type ThemeInstance, type ThemeStyleName, type TimelineEvent, type TimelineOptions, type TreeData, type TreeDimensions, type TreeNode, type RenderOptions as TreeRenderOptions, type TreeStyle, type TypeDeleteOptions, type TypewriterOptions, type VsplitOptions, type WalkVisitor, type WaveOptions, type WriteAsyncOptions, animate, animateGradient, ascii, bell, bg256, bgRgb, box, canAnimate, cancelTerminalFrame, center$1 as center, center as centerBlock, chain, charWidth, clamp, clearAnsiCache, clearColorCache, clearLine, clearRenderCache, clearThemeColorCache, color, colorLevel, presets as colorPresets, components, compose, configure, countNodes, createCanvas, createGradient, createOutputBuffer, createTheme, cursor, debounce, ansimax as default, diffLines, escapeForRegex, escapeRegex, fg256, fgRgb, figletText, filterTree, findInTree, flipHorizontal, flipVertical, frame, frames, fromImage, getConfig, getConfigValue, getRenderCacheSize, getSpeedMultiplier, getTerminalHeight, getTerminalWidth, gradient, gradientColor, gradientRect, gradientStops, graphemes, grid, hasFont, hexToRgb, hideCursor, hsplit, hyperlink, images, isHexColor, isNoColor, json, pretty as jsonPretty, lerp, lerpColor, link, listFonts, listPresets, loader, mapTree, measureBlock, measureTree, memoize, nextTick, onConfigChange, onConfigKeyChange, onResize, once, padBoth, padEnd, padStart, panels, parseFiglet, pauseListeners, presetNames, presets, rainbow, registerFont, registerPreset, renderPixelArt, renderTree, renderTreeStream, repeatVisible, requestTerminalFrame, reset, resetColorSupportCache, resetConfig, resetCursorRefCount, resetFramesCursorCount, resetLoaderCursorCount, resetNoColor, resumeListeners, reverseGradient, rgbTo256, rgbToHex, rotate90, safeJson, screen, setConfigValue, setNoColor, setTitle, sgr, showCursor, sleep, sleepFrame, sliceAnsi, stripAnsi$2 as stripAnsi, stripAnsi$1 as stripAnsiCodes, stripAnsi as stripAnsiColors, subscribeConfig, supportsColor, supportsColorLevel, termSize, themes, throttle, tree, trees, truncateAnsi, visibleLen, vsplit, walkTree, withConfig, wordWrap, wrapAnsi, write, writeAsync, writeErr, writeln, writelnErr };
3057
+ export { ASCII_RAMPS, type Alignment, type AnimateGradientController, type AnimateGradientOptions, type AnimationHooks, type AnimationSpeed, type AnsiCode, type AnsimaxConfig, type AsciiRamp, BEL, BG, type BadgeOptions, type BallOptions, type BannerOptions, type BoxOptions, type BoxStyle, type BreatheOptions, DEFAULTS as CONFIG_DEFAULTS, CSI, type Canvas, type CanvasRenderOptions, type CenterOptions, type ColorChain, type ColorFn, type ColorLevel, type ColorMode, type ColorSpace, type ColorSupport, type ColumnsOptions, type ConfigChangeListener, type ConfigKey, type ConfigKeyListener, type ConfigureOptions, type CountdownOptions, type CustomOptions, DEFAULT_TERM_COLS, DEFAULT_TERM_ROWS, type DebounceOptions, type DiffType, type Dimensions, type DividerOptions, type DotsOptions, ESC, type EasingFn, type EasingFunction, type EasingLibraryName, type EasingName, type EraseMode, FG, FRAME_MS, type FadeOptions, type FigletFont, type FigletOptions, type FontMap, type FontName, type FrameCallback, type FrameHandle, type FrameOptions, type FromImageOptions, type GlitchOptions, type Glyph, type GradientOptions, type GradientRectOptions, type GridOptions, type HSL, type HsplitOptions, type PrettyOptions as JsonPrettyOptions, type LineDiff, type LiveController, type LiveOptions, type LoadingBarOptions, type LogoOptions, MENU_CANCELLED, type MemoizeOptions, type MenuInput, type MenuOptions, type MenuOutput, type MenuResult, type MultiLoader, type MultiLoaderItem, OSC, type Oklab, type OnResizeOptions, type OutputBuffer, type ParallelOptions, type ParallelStep, type Pixel, type PixelGrid, type PlayController, type PlayOptions, type PresetName, type ProgressAnimateOptions, type ProgressBarOptions, type ProgressOptions, type PulseOptions, type RGB, type RGBA, type RegisterFontOptions, type RenderOptions$1 as RenderOptions, type ResizeListener, type ReusableGradient, type RevealOptions, SPINNERS, SPRITES, ST, STYLE, type SectionOptions, type SleepOptions, type SlideOptions, type SpinOptions, type SpinnerType, type StatusOptions, type StatusType, type StopFn, type StreamOptions, type TableBorderStyle, type TableOptions, type Task, type TaskResult, type TasksOptions, type Theme, type BannerOpts as ThemeBannerOpts, type ThemeChangeListener, type ThemeInstance, type ThemeStyleName, type TimelineEvent, type TimelineOptions, type TreeData, type TreeDimensions, type TreeNode, type RenderOptions as TreeRenderOptions, type TreeStyle, type TypeDeleteOptions, type TypewriterOptions, type VsplitOptions, type WalkVisitor, type WaveOptions, type WriteAsyncOptions, animate, animateGradient, ascii, bell, bg256, bgRgb, box, canAnimate, cancelTerminalFrame, center$1 as center, center as centerBlock, chain, charWidth, clamp, clampByte, clearAnsiCache, clearColorCache, clearLine, clearRenderCache, clearThemeColorCache, color, colorLevel, presets as colorPresets, components, compose, configure, countNodes, createCanvas, createGradient, createOutputBuffer, createTheme, cursor, debounce, ansimax as default, diffLines, easings, escapeForRegex, escapeRegex, fg256, fgRgb, figletText, filterTree, findInTree, flipHorizontal, flipVertical, frame, frames, fromImage, getConfig, getConfigValue, getRenderCacheSize, getSpeedMultiplier, getTerminalHeight, getTerminalWidth, gradient, gradientColor, gradientRect, gradientStops, graphemes, grid, hasFont, hexToRgb, hideCursor, hslToRgb, hsplit, hyperlink, images, isFiniteNumber, isHexColor, isNoColor, json, pretty as jsonPretty, lerp, lerpColor, link, listFonts, listPresets, loader, mapTree, measureBlock, measureTree, memoize, mixColors, nextTick, oklabToRgb, onConfigChange, onConfigKeyChange, onResize, once, padBoth, padEnd, padStart, panels, parseFiglet, pauseListeners, presetNames, presets, quantizeColor, rainbow, registerFont, registerPreset, renderPixelArt, renderTree, renderTreeStream, repeatVisible, requestTerminalFrame, reset, resetColorSupportCache, resetConfig, resetCursorRefCount, resetFramesCursorCount, resetLoaderCursorCount, resetNoColor, resolveEasingByName, resumeListeners, reverseGradient, rgbTo256, rgbToHex, rgbToHsl, rgbToOklab, rotate90, safeInt, safeJson, screen, setConfigValue, setNoColor, setTitle, sgr, showCursor, sleep, sleepFrame, sliceAnsi, stripAnsi$2 as stripAnsi, stripAnsi$1 as stripAnsiCodes, stripAnsi as stripAnsiColors, subscribeConfig, supportsColor, supportsColorLevel, termSize, themes, throttle, tree, trees, truncateAnsi, visibleLen, vsplit, walkTree, withConfig, wordWrap, wrapAnsi, write, writeAsync, writeErr, writeln, writelnErr };