Colorspaces

Colorspaces

Available colorspaces

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

Converting colors

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

For example:

convert(RGB, HSL(270, 0.5, 0.5))

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

Color Parsing

julia> c = colorant"red"
RGB{N0f8}(1.0, 0.0, 0.0)

julia> parse(Colorant, "red")
RGB{N0f8}(1.0, 0.0, 0.0)

julia> parse(Colorant, HSL(1, 1, 1))
HSL{Float32}(1.0f0, 1.0f0, 1.0f0)

julia> colorant"#FF0000"
RGB{N0f8}(1.0, 0.0, 0.0)

julia> parse(Colorant, RGBA(1, 0.5, 1, 0.5))
RGBA{Float64}(1.0, 0.5, 1.0, 0.5)

You can parse any CSS color specification with the exception of currentColor.

All CSS/SVG named colors are supported, in addition to X11 named colors, when their definitions do not clash with SVG.

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).

@colorant_str(ex)

Parse a literal color name as a Colorant.

Base.hexFunction.
hex(c)

Print a color as a RGB hex triple, or a transparent paint as an ARGB hex quadruplet.

Base.parseFunction.
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.

Arguments

  • 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.

Returns

  • 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

  • a specific Colorant type as specified in the first argument

Color match for CIE Standard Observer

The colormatch() function returns an XYZ color corresponding to a wavelength specified in nanometers.

colormatch(wavelen::Real)

The CIE defines a standard observer, defining a typical frequency response curve for each of the three human eye cones.

For instance, conversion from optical wavelength to RGB can be achieved with:

RGB.(colormatch.(350:10:750))

wavelength to RGB

Colors.colormatchFunction.
colormatch(wavelength)
colormatch(matchingfunction, wavelength)

Evaluate the CIE standard observer color match function.

Arguments

  • matchingfunction (optional): a type used to specify the matching function. Choices include:

    • CIE1931_CMF (the default, the CIE 1931 2° matching function)
    • CIE1964_CMF (the CIE 1964 10° color matching function)
    • CIE1931J_CMF (Judd adjustment to CIE1931_CMF)
    • CIE1931JV_CMF (Judd-Vos adjustment to CIE1931_CMF)
  • wavelength: Wavelength of stimulus in nanometers.

Returns the XYZ value of perceived color.

Chromatic Adaptation (white balance)

The whitebalance() function converts a color according to a reference white point.

whitebalance{T <: Color}(c::T, src_white::Color, ref_white::Color)

Convert a color c viewed under conditions with a given source whitepoint src_whitepoint to appear the same under different conditions specified by a reference whitepoint ref_white.

Colors.whitebalanceFunction.
whitebalance(c, src_white, ref_white)

Whitebalance a color.

Input a source (adopted) and destination (reference) white. For example, if you want a photo taken under fluorescent lighting to appear correct in regular sunlight, you might do something like whitebalance(c, WP_F2, WP_D65).

Arguments

  • c: An observed color.
  • src_white: Adopted or source white corresponding to c
  • ref_white: Reference or destination white.

Returns a whitebalanced color.

Color Difference

The colordiff function gives an approximate value for the difference between two colors.

julia> colordiff(colorant"red", parse(Colorant, HSB(360, 0.75, 1)))
8.178248292426845

colordiff(a::Color, b::Color; metric::DifferenceMetric=DE_2000())

Evaluate the CIEDE2000 color difference formula by default. This gives an approximate measure of the perceptual difference between two colors to a typical viewer. A larger number is returned for increasingly distinguishable colors.

Options for DifferenceMetric are as follows:

OptionAction
DE_2000(kl::Float64, kc::Float64, kh::Float64)Specify the color difference using the recommended CIEDE2000 equation, with weighting parameters kl, kc, and kh as provided for in the recommendation.
DE_2000()- when not provided, these parameters default to 1.
DE_94(kl::Float64, kc::Float64, kh::Float64)Specify the color difference using the recommended CIEDE94 equation, with weighting parameters kl, kc, and kh as provided for in the recommendation.
DE_94()- hen not provided, these parameters default to 1.
DE_JPC79()Specify McDonald's "JP Coates Thread Company" color difference formula.
DE_CMC(kl::Float64, kc::Float64)Specify the color difference using the CMC equation, with weighting parameters kl and kc.
DE_CMC()- when not provided, these parameters default to 1.
DE_BFD(wp::XYZ, kl::Float64, kc::Float64)Specify the color difference using the BFD equation, with weighting parameters kl and kc. Additionally, a white point can be specified, because the BFD equation must convert between XYZ and LAB during the computation.
DE_BFD(kl::Float64, kc::Float64)
DE_BFD()- when not specified, the constants default to 1, and the white point defaults to CIED65.
DE_AB()Specify the original, Euclidean color difference equation.
DE_DIN99()Specify the Euclidean color difference equation applied in the DIN99 uniform colorspace.
DE_DIN99d()Specify the Euclidean color difference equation applied in the DIN99d uniform colorspace.
DE_DIN99o()Specify the Euclidean color difference equation applied in the DIN99o uniform colorspace.
Colors.colordiffFunction.
colordiff(a, b; metric::DifferenceMetric=DE_2000())

Compute an approximate measure of the perceptual difference between colors a and b. Optionally, a metric may be supplied, chosen among DE_2000 (the default), DE_94, DE_JPC79, DE_CMC, DE_BFD, DE_AB, DE_DIN99, DE_DIN99d, DE_DIN99o.

Simulation of color deficiency ("color blindness")

Three functions are provided that map colors to a reduced gamut to simulate different types of dichromacy, the loss of one of the three types of human photopigments.

Protanopia, deuteranopia, and tritanopia are the loss of long, middle, and short wavelength photopigment, respectively.

These functions take a color and return a new, altered color in the same colorspace.

protanopic(c::Color, p::Float64)
deuteranopic(c::Color, p::Float64)
tritanopic(c::Color, p::Float64)

Also provided are versions of these functions with an extra parameter p in [0, 1], giving the degree of photopigment loss, where 1.0 is a complete loss, and 0.0 is no loss at all.

Colors.protanopicFunction.
protanopic(c)
protanopic(c, p)

Convert a color to simulate protanopic color deficiency (lack of the long-wavelength photopigment). c is the input color; the optional argument p is the fraction of photopigment loss, in the range 0 (no loss) to 1 (complete loss).

Colors.deuteranopicFunction.
deuteranopic(c)
deuteranopic(c, p)

Convert a color to simulate deuteranopic color deficiency (lack of the middle-wavelength photopigment). See protanopic for detail about the arguments.

Colors.tritanopicFunction.
tritanopic(c)
tritanopic(c, p)

Convert a color to simulate tritanopic color deficiency (lack of the short-wavelength photopigment). See protanopic for detail about the arguments.