paint-by-number

Color ASCII art with ease

Usage no npm install needed!

<script type="module">
  import paintByNumber from 'https://cdn.skypack.dev/paint-by-number';
</script>

README

Paint by Number

Color ASCII art with ease

Travis npm

Installation

npm install paint-by-number

Usage

Let's say you have a string like this:

Rainbow!

Now you create a template with a string of the same character length like this:

 0123456

Now you create a palette that maps ANSI colors to the numbers.

const palette = {
  0: 'red',
  1: 'yellow',
  2: 'green',
  3: 'cyan',
  4: 'blue',
  5: 'magenta',
  6: 'white'
}

Let's put it all together.

const paint = require('paint-by-number');

const input  = 'Rainbow!';
const template = ' 0123456';
const palette = {
  0: 'red',
  1: 'yellow',
  2: 'green',
  3: 'cyan',
  4: 'blue',
  5: 'magenta',
  6: 'white'
};

const output = paint(input, template, palette);
console.log(output);

API

paint(input, template, palette)

Colors the characters of a string (or an array of strings) based on a color map and palette. Returns the same string or array, but colored.

  • input (String or Array): string(s) to be painted.
  • template (String or Array): template to refer to when painting the input string. The structure of template should match that of input.
    • Every non-whitespace character in the color map is read and checked against the color palette. If a matching property is found in the color palette, the character in the input string is painted with the value of the property. Use whitespace to indicate that a character should not be colored, which means it will use the user's command line default.
  • palette (Object): Color palette to use when reading the template. The key is the character to use, and the value is the color to attach to that character. Any character can be a key, but a key must be a single character.

Characters are colored using chalk. Any function chalk has for coloring can be used as a palette color.

const palette = {
  0: 'red',
  1: 'bgRed'
}

It's also possible to override the default color by adding a one-space key to the palette.

const palette = {
  ' ': 'blue'
}

Local Development

git clone https://github.com/gakimball/paint-by-number
cd paint-by-number
npm install
npm test

License

MIT © Geoff Kimball