A collection of scientifically accurate color maps for use in data visualization.
This package provides a set of pre-defined color maps that can be used to visualize data. Each color map is designed to accurately represent the underlying data, taking into account human perception and color theory.
- 32 scientifically accurate color maps, each with its own unique characteristics and uses.
- Categorized palettes for easy selection and use.
- Support for discrete color sampling (10, 25, 50, and 100 samples).
- Easy to use and integrate into your Swift project.
- Colors are easily convertible to
CGColor
,NSColor
,UIColor
,CIColor
,SKColor
,simd_float3
To use the color maps in this package, simply import it into your Swift project and use the global instance of the desired color map. For example:
import ScientificColorMaps
let colorMap = ScientificColorMap.batlow
Each color is stored as platform agnostic ScientificColor
. Internally the color components are stored as red, green, blue and represented as Float. Each Float is in the range 0 to 1. The raw values can be obtained by accessing the instances' constants: red
, green
and blue
. ScientificColor is Sendable, Equatable and Hashable.
This type provides conversions to UIColor
, NSColor
, CIColor
, SKColor
, simd_float3
if the respective type is available. Either by a generic:
public func into<T>() -> T
Or direct conversions:
asTuple() -> (red: Float, green: Float, blue: Float)
asArray() -> [Float]
asCGColor() -> CGColor
asCIColor() -> CIColor
asNSColor() -> NSColor
asUIColor() -> UIColor
asSimd() -> simd_float3
Every ScientificColor comes with an index value, which is not included in hash or equation test.
It provides the index: Int
(0-255) of the related color into the respective palette. If the categorized palette is used, then categoryIndex: Int?
is not nil and gives the color's index of the category (0-99).
Every color palette offers 256 colors
ScientificColorMap.palettes() -> [ScientificColorMap]
All color palettes, which offer in addition 100 categories
ScientificColorMap.categorizedPalettes() -> [ScientificColorMap]
name
... Stringrgb_data
... array of 256[ScientificColor]
categorical
... array of 100[ScientificColor]
if defined, otherwise nildiscrete10() -> [ScientificColor]
... subset of 10 colorsdiscrete25() -> [ScientificColor]
... subset of 25 colorsdiscrete50() -> [ScientificColor)]
... subset of 50 colorsdiscrete100() -> [ScientificColor]
... subset of 100 colorsmapToColor(value: Float, minValue: Float = 0, maxValue: Float = 1) -> ScientificColor
Just run swift run CodeGenerator
in the package directory, which will (re-)create the color palette swift files in folder Sources/ScientificColorMaps/ColorMaps
The color deficiency simulation is based on: A Physiologically-based Model for Simulation of Color Vision Deficiency
The functionality is only available, if simd can be imported. The enum ColorDeficiency
will be provided,
which offers the different anomalies: protanomaly, deuteranomaly and tritanomaly with possibility to
adjust the severity from 0 - none to 1 - full.
The enum provides the function mapRGB(rgb: simd_float3) -> simd_float3
to map the actual rgb value to a simulated rgb value, which is from view under color deficiency.
To illustrate the inner contrast, there are diagrams generated using swift run GenerateDiagram
and stored in the folder Diagrams. The simulated delta for the colormap discrete50()
in the folder range
and first 10 of categorical
in the folder categorical
. The resulting images are squares with the colormaps` color along each axis and the color value represents the difference between the colors. The upper left triangle is the difference between the color along x- and y-axis. The two lower right triangles illustrates the contrast to white and black as background color. For the difference of the color is the WCAG-contrast calculated between the rgb-values.
API may change
This package is licensed under the MIT License. The color data has its own license. See in the respective file +LICENCE.pdf
The package WCAG-Colors and its package dependency comes with its own licenses. The package is only used to create the diagrams and not part of the library itself.
- This package was generated using data from Version 8.0.1 of the Scientific Colour Maps, available at https://www.fabiocrameri.ch/colourmaps/.