Colorspaces
Available colorspaces
The colorspaces used by Colors are defined in ColorTypes. Briefly, the defined spaces are:
Red-Green-Blue spaces:
RGB
,BGR
,RGB1
,RGB4
,RGB24
, plus transparent versionsARGB
,RGBA
,ABGR
,BGRA
, andARGB32
.HSV
,HSL
,HSI
, plus all 6 transparent variants (AHSV
,HSVA
,AHSL
,HSLA
,AHSI
,HSIA
)XYZ
,xyY
,LMS
and all 6 transparent variantsLab
,Luv
,LCHab
,LCHuv
and all 8 transparent variantsDIN99
,DIN99d
,DIN99o
and all 6 transparent variantsStorage formats
YIQ
,YCbCr
and their transparent variantsGray
,Gray24
, and the transparent variantsAGray
,GrayA
, andAGray32
.
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).
Colors.@colorant_str
— Macro.@colorant_str(ex)
Parse a literal color name as a Colorant.
Base.hex
— Function.hex(c)
Print a color as a RGB hex triple, or a transparent paint as an ARGB hex quadruplet.
Base.parse
— Function.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 Colorantdesc
: 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, oran
HSL
color ifhsl(h, s, l)
was usedan
RGBA
color ifrgba(r, g, b, a)
was usedan
HSLA
color ifhsla(h, s, l, a)
was useda 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))
Colors.colormatch
— Function.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 toCIE1931_CMF
)
CIE1931JV_CMF
(Judd-Vos adjustment toCIE1931_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.whitebalance
— Function.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 toc
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:
Option | Action |
---|---|
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.colordiff
— Function.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.protanopic
— Function.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.deuteranopic
— Function.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.tritanopic
— Function.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.