Construction and Conversion

Available colorspaces

The colorspaces used by Colors are defined in ColorTypes. Briefly, the defined spaces are:

  • Red-Green-Blue spaces: RGB, BGR, XRGB, RGBX, RGB24, plus transparent versions ARGB, RGBA, ABGR, BGRA, and ARGB32.

  • HSV, HSL, HSI, plus all 6 transparent variants (AHSV, HSVA, AHSL, HSLA, AHSI, HSIA)

Example block outputExample block outputExample block output
  • XYZ, xyY, LMS and all 6 transparent variants

  • Lab, Luv, LCHab, LCHuv and all 8 transparent variants

Example block outputExample block outputExample block outputExample block output
  • DIN99, DIN99d, DIN99o and all 6 transparent variants

  • Storage formats YIQ, YCbCr and their transparent variants

Example block outputExample block output
  • Gray, Gray24, and the transparent variants AGray, GrayA, and AGray32.

Color Parsing

You can parse any CSS color specification with the exception of currentColor. You can construct colors from strings using the @colorant_str macro and the parse function.

julia> using Colors

julia> colorant"red" # named color

julia> parse(Colorant, "DeepSkyBlue") # color names are case-insensitive

julia> colorant"#FF0000" # 6-digit hex notation

julia> colorant"#f00" # 3-digit hex notation

julia> colorant"rgb(255,0,0)" # rgb() notation with integers in [0, 255]

julia> colorant"rgba(255,0,0,0.6)" # with alpha in [0, 1]

julia> colorant"rgba(100%,80%,0%,0.6)" # with "integer" percentages

julia> parse(ARGB, "rgba(255,0,0,0.6)") # you can specify the return type

julia> colorant"hsl(120, 100%, 25%)" # hsl() notation

julia> colorant"hsla(120, 100%, 25%, 0.6)" # hsla() notation

julia> colorant"transparent" # transparent "black"

All CSS/SVG named colors are supported, in addition to X11 named colors, when their definitions do not clash with SVG. You can find all names and their color swatches in Named Colors page.

When writing functions the colorant"red" version is preferred, because the slow step runs when the code is parsed (i.e., during compilation rather than run-time).

The element types of the return types depend on the colorspaces, i.e. the hsl() and hsla() notations return HSL/HSLA colors with Float32 elements, and other notations return RGB/RGBA colors with N0f8 elements. The result colors can be converted to RGB{N0f16} (for example) using:

julia> using FixedPointNumbers

julia> RGB{N0f16}(colorant"indianred")


julia> parse(RGB{N0f16}, "indianred")

You can also pass a color (Colorant value) to the second argument of the parse function, but in this case, parse does not apply any conversion on the color.

julia> parse(RGB{N0f8}, HSL{Float64}(120, 1, 0.5)) # use `convert` instead

You can convert colors to hexadecimal strings using the hex function. Note that the conversion result does not have the prefix "#".

julia> color = colorant"#C0FFEE"

julia> hex(color)

Color Conversions

Colors.jl allows you to convert from one colorspace to another using the convert function.

For example:

julia> convert(RGB, HSL(270, 0.5, 0.5)) # without the element type

julia> convert(RGB{N0f8}, HSL(270, 0.5, 0.5)) # with the element type

Depending on the source and destination colorspace, this may not be perfectly lossless.

parse(Colorant, desc)

Parse a color description.

This parses a subset of HTML/CSS color specifications. In particular, everything is supported but: currentColor.

It does support named colors (though it uses X11 named colors, which are slightly different than W3C named colors in some cases), rgb(), hsl(), #RGB, and #RRGGBB syntax.


  • Colorant: literal Colorant
  • desc: color name or description

A literal Colorant will parse according to the desc string (usually returning an RGB); any more specific choice will return a color of the specified type.


  • an RGB{N0f8} color, or

  • an HSL color if hsl(h, s, l) was used

  • an RGBA color if rgba(r, g, b, a) was used

  • an HSLA color if hsla(h, s, l, a) was used

  • an ARGB{N0f8} color if 0xAARRGGBB/0xARGB was used

  • a specific Colorant type as specified in the first argument

Note for X11 named colors

The X11 color names with spaces (e.g. "sea green") are not recommended because they are not allowed in the SVG/CSS.

Note for hex notations

You can parse not only the CSS-style hex notations #RRGGBB/#RGB, but also 0xRRGGBB/0xRGB.

You can also parse the 8-digit or 4-digit hex notation into an RGB color with alpha. However, the result depends on the prefix (i.e. # or 0x).

julia> parse(Colorant, "#FF8800AA") # transparent orange

julia> parse(Colorant, "0xFF8800AA") # opaque purple
hex(c::Colorant, style::Symbol)

Convert a color to a hexadecimal string, optionally specifying its style.


  • c: a target color.
  • style: a symbol to specify the hexadecimal notation. Spesifying the uppercase symbols means the return values are in uppercase. The following symbols are available:
    • :AUTO: notation automatically selected according to the type of c
    • :RRGGBB/:rrggbb: 6-digit opaque notation
    • :AARRGGBB/:aarrggbb: 8-digit notation with alpha at the head
    • :RRGGBBAA/:rrggbbaa: 8-digit notation with alpha at the tail
    • :RGB/:rgb/:ARGB/:argb/:RGBA/:rgba: 3-digit or 4-digit noatation
    • :S/:s: short notation if available


julia> hex(RGB(1,0.5,0))

julia> hex(ARGB(1,0.5,0,0.25))

julia> hex(HSV(30,1.0,1.0), :AARRGGBB)

julia> hex(ARGB(1,0.533,0,0.267), :rrggbbaa)

julia> hex(ARGB(1,0.533,0,0.267), :rgba)

julia> hex(ARGB(1,0.533,0,0.267), :S)
Colors v0.12

style requires at least Colors v0.12.


For backward compatibility, hex(c::ColorAlpha) currently returns an "AARRGGBB" style string. This is inconsistent with hex(c, :AUTO) returning an "RRGGBBAA" style string. The alpha position for ColorAlpha will soon be changed to the tail.


Returns a normalized (wrapped-around) hue angle, or a color with the normalized hue, in degrees, in [0, 360]. The normalization is essentially equivalent to mod(h, 360), but is faster at the expense of some accuracy.