Skip to content

To upsample, or not to upsample? #370

@pedrottimark

Description

@pedrottimark

Please read this as an observation from a thankful user, instead of a late request for chalk 3

It might be relevant input to your discussion about performance and simplification

Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator

While studying bgAnsi256 as a visual enhancement when available, I noticed that chalk also upsamples from lower-level color models to higher-level ANSI color formats

It looks like a side-effect of the implementation more than an intended feature, because:

  • less predictable result (see ansi below)
  • more complicated to test
  • obstacle to faster paths
  • more than necessary dependence on color-convert

green

Here is (new chalk.Instance({level)).green('…') with ^ as placeholder for escape:

level output
0
1 ^[32m…^[39m
2 ^[32m…^[39m
3 ^[32m…^[39m

This is predictable: it uses lowest-level format and downsamples when needed

ansi

Here is (new chalk.Instance({level)).ansi(32)('…') which is a synonym for green:

level output
0
1 ^[32m…^[39m
2 ^[38;5;34m…^[39m
3 ^[38;2;0;128;0m…^[39m

The visual result is less predictable because on macOS 10.12.6 Terminal:

  • level 1 output has color #17a41a from default theme
  • level 2 output has color #19ad1d

This is research, because I don’t need to use ansi nor bgAnsi

ansi256

Here is (new chalk.Instance({level)).bgAnsi256(194)('…')

level output
0
1 ^[107m…^[49m
2 ^[48;5;194m…^[49m
3 ^[48;2;204;255;204m…^[49m

Because the contrasting background colors that I intend to use both downsample to 107 I would use chalk.level >= 2 as condition for foreground color with or without background color

However, it would eliminate a test case if levels 2 and 3 have the same format

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions