Utilities
-
Angle wrapped to be between -180 and +180 degrees.
See moreDeclaration
Swift
public struct Angle
extension Angle: Encodable
extension Angle: ExpressibleByFloatLiteral
extension Angle: ExpressibleByIntegerLiteral
-
Undocumented
See moreDeclaration
Swift
public enum Anything : Encodable
extension Anything: ExpressibleByFloatLiteral
extension Anything: ExpressibleByIntegerLiteral
extension Anything: ExpressibleByStringLiteral
-
Data structure that stores color, optionally with opacity.
Color can be expressed in a variety of ways:
As a one of 100+ CSS named colors :
let darkRed = Color.darkRed
As red, green and blue pixel intensities with optional alpha channel for transparency:
let black = Color.RGB(255, 255, 255) let blackTransparent = Color.RGB(255, 255, 255, 0.2)
As hue, saturation and lightness values, also with optional transparency:
let pink = Color.HSL(300, 100, 50) let pinkTransparent = Color.HSL(300, 100, 50, 0.3)
As a hexadecimal integer literal in
0xRRGGBB
format:let black: Color = 0xffffff
Transparency is expressed as
alpha
channel between0
(transparent) and1
(opaque). Valid lightness and saturation values are percentages between0
and100
. Hue represents a positive angle smaller than360°
. There are no checks, clippings or exceptions that enforce these constraints.See also
Warning
CSS hexadecimal 3-digit
#RGB
and 6-digit+transparency#RRGGBBAA
formats are not supported. For example the following code doesn’t work as expected:let black: Color = 0xfff // Blue #000fff let blackTransparent: Color = 0xffffff88 // Yellow #ffff88
It’s not possible to distinguish between these situations in Swift because they are all of the same type - a hexadecimal 32-bit integer literal. Failures, tough, are usually very visible and produce a significantly different result. The only correctly working format for literals is
#RRGGBB
.Declaration
Swift
public enum Color : Encodable
extension Color: CustomPlaygroundDisplayConvertible
extension Color: ExpressibleByIntegerLiteral
-
Constant or variable coloring of a property.
See moreDeclaration
Swift
public enum Coloring
extension Coloring: Encodable
extension Coloring: ExpressibleByIntegerLiteral
extension Coloring: ExpressibleByArrayLiteral
-
Qualitative color scale or a sequence of colors.
Qualitative color sequences are appropriate for data without natural ordering, such as categories, colors, names, countries and so on. The built-in static color sequences are meant to be used mostly as a
Layout.colorWay
attribute.Declaration
Swift
public typealias ColorList = [Color]
-
Mapping from numerical values to colors.
Instead of constructing a custom color scale, in most situation it’s desirable to pick one from a pre-defined palette. There is a wide range of built-in scales that should suit nearly all applications. Key advantage of these scales is that they address the technicalities to avoid hidden biases that can distort how people naturally perceive the data.
Built-in Color Scales
Based on the type of data the built-in scales are suited to one can distinguish the following types:
- Sequential - Vast majority of continuous data without any special properties.
- Diverging - Continuous data with a natural mid-point or a critical value where roughly half of the data is smaller and the other half is larger.
- Cyclic - Periodic data that wrap back to the same value after reaching end-points. For example tide height plotted on the globe.
Good way to pick one of the scales is to use the
swatch()
method that displays a figure with the scales other. The following example displays all diverging scales:let diverging = ColorScale.Diverging.swatch() // Also: Sequential, Cyclic, Ocean, Brewer, Carto, Plotly try diverging.show()
Most popular color scales are exposed directly as static properties of
ColorScale
struct. The following code displays a swatch of the most frequent used scales:let frequent = ColorScale.swatch() try frequent.show()
Custom Color Scales
Color scale can be also created manually from a sequence of colors and optional normalizes scale values. In most situations the desirable behavior is a smooth, linear interpolation between in-between the sequence colors. However, it’s also possible to create discontinuous scales with sharp jumps at particular thresholds.
The following example creates a custom color scale by linearly interpolating colors. The minimum/maximum value of the data will be displayed in red/blue with the intermediate values interpolated through green. The min/max value can be changed by manually setting
zMin
/cMin
orzMax
/cMax
properties of theTrace
orMarker
objects. Location of the green mid-point can be altered byzMid
/cMid
property in a similar fashion.let rgb0: ColorScale = [.red, .green, .blue] let rgb1 = ColorScale(interpolate: [.red, .green, .blue])
The following example achieves the same result as above but builds the color scale fom a dictionary that maps the scale values to colors:
let rgb0: ColorScale = [0: .red, 0.5: .green, 1.0, .blue] let rgb1 = ColorScale(interpolate: [0: .red, 0.5: .green, 1.0, .blue])
The next code snippet creates a discontinuous black-white color scale with sharp jump below and above the mid-point:
let bw = ColorScale(interpolate: [(0: .white), (0.5: .white), (0.5, .black), (1.0, .black)])
See also
Color, ColorWayRemark
.reversed()
returns the color scale flipped, so the min color becomes the max and vice versa.Declaration
Swift
public struct ColorScale : Encodable
extension ColorScale: ExpressibleByArrayLiteral
extension ColorScale: ExpressibleByDictionaryLiteral
-
Encapsulation of either a constant value or a collection of changing data.
See moreDeclaration
Swift
public enum Data<T>
extension Data: ExpressibleByArrayLiteral
extension Data: Encodable where T: Encodable
extension Data: ExpressibleByFloatLiteral where T: ExpressibleByFloatLiteral
extension Data: ExpressibleByIntegerLiteral where T: ExpressibleByIntegerLiteral
extension Data: ExpressibleByUnicodeScalarLiteral where T: ExpressibleByUnicodeScalarLiteral
extension Data: ExpressibleByExtendedGraphemeClusterLiteral where T: ExpressibleByExtendedGraphemeClusterLiteral
extension Data: ExpressibleByStringLiteral where T: ExpressibleByStringLiteral
extension Data: ExpressibleByStringInterpolation where T: StringProtocol
-
A representation of an arbitrary JSON value.
This is a bit more useful than the naive
See more[String:Any]
type for JSON values, since it makes sure only valid JSON values are present.Declaration
Swift
public enum InfoArray : Encodable
extension InfoArray: CustomDebugStringConvertible
extension InfoArray: ExpressibleByNilLiteral
extension InfoArray: ExpressibleByBooleanLiteral
extension InfoArray: ExpressibleByIntegerLiteral
extension InfoArray: ExpressibleByFloatLiteral
extension InfoArray: ExpressibleByStringLiteral
extension InfoArray: ExpressibleByArrayLiteral
extension InfoArray: ExpressibleByDictionaryLiteral