Colorize hexadecimal colors

pastel can be used to get a readable text color but it would require shelling out, which I want to avoid for speed and simplicity.

pastel textcolor '#a5cdff' | pastel format hex
# => #000000

Thank you for explaining it, now it’s clear! One last question, any reason why you use:

    execute-keys 'gtGbx'

instead of

    execute-keys '%'

To only consider the visible part of the buffer. It can have an impact on big files and is not useful to highlight non visible parts.

1 Like

I am tweaking the highlighter and this is what I got:

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' # select the visible part of the buffer only.
        evaluate-commands -draft -verbatim try %{
            execute-keys 's(\B#|\b0[xX]|\brgb[a]?:)([0-9A-Fa-f]{6})([0-9A-Fa-f]{2})?)\b<ret>'
            evaluate-commands -itersel %{
                set-option -add window hex_color_code_ranges "%val{selection_desc}|,,rgb:%reg{2}+U"
            }
        }
        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}"
                set-option -add window hex_color_code_ranges "%val{selection_desc}|,,rgb:%reg{1}%reg{1}%reg{2}%reg{2}%reg{3}%reg{3}+U"
            }
        }
    }
}
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

The commented line works, but I’d like to highlight the #XXX codes with the double underline.
However, I can’t make it work, only with background highlighting.
The double underline works with the #XXXXXX format, though.

@alexherbo2 Can you see what I am doing wrong? Thanks in advance.

This is an example where I found the problem

#canvas {
    border: 1px solid #000;
}

#000 gets highlighted when I use:

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}

but not when I use:

set-option -add window hex_color_code_ranges "%val{selection_desc}|,,rgb:%reg{1}%reg{1}%reg{2}%reg{2}%reg{3}%reg{3}+U"

However, if I surround it with ' like '#000' then it works.

BTW, I figured out what the issue was: the css filetype has marked the hex colors with +a, that is final attributes, so we can’t change stuff like italics, bold, and underlines, but we can change the background.

1 Like