sharkdp / pastel

A command-line tool to generate, analyze, convert and manipulate colors
Apache License 2.0
4.98k stars 97 forks source link

Is there a way for pastel to find the nearest 256-palette colour to a 24-bit one? #124

Closed eggbean closed 2 years ago

eggbean commented 4 years ago

For example, I would like to find the closest 256-palette colour to #dc143c.

I tried this, which worked:

$ PASTEL_COLOR_MODE=8bit
$ pastel color dc143c

And then used a colour picker on the output to get #FF307D.

Is there a better way than this?

sharkdp commented 4 years ago

You can use pastel format ansi-8bit or pastel format ansi-8bit-escapecode:

❯ pastel format ansi-8bit dc143c                
\x1b[38;5;197m

or

❯ CODE=$(pastel format ansi-8bit-escapecode dc143c)
❯ echo "${CODE}HELLO\x1b[0m"   
HELLO

You can also use pastel paint in combination with -m/--mode (or PASTEL_COLOR_MODE) to directly paint something in that color:

❯ pastel -m 8bit paint black --on dc143c "WARNING"

I don't think #FF307D is the right answer. If I follow your process, I get #cc004c (which I believe is more similar to #dc143c). Did you forget to export PASTEL_COLOR_MODE?

eggbean commented 4 years ago

Thanks. I am get #DC143C. translated to #FF307D, which is colour197 in the terminal 256-colour index on my system. I assumed only the first 16 ANSI colours change with terminal themes and the rest of the palette was always the same.

I used the method I posted above to find the closest 256 palette colours to some 24-bit colours:

original 24bit nearest 8bit terminal-256
#5B7CE5 #5F87D7 colour68
#4169E1 #005FFF colour27
#132C76 #000087 colour18
#FA8072 #FF8787 colour210
#69E141 #5FD700 colour76
#DC143C #FF005F colour197

Seems to work fine.

Does #CC004C come up for colour197 when you use a colour picker?

I used this script to get the terminal colours:

 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/gawin/bash-colors-256/master/colors)"
sharkdp commented 4 years ago

Does #CC004C come up for colour197 when you use a colour picker?

yes

I assumed only the first 16 ANSI colours change with terminal themes and the rest of the palette was always the same.

Looks like they can be modified too :facepalm:

sharkdp commented 2 years ago

I guess this can be closed?