@palett/fluo
Version: 
A color converter
121 lines (101 loc) • 3.77 kB
JavaScript
import { DyeFab, pushRgb, pushHex, pushHsl, pushInt } from '@palett/dye-factory';
import '@palett/presets';
import { stringValue } from '@spare/string-value';
import { parseNum, isNumeric as isNumeric$1 } from '@texting/charset-fullwidth';
import { isNumeric, parseNum as parseNum$1 } from '@texting/charset-halfwidth';
import { isLiteralAny, isLiteral } from '@typen/literal';
import { replenish } from '@vect/object-update';
import { mutazip, mutate } from '@vect/vector';
export { fluoVector } from '@palett/fluo-vector';
export { fluoByColumns, fluoByPoints, fluoByRows, fluoMatrix } from '@palett/fluo-matrix';
export { fluoEntries } from '@palett/fluo-entries';
/**
 * @typedef {string|number|number[]} chroma
 */
/**
 *
 * @this {DyeFab}
 * @param {string|any} text
 * @param {chroma} color
 * @returns {function(string):string}
 */
class Fluo {
  /**
   *
   * @this {DyeFab}
   * @param {function(chroma):string} encolor
   * @param {string|any} text
   * @param {chroma} color
   * @returns {function(string):string}
   */
  static render(encolor, text, color) {
    var _this$slice;
    const local = (this === null || this === void 0 ? void 0 : (_this$slice = this.slice) === null || _this$slice === void 0 ? void 0 : _this$slice.call(this)) ?? DyeFab.shallow();
    if (color) (encolor ?? local.encolor).call(local, color);
    return DyeFab.prototype.render.call(local, text);
  }
  static rgb(text, rgb) {
    return Fluo.render.call(this, pushRgb, text, rgb);
  }
  static hex(text, hex) {
    return Fluo.render.call(this, pushHex, text, hex);
  }
  static hsl(text, hsl) {
    return Fluo.render.call(this, pushHsl, text, hsl);
  }
  static int(text, int) {
    return Fluo.render.call(this, pushInt, text, int);
  }
}
function fluo(text, color) {
  var _this$slice2;
  const local = (this === null || this === void 0 ? void 0 : (_this$slice2 = this.slice) === null || _this$slice2 === void 0 ? void 0 : _this$slice2.call(this)) ?? DyeFab.shallow();
  if (color) (local.encolor ?? pushRgb).call(local, color);
  return DyeFab.prototype.render.call(local, text);
}
const isNumericAny = x => isNumeric$1(x) || isNumeric(x);
const NUM_BOUND_CONF_FULL = {
  filter: isNumericAny,
  mapper: parseNum
};
const STR_BOUND_CONF_FULL = {
  filter: isLiteralAny,
  mapper: stringValue
};
const NUM_BOUND_CONF_HALF = {
  filter: isNumeric,
  mapper: parseNum$1
};
const STR_BOUND_CONF_HALF = {
  filter: isLiteral,
  mapper: stringValue
};
class PresetCollection extends Array {
  constructor(presets) {
    super(presets.length);
    mutazip(this, presets, (receiver, preset) => Object.assign({}, preset));
  }
  static build(...presets) {
    return new PresetCollection(presets);
  }
  assignPresets(...presets) {
    // if (this.length < presets.length) {this.length = presets.length}
    return mutazip(this, presets, (conf, preset) => Object.assign(conf ?? {}, preset), presets.length);
  }
  replenishPresets(...presets) {
    // if (this.length < presets.length) {this.length = presets.length}
    return mutazip(this, presets, (conf, preset) => replenish(conf ?? {}, preset), presets.length);
  }
  assignEffect(...effects) {
    if (effects.length === 0) return this;
    return mutate(this, conf => (conf.effects = effects, conf));
  }
  setBound(full) {
    const boundConfigs = full ? [NUM_BOUND_CONF_FULL, STR_BOUND_CONF_FULL, STR_BOUND_CONF_FULL] : [NUM_BOUND_CONF_HALF, STR_BOUND_CONF_HALF, STR_BOUND_CONF_HALF];
    return mutazip(this, boundConfigs, (conf, boundConf) => Object.assign(conf, boundConf));
  }
} // if (presets.length === 0) presets = [NUMERIC_PRESET, LITERAL_PRESET]
const POINTWISE = 0;
const ROWWISE = 1;
const COLUMNWISE = 2;
export { COLUMNWISE, Fluo, POINTWISE, PresetCollection, ROWWISE, fluo };