culori icon indicating copy to clipboard operation
culori copied to clipboard

feat: implement basic cli

Open NickCrews opened this issue 5 months ago • 4 comments

I wanted to be able to convert between formats on the command line, eg pnpx culori convert '#12ff45' --to oklch

This implements this, along with a few other useful util commands. This is current usage text:

usage: ${binString} <command> [options]
Commands:
  convert <color> --to <format>     Convert color to a different format
  brighten <color>                  Adjust brightness of a color using CSS brightness() filter
    --amount <float>                An amount of 1 leaves the color unchanged.
    [--to <format>]                 Smaller values darken the color (with 0 being fully black), while larger values brighten it.
  blend <color1> <color2>           Blend two colors
    [--mode <mode='multiply'>]
    [--to <format>]
  info <color>                      Show info about color
  luminance <color>                 Show luminance based on WCAG
  contrast <color1> <color2>        Show contrast ratio based on WCAG
  help                              Show help
  version                           Show version

Supported formats: hex, rgb, hsl, lab, lch, oklch, oklab, etc.
Supported blend modes: multiply, screen, overlay, etc.

I'm not sure if this is quite the right semantics of when I log to console.error vs console.log.

This is decently well tested. This would need some documentation, but I wanted to start with this and get verification that this has some hope of getting merged before I put more effort in.

NickCrews avatar Sep 30 '25 21:09 NickCrews

Hi Nick, thanks for this! A CLI for color operations has been sitting on my Someday list, so I’m glad to see it here.

If that’s fine with you, I’d like to bikeshed the names of the commands and options a bit to see if we can find a good mapping to the programmatic API. I’ll get back with a proposal, and other (stylistic) notes I will add as code comments.

danburzo avatar Oct 03 '25 16:10 danburzo

Yes, please bikeshed those names, I wasn't totally happy with them either. Also curious to hear if you would want to pull in a CLI library to make the arg parsing better (my vanilla implementation is probably full of bugs)

NickCrews avatar Oct 03 '25 17:10 NickCrews

Also curious to hear if you would want to pull in a CLI library to make the arg parsing better (my vanilla implementation is probably full of bugs)

I was actually happily surprised by the no-dep approach! I wanted to suggest Node’s built-in utils.parseArgs() for even simpler code. The ability to use multiple occurrences of the same option might come in handy, e.g. to specify multiple output formats.

danburzo avatar Oct 03 '25 18:10 danburzo

EDIT: I'm sorry I was lazy here, I can answer that myself with a google. The answer is yes, those APIs are implemented in bun and deno.

I'm not actually that familiar with the node ecosystem, if we use that node API, does that make this incompatible with other runtimes eg bun, deno? I would not want to do that.

NickCrews avatar Oct 04 '25 04:10 NickCrews