Movatterモバイル変換


[0]ホーム

URL:


Version:1.4.0
Date:2019-08-02
Title:Simple Colour Manipulation
Author:Jon Clayden
Maintainer:Jon Clayden <code@clayden.org>
Description:Functions for easily manipulating colours, creating colour scales and calculating colour distances.
Suggests:testthat, covr, ggplot2
Enhances:colorspace
Encoding:UTF-8
License:BSD_3_clause + file LICENCE
URL:https://github.com/jonclayden/shades
BugReports:https://github.com/jonclayden/shades/issues
RoxygenNote:6.1.1
NeedsCompilation:no
Packaged:2019-08-02 14:53:11 UTC; jon
Repository:CRAN
Date/Publication:2019-08-02 16:00:02 UTC

Colour mixtures

Description

These functions allow colours to be mixed in any colour space, eitheradditively (like light) or subtractively (like paint). The infix form%.)% is an alternative foraddmix, and%_/% forsubmix, with the mixing amount being fixed to 1 in these cases.

Usage

addmix(base, mixer, amount = 1, space = NULL)submix(base, mixer, amount = 1, space = NULL)X %.)% YX %_/% Y

Arguments

base,X

A vector of base colours, or a palette function or scale.

mixer,Y

A vector of colours to mix in.

amount

The amount of each colour to mix in, relative to the amountof the base. This will be recycled to the length ofmixer.

space

A string giving the space in which to perform the mixing, orNULL. In the latter case, the space ofbase will be used.

Value

New colours of class"shade", or a new palette function.

Author(s)

Jon Clayden <code@clayden.org>

Examples

addmix(c("red","green","blue"), "red")submix(c("cyan","magenta","yellow"), "cyan")

Complementary colours

Description

This function returns the complement of its argument, the "opposite" coloursin the specified space.

Usage

complement(shades, space = NULL)

Arguments

shades

One or more colours, in any suitable form (seeshade), or a palette function or scale.

space

The space in which to take the complement.

Value

New colours of class"shade", or a new palette function.

Examples

complement("cyan")complement("cyan", space="HSV")complement("cyan", space="Lab")

Retrieve the coordinates of a colour vector

Description

This function retrieves the coordinates of a colour vector's elements,within whatever space it is currently defined.

Usage

coords(x, ...)

Arguments

x

An R object.

...

Additional arguments to methods.

Value

A matrix giving colour coordinates in the relevant space, one colourper row. Columns are typically named.

Author(s)

Jon Clayden <code@clayden.org>

Examples

coords("red")

Simulate colour appearance for dichromats

Description

This functions manipulates colours to simulate the effects of differentkinds of colour blindness, and specifically dichromacy, in which only two ofthe usual three types of photoreceptors are present. There are three types,corresponding to the loss of red, green or blue photoreceptors.

Usage

dichromat(shades, type = c("protanopic", "deuteranopic", "tritanopic",  "none"))

Arguments

shades

One or more colours, in any suitable form (seeshade).

type

The type of colour vision deficiency to simulate: protanopia(red blindness), deuteranopia (green blindness) or tritanopia (blueblindness). The latter is the rarest in the population."none" isalso available, as a pass-through option. Abbrevations, such as the firstletter, may be used, and multiple values are acceptable.

Value

New colours of class"shade" in LMS space, representingprojections of the original shades onto a submanifold appropriate to thetype of dichromacy being simulated.

Author(s)

Jon Clayden <code@clayden.org>

References

Brettel, H., Viénot, F. and Mollon, J.D. (1997). Computerized simulation ofcolor appearance for dichromats. Journal of the Optical Society of America A14(10):2647-2655.

Examples

dichromat(c("red", "green", "blue"))

Colour distance

Description

This function calculates a distance measure that aims to quantify theperceptual difference between a vector of colours and a reference colour.The measure in question is the CIE Delta E (2000), which is calculated basedon colour coordinates in Lab space.

Usage

distance(shades, reference)

Arguments

shades

One or more colours, in any suitable form (seeshade).

reference

A single reference colour.

Value

A numeric vector of distances.

Author(s)

Jon Clayden <code@clayden.org>

References

http://www.brucelindbloom.com/index.html?Eqn_DeltaE_CIE2000.html

Examples

distance(c("red","green","blue"), "red")

Colour gradients

Description

This function returns a set of colours interpolating between the specifiedkey colours, equally separated in the specified space.

Usage

gradient(shades, steps = NULL, space = NULL)

Arguments

shades

Two or more colours, in any suitable form (seeshade), or a named colour map such as"viridis".

steps

An integer giving the number of shades required in the palette.IfNULL, a function will instead be returned that takes thisargument.

space

The colour space to traverse. Defaults to the current space ofshades, or"Lab" for thematplotlib colour maps, or"sRGB" otherwise.

Details

The key colours may be specified explicitly, or else a built-in colour mapmay be used. The maps available are currently those developed for Python'smatplotlib 2.0, namely"magma","inferno","plasma" and"viridis", and certain ColorBrewer palettes,namely"Blues","Reds","YlOrRd" (yellow-orange-red)and"RdBu" (red-grey-blue, a balanced diverging scale).

Value

A character vector of class"shade" containing the gradientelements in the specified space, or a palette function.

Author(s)

Jon Clayden <code@clayden.org>

References

http://bids.github.io/colormap/ for thematplotlib colour maps;http://colorbrewer2.org for the ColorBrewer ones.

Examples

gradient(c("red","blue"), 5)gradient(c("red","blue"), 5, space="Lab")gradient("viridis", 5)

Query or change colour properties

Description

These functions obtain the value of a colour property, or modify it. Theywill convert between colour spaces as required, but the RGB representationwill be appropriately updated in the result.

Usage

saturation(shades, values = NULL)brightness(shades, values = NULL)lightness(shades, values = NULL)chroma(shades, values = NULL)hue(shades, values = NULL)opacity(shades, values = NULL)delta(...)scalefac(...)recycle(...)

Arguments

shades

One or more colours, in any suitable form (seeshade), or a palette function or scale.

values

New values for the property in question, withNA as apass-through value that will leave the property as-is. IfNULL, thecurrent value(s) will be returned. May also be a function computing newvalues from old ones, such asdelta, which adds its argument, orscalefac, which multiplies it.

...

Arguments to replacement functionsdelta,scalefacandrecycle, which will be concatenated.

Details

Brightness and lightness differ technically, in the sense that one isabsolute and the other is relative. Intuitively, a grey square on whitepaper is brighter under bright sunlight than in a dark room, but itslightness (relative to the white around it) is constant between conditions.In these functions, brightness is “value” in HSV space and is between 0and 1, while lightness is defined in Lab space and is between 0 and 100.Saturation and chroma are also related. Hue is defined in HSV space, withred at 0º (and 360º), which is generally the most familiar parameterisation.

Value

Current colour property values, or new colours of class"shade". Ifshades is a function, the result will be a newfunction that wraps the old one and modifies its return value accordingly.

Note

The colour property functions are vectorised over both of theirarguments, such that the dimensions of the result will bec(length(values),dim(shades)). However, therecycle functioncan be used to suppress the usual dimensional expansion, and insteadfollow R's standard recycling rule.

Author(s)

Jon Clayden <code@clayden.org>

Examples

saturation(c("papayawhip","lavenderblush","olivedrab"))saturation("papayawhip", 0.7)saturation("papayawhip", delta(0.2))saturation("papayawhip", scalefac(1.5))saturation(c("red","green"), c(0.4,0.6))saturation(c("red","green"), recycle(0.4,0.6))

The shade class

Description

Objects of class"shade" are simply standard R character vectorsrepresenting one or more 8-bit (s)RGB colours in CSS-like hex format, butwith extra attributes giving the current colour space and coordinates.

Usage

shade(x, ...)## S3 method for class 'shade'shade(x, ...)## S3 method for class 'color'shade(x, ...)## S3 method for class 'matrix'shade(x, space = "sRGB", alpha = NULL, ...)## S3 method for class 'character'shade(x, ...)## Default S3 method:shade(x, ...)## S3 method for class 'shade'print(x, ...)## S3 method for class 'shade'x[i]## S3 replacement method for class 'shade'x[i] <- value## S3 method for class 'shade'c(...)## S3 method for class 'shade'rep(x, ...)## S3 method for class 'shade'rev(x)## S3 method for class 'shade'x == y## S3 method for class 'shade'x != y## S3 method for class 'equal.shade'all(target, current, hexonly = FALSE, ...)

Arguments

x,y

R objects, or"shade" objects for methods.

...

Additional parameters to methods. Forc, any number ofcolours in any acceptable form.

space

For a matrix, the space in which coordinates are beingprovided.

alpha

For a matrix, an associated vector of opacity values between 0and 1, if required.

i

An index vector.

value

A vector of replacement colours.

target,current

Shade vectors to compare.

hexonly

IfTRUE, compare only on the basis of the hex strings.Otherwise test for equal coordinates.

Details

Comparison between"shade" objectsx andy is achievedby convertingy (the second argument) into the colour space ofx and then comparing coordinates, after any clipping.

Value

A character vector of class"shade", with additionalattributes as follows.

space

A string naming a color space.

coords

A matrix giving colour coordinates in the relevant space,one colour per row.

Note

When concatenating, shades that are all from the same space willremain in that space, but shades from different spaces will be warped to“XYZ” space.

Author(s)

Jon Clayden <code@clayden.org>

Examples

s <- shade(c("red", "green", "blue"))s[1]s[1] <- "pink"

Retrieve the space of a colour vector

Description

This function retrieves the colour space in which its argument is currentlydefined.

Usage

space(x, ...)

Arguments

x

An R object.

...

Additional arguments to methods.

Value

A string naming a colour space.

Author(s)

Jon Clayden <code@clayden.org>

Examples

space("red")

Simple colour swatches

Description

This function provides a simple visualisation of a colour series as a seriesof boxes against the specified background colour. If the input has more thanone dimension then the boxes will be arranged in a grid (flattening furtherdimensions after the second).

Usage

swatch(x, bg = "white", ...)

Arguments

x

One or more colours, in any suitable form (seeshade).

bg

A background colour.

...

Additional arguments (currently unused).

Author(s)

Jon Clayden <code@clayden.org>

Examples

swatch(c("red", "green", "blue"))

Shift colours between spaces

Description

This function shifts the current colour space of its arguments to thespecified space, returning a new object of class"shade".

Usage

warp(x, space)

Arguments

x

An R object which can be coerced to class"shade".

space

A string naming the new space.

Details

Valid names for spaces are currently those supported by theconvertColor function, namely “sRGB”, “Apple RGB”, “CIERGB”, “XYZ”, “Lab” and “Luv”; plus “RGB” (which is treated as analias for “sRGB”), “HSV”, “LCh” and “LMS”. Case is not significant.

Value

A new object of class"shade".

Note

LMS space, used for chromatic adaptation and simulating colourblindness, is not uniquely defined. Here we use the (linearised) Bradfordtransform, obtained by Lam (1985) and used widely in ICC colour profilesand elsewhere, to transform to and from CIE XYZ space.

R uses the D65 standard illuminant as the reference white for the “Lab”and “Luv” spaces.

Author(s)

Jon Clayden <code@clayden.org>

References

Lam, K.M. (1985). Metamerism and colour constancy. PhD thesis, University ofBradford.

See Also

convertColor

Examples

warp("red", "HSV")

[8]ページ先頭

©2009-2025 Movatter.jp