Colorize hexadecimal colors

I’d like to do something as

add-highlighter shared/colors dynregex '(#[0-9a-f]{6})' 1:white,\1

where \1 would be the matched hex color.

Is it just possible ? I cant find any plugins that try to colorize colors.

There is also pastel. Maybe you can do something with it for Kakoune.

I’ve used this hook:

hook global WinSetOption filetype=kak %{ hook global NormalIdle .* %{
    evaluate-commands -save-regs 'a' %{ try %{
        execute-keys -draft <a-i>w"ay
        evaluate-commands %sh{ (
            color="${kak_reg_a}"
            inverted_color=$(echo "${color}" | perl -pe 'tr/0123456789abcdefABCDEF/fedcba9876543210543210/')
            printf "%s\n" "evaluate-commands -client $kak_client %{ try %{
                               echo -markup %{{rgb:${inverted_color},rgb:${color}+b}   #${color}   }
                           }}" | kak -p $kak_session
        ) >/dev/null 2>&1 </dev/null & }
    }}
}}

It echoes the color in the echo area when hovering:

image

Not exaclty a highlighter, but maybe this will help making a highlighter out of it

1 Like

This is by far the less broken thing I found about colorizing hexadecimal :smiley:

ty !

It definitely solve partialy the problem. I’ll try to implement something

both seems unreliable for different reason but thanks a lot for bringing those to me.

I still can inspire myself on some point to make on that works better

In fact, palette seems pretty much what I dream for but it is somehow broken on tmux

Okay, all was my bad. I had to update my TERM value on .tmux.conf

you may also need to update terminfo to the one shipped with Kakoune in contrib directory

i have plugin for that, which is a bit more customizable, but requires compiling a binary. GitHub - SolitudeSF/colorcol: Kakoune plugin for color previews.

1 Like

I can’t findout what it is usefull for. What’s the plus with this terminfo ?

| ReedWade
March 11 |

  • | - |

I can’t findout what it is usefull for. What’s the plus with this terminfo ?

Kakoune displays colors incorrectly in tmux without this terminfo.

I played a bit with Kakoune to get this working and, while I am not 100% happy, I think it’s good enough.
Maybe someone else might want to improve it. I couldn’t manage to do it in a draft context, so all selections are messed, hence the mapping at the end:

define-command hex-hl-update %{ try %{
    execute-keys \%s(#|0(x|X)|rgb:)([0-9A-Fa-f]{6,7})<ret>
    add-highlighter -override window/hex-colors group
    evaluate-commands %sh{
        for color in $kak_reg_3
        do
            printf "add-highlighter -override window/hex-colors/ regex '(#|0(x|X)|rgb:)(%s)' 1:rgb:%s,rgb:%s\n" "$color" "$color" "$color"
        done
    }
}}
define-command hex-hl-remove %{ try %{
    remove-highlighter window/hex-colors
}}

map global user -docstring 'preview hex colors' h 'Z:hex-hl-update<ret>z'
map global user -docstring 'remove hex colors' <a-h> ':hex-hl-remove<ret>'

Edit: I forgot to add a preview of what this looks like

1 Like

I have this implementation:

declare-option -hidden range-specs hex_color_code_ranges

add-highlighter shared/hex_color_code ranges hex_color_code_ranges

define-command update_hex_color_code_ranges %{
  set-option window hex_color_code_ranges %val{timestamp}
  evaluate-commands -draft %{
    execute-keys 'gtGbx'
    evaluate-commands -draft -verbatim try %{
      execute-keys 's\B#([0-9A-Fa-f]{6})\b<ret>'
      evaluate-commands -itersel %{
        set-option -add window hex_color_code_ranges "%val{selection_desc}|default,rgb:%reg{1}"
      }
    }
    evaluate-commands -draft -verbatim try %{
      execute-keys 's\B#([0-9A-Fa-f]{3})\b<ret>'
      evaluate-commands -itersel %{
        set-option -add window hex_color_code_ranges "%val{selection_desc}|default,rgb:%reg{1}%reg{1}"
      }
    }
  }
}

hook -always global NormalIdle '' update_hex_color_code_ranges
hook -always global InsertIdle '' update_hex_color_code_ranges
hook -always global PromptIdle '' update_hex_color_code_ranges

Usage:

add-highlighter global/hex_color_code ref hex_color_code
2 Likes

Oh, that’s nice, I need to get more familiar with highlighters.

I don’t get why we need two almost identical evaluate commands

    evaluate-commands -draft -verbatim try %{
      execute-keys 's\B#([0-9A-Fa-f]{6})\b<ret>'
      evaluate-commands -itersel %{
        set-option -add window hex_color_code_ranges "%val{selection_desc}|default,rgb:%reg{1}"
      }
    }
    evaluate-commands -draft -verbatim try %{
      execute-keys 's\B#([0-9A-Fa-f]{3})\b<ret>'
      evaluate-commands -itersel %{
        set-option -add window hex_color_code_ranges "%val{selection_desc}|default,rgb:%reg{1}%reg{1}"
      }
    }

If I remove either of those, it still works. Would you mind explaining it?

#ff0000 and #f00 are equivalent. Red.

The first s selects a six-character color code, the second selects three-character.

…but, there is a bug in the code generated for the second. See my next reply for details.

It does work for me:

screen

Three-char code #00f should expand to #0000ff. It looks like it is expanding to #00ff00 from the screen.

But the code looks like it is trying to color it as #00f00f.

The s command should probably be execute-keys 's\B#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])\b<ret>'. But that’s as far as I get. =]

Anyway, I haven’t been able to figure the fix.

Yet.

Thanks for the code.

2 Likes

Oh good catch. Here’s the updated version:

declare-option -hidden range-specs hex_color_code_ranges

add-highlighter shared/hex_color_code ranges hex_color_code_ranges

define-command update_hex_color_code_ranges %{
  set-option window hex_color_code_ranges %val{timestamp}
  evaluate-commands -draft %{
    execute-keys 'gtGbx'
    evaluate-commands -draft -verbatim try %{
      execute-keys 's\B#([0-9A-Fa-f]{6})\b<ret>'
      evaluate-commands -itersel %{
        set-option -add window hex_color_code_ranges "%val{selection_desc}|default,rgb:%reg{1}"
      }
    }
    evaluate-commands -draft -verbatim try %{
      execute-keys 's\B#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])\b<ret>'
      evaluate-commands -itersel %{
        set-option -add window hex_color_code_ranges "%val{selection_desc}|default,rgb:%reg{1}%reg{1}%reg{2}%reg{2}%reg{3}%reg{3}"
      }
    }
  }
}

hook -always global NormalIdle '' update_hex_color_code_ranges
hook -always global InsertIdle '' update_hex_color_code_ranges
hook -always global PromptIdle '' update_hex_color_code_ranges

2 Likes

So close.

Probably never would have caught my bug. Ever.

Thanks, alexherbo2


I haven’t figured out the %val{selection_desc} yet.

As a feature request, how would I color just the #, so I don’t have to complicate the code to figure out how to make the color code readable on dark and light?

Just append <a-;>; here

execute-keys 's\B#([0-9A-Fa-f]{6})\b<ret><a-;>;'

and here

execute-keys 's\B#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])\b<ret><a-;>;'

1 Like