# Colorize hexadecimal colors

**URL:** <https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623>\
**Category:** Help\
**Created:** [March 10, 2021, 12:47pm UTC](https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623 "2021-03-10T12:47:47Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![alexherbo2](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/alexherbo2/32/1140_2.png) [@alexherbo2](https://discuss.kakoune.com/u/alexherbo2)\
**Post date:** [January 23, 2024, 7:10pm UTC](https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623/21 "2024-01-23T19:10:36Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![arrufat](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/arrufat/32/1157_2.png) [@arrufat](https://discuss.kakoune.com/u/arrufat)\
**Post date:** [January 23, 2024, 11:07pm UTC](https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623/22 "2024-01-23T23:07:28Z")

</div>

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

```auto
    execute-keys 'gtGbx'

```

instead of

```auto
    execute-keys '%'

```

---

<div class="post-metadata">

**Author:** ![alexherbo2](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/alexherbo2/32/1140_2.png) [@alexherbo2](https://discuss.kakoune.com/u/alexherbo2)\
**Post date:** [January 24, 2024, 12:07am UTC](https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623/23 "2024-01-24T00:07:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![arrufat](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/arrufat/32/1157_2.png) [@arrufat](https://discuss.kakoune.com/u/arrufat)\
**Post date:** [September 25, 2024, 1:23am UTC](https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623/24 "2024-09-25T01:23:20Z")

</div>

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

```kakoune
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.

---

<div class="post-metadata">

**Author:** ![arrufat](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/arrufat/32/1157_2.png) [@arrufat](https://discuss.kakoune.com/u/arrufat)\
**Post date:** [October 11, 2024, 1:25pm UTC](https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623/25 "2024-10-11T13:25:19Z")

</div>

This is an example where I found the problem

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

```

`#000` gets highlighted when I use:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![arrufat](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/arrufat/32/1157_2.png) [@arrufat](https://discuss.kakoune.com/u/arrufat)\
**Post date:** [April 6, 2025, 11:31pm UTC](https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623/26 "2025-04-06T23:31:04Z")

</div>

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.

[Previous page](https://discuss.kakoune.com/t/colorize-hexadecimal-colors/1623.md?page=1)
