logo

How to Use RGB Color Codes in Julia 📂Julia

How to Use RGB Color Codes in Julia

Code

The package provided in Julia for dealing with colors is Colors.jl. By importing the visualization package Plots.jl, the features within Colors.jl can also be used. The color codes representing the RGB space include RGB, BGR, RGB24, RGBX, XRGB, which are subtypes of AbstractRGB. RGBA adds transparency to RGB.

julia> using Plots

julia> subtypes(AbstractRGB)
5-element Vector{Any}:
 BGR
 RGB
 RGB24
 RGBX
 XRGB

julia> subtypes(AbstractRGBA)
2-element Vector{Any}:
 BGRA
 RGBA

Strings

For the function plot(), entering a string like "rgb(255, 0, 0)" as a color specifying keyword allows the use of the color with RGB code (255, 0, 0). As seen below, the reason why entering a string also works is apparently because plot() parses it automatically. For named colors, they can be used as either strings or symbols like "red" or :red.

using Plots

r = "rgb(255, 0, 0)"          # 정수로 표현된 RGB 빨간색
g = "rgba(0, 255, 0, 0.2)"    # 투명도가 0.2인 RGB 초록색
p = "rgb(50%, 0%, 100%)"      # 퍼센테이지로 표현된 RGB 보라색

plot(
    plot(rand(15), lc = r),
    bar(rand(15), fc = g),
    scatter(rand(15), mc = p),
    layout = (3, 1)
)

Parsing

RGB color codes can be parsed like colorant"rgb(0, 0, 0)".

julia> r = colorant"rgb(255, 0, 0)"          # 정수로 표현된 RGB 빨간색
RGB{N0f8}(1.0,0.0,0.0)

julia> g = colorant"rgba(0, 255, 0, 0.2)"    # 투명도가 0.2인 초록색
RGBA{N0f8}(0.0,1.0,0.0,0.502)

julia> p = colorant"rgb(50%, 0%, 100%)"      # 퍼센테이지로 표현된 RGB 보라색
RGB{N0f8}(0.502,0.0,1.0)

julia> plot(
            plot(rand(15), lc = r),
            bar(rand(15), fc = g),
            scatter(rand(15), mc = p),
            layout = (3, 1))

It is also possible to parse using parse(RGB, "rgb(0, 255, 255)").

julia> parse(RGB, "rgb(0, 255, 255)")
RGB{N0f8}(0.0,1.0,1.0)

julia> parse(RGBA, "rgba(0, 255, 0, 0.5)")
RGBA{N0f8}(0.0,1.0,0.0,0.502)

Custom Definition

Colors can be defined directly using functions like RGB(), RGBA().

julia> RGB(1, 0, 0)
RGB{N0f8}(1.0,0.0,0.0)

julia> RGB(1.0, 0.0, 0.0)
RGB{Float64}(1.0,0.0,0.0)

julia> RGBA(1, 0, 0.5, 0.5)
RGBA{Float64}(1.0,0.0,0.5,0.5)

To get exactly the same type of color as parsed by colorant, input of numbers in N0f8 type is needed. To use this, FixedPointNumbers.jl is required. Or, directly defining it as 1.0N0f8 is also an option. Below is the code returning the same color as colorant"rgb(255, 0, 0)", which is red.

julia> using FixedPointNumbers

# RGB 빨간색 RGB
julia> colorant"rgb(255, 0, 0)"
RGB{N0f8}(1.0,0.0,0.0)

julia> RGB(1.0, 0.0, 0.0)
RGB{Float64}(1.0,0.0,0.0)

julia> RGB(1.0N0f8, 0N0f8, 0N0f8)
RGB{N0f8}(1.0,0.0,0.0)

julia> RGB(reinterpret(N0f8, UInt8(255)), reinterpret(N0f8, UInt8(0)), reinterpret(N0f8, UInt8(0)))
RGB{N0f8}(1.0,0.0,0.0)

Conversion from Other Color Spaces

The function convert() converts color codes from other color spaces to RGB code.

julia> using Colors

julia> convert(RGB{N0f8}, HSL(270, 0.5, 0.5))
ERROR: UndefVarError: `N0f8` not defined

julia> using FixedPointNumbers

julia> convert(RGB{N0f8}, HSL(270, 0.5, 0.5))
RGB{N0f8}(0.502,0.251,0.749)

Getting Color Names

Functions rgb_string() and rgba_string() return the RGB, RGBA codes of colors as strings, respectively.

julia> rgb_string(colorant"rgba(255, 0, 0, 0.5)")
"rgb(255, 0, 0)"

julia> rgba_string(colorant"rgba(255, 0, 0, 0.5)")
"rgba(255, 0, 0, 0.502)"

julia> rgb_string(colorant"red")
"rgb(255, 0, 0)"

julia> rgb_string(parse(RGB, :blue))
"rgb(0, 0, 255)"

julia> rgb_string(colorant"#00FF00")
"rgb(0, 255, 0)"

See Also

Environment

  • OS: Windows11
  • Version: Julia 1.9.4, Plots v1.39.0, Colors v0.12.10, FixedPointNumbers v0.8.4

See Also