# Color conversion from terminal color to Colors.jl color

**URL:** <https://discourse.julialang.org/t/color-conversion-from-terminal-color-to-colors-jl-color/111889>\
**Category:** New to Julia\
**Tags:** colors\
**Created:** [March 20, 2024, 4:24pm UTC](https://discourse.julialang.org/t/color-conversion-from-terminal-color-to-colors-jl-color/111889 "2024-03-20T16:24:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julia2001](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@Julia2001](https://discourse.julialang.org/u/Julia2001)\
**Post date:** [March 20, 2024, 4:24pm UTC](https://discourse.julialang.org/t/color-conversion-from-terminal-color-to-colors-jl-color/111889/1 "2024-03-20T16:24:55Z")

</div>

Hi,  
how can I convert the “one integer” terminal color consumed by printstyled() to a Colors.jl color usable within Luxor e.g.?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [March 20, 2024, 8:30pm UTC](https://discourse.julialang.org/t/color-conversion-from-terminal-color-to-colors-jl-color/111889/2 "2024-03-20T20:30:13Z")

</div>

Something like this might get you started

```julia
using Luxor
using Colors

low_ansi_colors = Dict(
    0 => RGB(0, 0, 0),    
    1 => RGB(0.5, 0, 0), 
    2 => RGB(0, 0.5, 0), 
    3 => RGB(0.5, 0.5, 0), 
    4 => RGB(0, 0, 0.5), 
    5 => RGB(0.5, 0, 0.5),
    6 => RGB(0, 0.5, 0.5),
    7 => RGB(0.75, 0.75, 0.75),
    8 => RGB(0.5, 0.5, 0.5),
    9 => RGB(1, 0, 0), 
    10 => RGB(0, 1, 0), 
    11 => RGB(1, 1, 0),
    12 => RGB(0, 0, 1), 
    13 => RGB(1, 0, 1),
    14 => RGB(0, 1, 1),
    15 => RGB(1, 1, 1)
)

function ansi_to_colorant(ansi_code)
    if ansi_code < 16
        return low_ansi_colors[ansi_code]
    elseif ansi_code < 232
        r = (ansi_code - 16) ÷ 36
        g = ((ansi_code - 16) ÷ 6) % 6
        b = (ansi_code - 16) % 6
        return RGB(r / 5, g / 5, b / 5)
    else
        grey = (ansi_code - 232) / 23
        return RGB(grey, grey, grey)
    end
end

@drawsvg begin
    background("black")
    table = Table(16, 16, 30, 30)
    for (pos, n) in table
        sethue(ansi_to_colorant(n))
        box(table, n, :fill)
        sethue("white")
        text(string(n), pos, halign=:center)
    end
end

```

 ![p](https://global.discourse-cdn.com/julialang/original/3X/0/a/0ae00f4e3684725db1f5e6c92ad7f3cd96bb0df0.png)

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [March 21, 2024, 1:23am UTC](https://discourse.julialang.org/t/color-conversion-from-terminal-color-to-colors-jl-color/111889/3 "2024-03-21T01:23:38Z")

</div>

> [@cormullion](#):
>
> `low_ansi_colors`

How about making these two

1. low\_ansi\_colors
2. ansi\_to\_colorant

available inside the Luxor package by default?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [March 21, 2024, 8:38am UTC](https://discourse.julialang.org/t/color-conversion-from-terminal-color-to-colors-jl-color/111889/4 "2024-03-21T08:38:22Z")

</div>

If it has to go anywhere, it would probably be in Colors.jl. I see no use for it anywhere else.

When StyledStrings.jl is widely available, you might be able to access the information [here](https://github.com/JuliaLang/StyledStrings.jl/blob/10f683929fad5e3768177bf7a9457373223821ab/src/legacy.jl#L21).

---

<div class="post-metadata">

**Author:** ![Julia2001](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@Julia2001](https://discourse.julialang.org/u/Julia2001)\
**Post date:** [March 21, 2024, 7:47pm UTC](https://discourse.julialang.org/t/color-conversion-from-terminal-color-to-colors-jl-color/111889/5 "2024-03-21T19:47:41Z")

</div>

@cormullion Thanks again for your efforts!  
I totally agree with you that this doesn’t belong to any drawing library at all which is why I tried not to bother you but rather to address this question more in the direction of the Colors.jl people.  
Regarding the StyledStrings.jl about which I read in the Julia 1.11(?) announcement there obviously happened what I was afraid of which is that two competing color management infrastructures come to life that are not directly connected to each other.  
I still don’t know enough about Julia syntax but I believe that the “Colors” way and the “StyledStrings” way are quite different in terms of identifying colors. One uses numbers the other one symbols. I have no idea how these can be translated into each other efficiently.

Anyway the legacy\_color() function which is not exposed to the public as far as I understand would translate from 8 bit to 24 bit RGB. Would this be consumable by e.g. Luxor directly?

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [March 22, 2024, 12:43am UTC](https://discourse.julialang.org/t/color-conversion-from-terminal-color-to-colors-jl-color/111889/6 "2024-03-22T00:43:05Z")

</div>

> [@Julia2001](#):
>
> I still don’t know enough about Julia syntax but I believe that the “Colors” way and the “StyledStrings” way are quite different in terms of identifying colors. One uses numbers the other one symbols. I have no idea how these can be translated into each other efficiently.

This won’t be a problem, StyledStrings defines a [SimpleColor](https://julialang.github.io/StyledStrings.jl/dev/#StyledStrings.SimpleColor) which can be an ordinary RGB triplet. The glue code to get this working with Colors.jl should be relatively straightforward.
