Color functions.
This namespace contains color manipulation functions which can be divided into following groups:
Color can be represented by following types:
Vec4
- this is core type representing 3 color channels and alpha (RGBA). Values are double
type from [0-255]
range. color
, gray
creators returns Vec4
representation. To ensure Vec4
use [[to-color]] function.Vec3
- 3 color channels, assuming alpha
set to value of 255
.java.awt.Color
- Java AWT representation. Creators are awt-color
, awt-gray
. Use [[to-awt-color]] to convert to this representations.keyword
- one of the HTML CSS names (see html-colors-list
) or thi.ng.color.presets/colors namesInteger
- packed ARGB value. Example: 0xffaa01
.String
- CSS ("#ab1122") or 6 chars string containg hexadecimal representation ("ffaa01")seqable
- list, vector containing 2-4 elements. Conversion is done by applying content to color
function.nil
- returning nil
during color conversion.To create color from individual channel values use color
function. To create gray for given intensity call gray
.
By default color is treated as RGB
with values from ranges [0.0-255.0]
inclusive.
You can access individual channels by calling on of the following:
0
(black) to 255
(white)).hue
- to get hue value in degrees (range from 0 to 360). Hexagon projection.hue-polar
- to get hue from polar transformation.set-ch0
, set-ch1
, set-ch2
and set-alpha
return new color with respective channel set to new value.
To make color darker/brighter use darken
/ [[lighten]] functions. Operations are done in Lab
color space.
To change saturation call saturate
/ desaturate
. Operations are done in LCH
color space.
You can also rely on VectorProto
from fastmath
library and treat colors as vectors.
Color can be converted from RGB to other color space (and back). List of color spaces are listed under colorspaces-list
variable. There are two types of conversions:
to-XXX
and from-XXX
where XXX
is color space name. Every color space has it's own value range for each channel. (comp from-XXX to-XXX)
acts almost as identity.to-XXX*
and from-XXX*
where XXX
is color space name. to-XXX*
returns values normalized to [0-255]
range. from-XXX*
expects also channel values in range [0-255]
.NOTE: there is no information which color space is used. It's just a matter of your code interpretation.
Color space conversion functions are collected in two maps colorspaces
for raw and colorspaces*
for normalized functions. Keys are color space names as keyword
and values are vectors with to-
fn as first and from-
fn as second element.
You can blend two colors (or individual channels) using one of the methods from blends-list
. All functions are collected in blends
map with names as names and blending function as value.
Palette is just sequence of colors.
There are plenty of them predefined or can be generated:
colourlovers-palettes
contains 500 top palettes in vector from colourlovers website.palette-presets
contains 256 Brewer, categorical, veridis, tableau, microsoft palettes as map. See palette-presets-list
for names.:monochromatic
, :triad
, :tetrad
with complementary color for given hue and configuration. See also Paletton website for details.Gradient is continuous functions which accepts value from [0-1]
range and returns color. Call gradient
, gradient-easing
or iq-gradient
to create one.
Predefined gradients are collected in gradient-presets
map. You can find them cubehelix
based and generated from Inigo Quilez settings.
To convert palette to gradient call gradient
function. You can set interpolation method and colorspace.
To convert gradient to palette call sample
function from fastmath library.
Call resample
to resample palette to other number of colors. Internally input palette is converted to gradient and sampled back.
To make gradient from two colors you can use also gradient-easing
where you interpolate between to colors using one of the easings functions from fastmath
.
Linear gradient between colors is defined as lerp
function.
Several functions to calculate distance between colors (euclidean
, delta-xxx
etc.).
Thi.ng RGBA
type implements ColorProto
. To convert any color to RGBA
use to-thing-rgba
.
Color functions. This namespace contains color manipulation functions which can be divided into following groups: * Representation * Channel manipulations * Conversions * Blending * Palettes / gradients * Distances ## Representation Color can be represented by following types: * fastmath `Vec4` - this is core type representing 3 color channels and alpha (RGBA). Values are `double` type from `[0-255]` range. [[color]], [[gray]] creators returns `Vec4` representation. To ensure `Vec4` use [[to-color]] function. * fastmath `Vec3` - 3 color channels, assuming `alpha` set to value of `255`. * `java.awt.Color` - Java AWT representation. Creators are [[awt-color]], [[awt-gray]]. Use [[to-awt-color]] to convert to this representations. * `keyword` - one of the HTML CSS names (see [[html-colors-list]]) or thi.ng.color.presets/colors names * `Integer` - packed ARGB value. Example: `0xffaa01`. * `String` - CSS ("#ab1122") or 6 chars string containg hexadecimal representation ("ffaa01") * any `seqable` - list, vector containing 2-4 elements. Conversion is done by applying content to [[color]] function. * `nil` - returning `nil` during color conversion. To create color from individual channel values use [[color]] function. To create gray for given intensity call [[gray]]. By default color is treated as `RGB` with values from ranges `[0.0-255.0]` inclusive. ## Color/ channel manipulations You can access individual channels by calling on of the following: * [[red]] or [[ch0]] - to get first channel value. * [[green]] or [[ch1]] - to get second channel value. * [[blue]] or [[ch2]] - to get third channel value. * [[alpha]] - to get alpha value. * [[luma]] - to get luma or brightness (range from `0` (black) to `255` (white)). * [[hue]] - to get hue value in degrees (range from 0 to 360). Hexagon projection. * [[hue-polar]] - to get hue from polar transformation. [[set-ch0]], [[set-ch1]], [[set-ch2]] and [[set-alpha]] return new color with respective channel set to new value. To make color darker/brighter use [[darken]] / [[lighten]] functions. Operations are done in `Lab` color space. To change saturation call [[saturate]] / [[desaturate]]. Operations are done in `LCH` color space. You can also rely on `VectorProto` from `fastmath` library and treat colors as vectors. ## Conversions Color can be converted from RGB to other color space (and back). List of color spaces are listed under [[colorspaces-list]] variable. There are two types of conversions: * raw - with names `to-XXX` and `from-XXX` where `XXX` is color space name. Every color space has it's own value range for each channel. `(comp from-XXX to-XXX)` acts almost as identity. * normalized - with names `to-XXX*` and `from-XXX*` where `XXX` is color space name. `to-XXX*` returns values normalized to `[0-255]` range. `from-XXX*` expects also channel values in range `[0-255]`. NOTE: there is no information which color space is used. It's just a matter of your code interpretation. Color space conversion functions are collected in two maps [[colorspaces]] for raw and [[colorspaces*]] for normalized functions. Keys are color space names as `keyword` and values are vectors with `to-` fn as first and `from-` fn as second element. ## Blending You can blend two colors (or individual channels) using one of the methods from [[blends-list]]. All functions are collected in [[blends]] map with names as names and blending function as value. ## Palettes / gradients ### Palette Palette is just sequence of colors. There are plenty of them predefined or can be generated: * [[colourlovers-palettes]] contains 500 top palettes in vector from [colourlovers](http://www.colourlovers.com/) website. * [[palette-presets]] contains 256 Brewer, categorical, veridis, tableau, microsoft palettes as map. See [[palette-presets-list]] for names. * [[paletton-palette]] function to generate palette of type: `:monochromatic`, `:triad`, `:tetrad` with complementary color for given hue and configuration. See also [Paletton](http://paletton.com) website for details. ### Gradient Gradient is continuous functions which accepts value from `[0-1]` range and returns color. Call [[gradient]], [[gradient-easing]] or [[iq-gradient]] to create one. Predefined gradients are collected in [[gradient-presets]] map. You can find them `cubehelix` based and generated from [Inigo Quilez](http://iquilezles.org/www/articles/palettes/palettes.htm) settings. ### Conversions To convert palette to gradient call [[gradient]] function. You can set interpolation method and colorspace. To convert gradient to palette call `sample` function from fastmath library. Call [[resample]] to resample palette to other number of colors. Internally input palette is converted to gradient and sampled back. To make gradient from two colors you can use also [[gradient-easing]] where you interpolate between to colors using one of the easings functions from `fastmath`. Linear gradient between colors is defined as [[lerp]] function. ## Distances Several functions to calculate distance between colors (`euclidean`, `delta-xxx` etc.). ## Thi.ng interoperability Thi.ng `RGBA` type implements [[ColorProto]]. To convert any color to `RGBA` use [[to-thing-rgba]].
Some blend functions can be parametrized with threshold
value. Default 0.5
.
Some blend functions can be parametrized with `threshold` value. Default `0.5`.
(average xs)
(average colorspace xs)
Average colors in given colorspace
(default: :RGB
)
Average colors in given `colorspace` (default: `:RGB`)
(blend-and a b)
Bitwise and of channel values.
Bitwise and of channel values.
(blend-colors f c1 c2)
(blend-colors f c1 c2 alpha?)
Blend colors with blending function. Set alpha?
if you want to blend alpha channel (default: false
).
Blend colors with blending function. Set `alpha?` if you want to blend alpha channel (default: `false`).
(blend-darkthreshold a b)
(blend-darkthreshold a b thr)
Dark thresholded (with *blend-threshold*
) channel values.
Dark thresholded (with `*blend-threshold*`) channel values.
(blend-difference a b)
Difference channel values.
Difference channel values.
(blend-exclusion a b)
Exclusion of channel values.
Exclusion of channel values.
(blend-hardlight a b)
Hardlight channel values.
Hardlight channel values.
(blend-hardmix a b)
Hard mix channel values.
Hard mix channel values.
(blend-lighten a b)
Lighten channel values.
Lighten channel values.
(blend-lightthreshold a b)
(blend-lightthreshold a b thr)
Light thresholded (with *blend-threshold*
) channel values.
Light thresholded (with `*blend-threshold*`) channel values.
(blend-linearburn a b)
Linear burn channel values.
Linear burn channel values.
(blend-linearlight a b)
Linear light channel values.
Linear light channel values.
(blend-madd a b)
Modulus add channel values.
Modulus add channel values.
(blend-mburn a b)
Modulus burn channel values.
Modulus burn channel values.
(blend-mdivide a b)
Modulus divide channel values.
Modulus divide channel values.
(blend-mdodge a b)
Modulus dodge channel values.
Modulus dodge channel values.
(blend-mhardlight a b)
Modulus hardlight channel values.
Modulus hardlight channel values.
(blend-mlinearburn a b)
Modulus linear burn channel values.
Modulus linear burn channel values.
(blend-mlinearlight a b)
Modulus linear light channel values.
Modulus linear light channel values.
(blend-moverlay a b)
Modulus overlay channel values.
Modulus overlay channel values.
(blend-mpegtoplight a b)
Modulus pegtop light channel values.
Modulus pegtop light channel values.
(blend-mpinlight a b)
Modulus pinlight channel values.
Modulus pinlight channel values.
(blend-msoftlight a b)
Modulus softlight channel values.
Modulus softlight channel values.
(blend-msubtract a b)
Modulus subtract channel values.
Modulus subtract channel values.
(blend-multiply a b)
Multiply channel values.
Multiply channel values.
(blend-mvividlight a b)
Modulus vividlight channel values.
Modulus vividlight channel values.
(blend-none a b)
Return first value only. Do nothing.
Return first value only. Do nothing.
(blend-opacity a b)
(blend-opacity a b thr)
Opacity (with *blend-threshold*
)
Opacity (with `*blend-threshold*`)
(blend-or a b)
Bitwise or of channel values.
Bitwise or of channel values.
(blend-overlay a b)
Overlay channel values.
Overlay channel values.
(blend-pegtoplight a b)
Pegtop light channel values.
Pegtop light channel values.
(blend-pinlight a b)
Pinlight of channel values.
Pinlight of channel values.
(blend-softlight a b)
Softlight channel values.
Softlight channel values.
(blend-subtract a b)
Subtract channel values.
Subtract channel values.
(blend-values f a b)
Blend two values a
and b
from range [0,255]
using blending function f
.
Result is from range [0,255]
.
Blend two values `a` and `b` from range `[0,255]` using blending function `f`. Result is from range `[0,255]`.
(blend-vividlight a b)
Vividlight channel values.
Vividlight channel values.
(blend-xor a b)
Bitwise xor of channel values.
Bitwise xor of channel values.
Map of all blending functions. * key - name as keyword * value - function See [[blends-list]] for names.
Make color brighter. See darken
.
Make color brighter. See [[darken]].
(change-lab-luma amt col)
Change luma for givent color by given amount. Works in LAB color space.
Luma value can't exceed [0-100]
range.
Change luma for givent color by given amount. Works in LAB color space. Luma value can't exceed `[0-100]` range.
(change-saturation amt col)
Change color saturation in LCH color space.
Saturation value can't exceed [0-134]
range.
Change color saturation in LCH color space. Saturation value can't exceed `[0-134]` range.
(clamp c)
Clamp all color channels to [0-255]
range.
Clamp all color channels to `[0-255]` range.
(clamp255 a)
Constrain value a
to 0-255 double.
Use to ensure that value is in RGB range.
Accepts and returns double
.
Constrain value `a` to 0-255 double. Use to ensure that value is in RGB range. Accepts and returns `double`. See also [[lclamp255]], [[clamp]] and [[lclamp]].
(color c)
(color c a)
(color r g b)
(color r g b a)
Create Vec4 object as color representation.
Arity:
Vec4
from any color. Same as [[to-color]]Create Vec4 object as color representation. Arity: * 1 - convert to `Vec4` from any color. Same as [[to-color]] * 2 - sets color alpha * 3 - sets r,g,b with alpha 255 * 4 - sets r,g,b and alpha See also [[awt-color]], [[awt-gray]].
(color-converter cs)
(color-converter cs ch-scale)
(color-converter cs ch1-scale ch2-scale ch3-scale)
(color-converter cs ch1-scale ch2-scale ch3-scale ch4-scale)
Create fn which converts provided color from cs
color space using ch-scale
as maximum value. (to simulate Processing colorMode
fn).
Arity:
Create fn which converts provided color from `cs` color space using `ch-scale` as maximum value. (to simulate Processing `colorMode` fn). Arity: * 1 - returns from-XXX* function * 2 - sets the same maximum value for each channel * 3 - sets individual maximum value without alpha, which is set to 0-255 range * 4 - all channels have it's own indivudual maximum value.
Basic color operations
Basic color operations
(to-color c)
Convert any color representation to Vec4
vector.
Convert any color representation to `Vec4` vector.
(green c)
Returns green (second channel) value. See also [[ch1]].
Returns green (second channel) value. See also [[ch1]].
(ch2 c)
Returns third channel value. See also [[blue]]
Returns third channel value. See also [[blue]]
(to-awt-color c)
Convert any color representation to java.awt.Color
.
Convert any color representation to `java.awt.Color`.
(ch0 c)
Returns first channel value. See also [[red]]
Returns first channel value. See also [[red]]
(red c)
Returns red (first channel) value. See also [[ch0]].
Returns red (first channel) value. See also [[ch0]].
(blue c)
Returns blue (third channel) value. See also [[ch2]].
Returns blue (third channel) value. See also [[ch2]].
(ch1 c)
Returns second channel value. See also [[green]]
Returns second channel value. See also [[green]]
(luma c)
Returns luma
Returns luma
(alpha c)
Returns alpha value.
Returns alpha value.
Map of all color spaces functions
Map of all color spaces functions * key - name as keyword * value - vector with functions containing to-XXX and from-XXX converters.
Map of all normalized color spaces functions
Map of all normalized color spaces functions * key - name as keyword * value - vector with functions containing to-XXX* and from-XXX* converters.
List of all color space names.
List of all color space names.
Vector 500 best palettes taken from http://www.colourlovers.com/
Vector 500 best palettes taken from http://www.colourlovers.com/
(contrast-ratio c1 c2)
WCAG contrast ratio.
Based on YUV luma.
WCAG contrast ratio. Based on YUV luma.
Delta E CIE distance (euclidean in LAB colorspace.
Delta E CIE distance (euclidean in LAB colorspace.
(delta-e-cmc c1 c2)
(delta-e-cmc l c c1 c2)
Delta E CMC distance.
Parameters l
and c
defaults to 1.0. Other common settings is l=2.0
and c=1.0
.
Delta E CMC distance. Parameters `l` and `c` defaults to 1.0. Other common settings is `l=2.0` and `c=1.0`.
(format-hex c)
Convert color to hex string (css).
Convert color to hex string (css).
(from-Cubehelix* c)
Cubehelix -> RGB, normalized
Cubehelix -> RGB, normalized
HSB(V) -> RGB, normalized (see from-HSV
)
HSB(V) -> RGB, normalized (see [[from-HSV]])
HSB(V) -> RGB (see [[from-HSV-raw]])
HSB(V) -> RGB (see [[from-HSV-raw]])
(from-HunterLAB* c)
HunterLAB -> RGB, normalized
HunterLAB -> RGB, normalized
(from-linear v)
Gamma correction (gamma=1/2.4), lighten
Gamma correction (gamma=1/2.4), lighten
(gradient palette)
(gradient colorspace palette)
(gradient colorspace interpolator palette)
(gradient colorspace interpolator domain palette)
Create gradient function from palette (list of colors).
Grandient function accepts value from 0 to 1 and returns interpolated color.
Optionally interpolate in given colorspace
and 1d interpolator
as keyword or function.
To make irregular spacings between colors, provide own domain
.
Create gradient function from palette (list of colors). Grandient function accepts value from 0 to 1 and returns interpolated color. Optionally interpolate in given `colorspace` and 1d `interpolator` as keyword or function. To make irregular spacings between colors, provide own `domain`.
Cubehelix gradient generator.
Cubehelix gradient generator.
(gradient-easing col1 col2)
(gradient-easing cs col1 col2)
(gradient-easing cs easing col1 col2)
Create gradient between two colors.
Input colors should be in expected color space (default: RGB
).
You can use easing function to make interpolation non-linear (default: linear).
Create gradient between two colors. Input colors should be in expected color space (default: `RGB`). You can use easing function to make interpolation non-linear (default: linear).
Ready to use gradients containing Inigo Quilez and Cubehelix sets.
Map with name (keyword) as key and gradient function as value.
Ready to use gradients containing Inigo Quilez and Cubehelix sets. Map with name (keyword) as key and gradient function as value.
(gray v)
(gray v a)
Create grayscale color based on intensity v
. Optional parameter alpha a
.
See also color
Create grayscale color based on intensity `v`. Optional parameter alpha `a`. See also [[color]]
(hue c)
Hue value of color (any representation). Returns angle (0-360).
Uses hexagonal transformation. See also hue-polar
.
Hue value of color (any representation). Returns angle (0-360). Uses hexagonal transformation. See also [[hue-polar]].
(hue-polar c)
Hue value of color (any representation). Returns angle (0-360).
Uses polar transformation. See also hue
.
Hue value of color (any representation). Returns angle (0-360). Uses polar transformation. See also [[hue]].
(iq-gradient c1 c2)
(iq-gradient a b c d)
Create gradient generator function with given parametrization or two colors.
See http://iquilezles.org/www/articles/palettes/palettes.htm and https://github.com/thi-ng/color/blob/master/src/gradients.org#gradient-coefficient-calculation
Parameters should be Vec3
type.
Create gradient generator function with given parametrization or two colors. See http://iquilezles.org/www/articles/palettes/palettes.htm and https://github.com/thi-ng/color/blob/master/src/gradients.org#gradient-coefficient-calculation Parameters should be `Vec3` type.
(iq-random-gradient)
Create random iq gradient.
Create random iq gradient.
(lclamp c)
Clamp all color channels to [0-255]
range. Round if necessary.
Clamp all color channels to `[0-255]` range. Round if necessary.
(lerp c1 c2 t)
Lineary interpolate color between two values.
See also gradient
or fastmath
vector interpolations.
Lineary interpolate color between two values. See also [[gradient]] or `fastmath` vector interpolations.
(mix x1 x2)
(mix x1 x2 t)
(mix colorspace x1 x2 t)
Mix colors in given optional colorspace
(default: :RGB
) and optional ratio (default: 0.5).
Mix colors in given optional `colorspace` (default: `:RGB`) and optional ratio (default: 0.5).
(nearest-color xf c)
(nearest-color f xf c)
Find nearest color from a set. Input: distance function (default euclidean), list of target colors and source color.
Find nearest color from a set. Input: distance function (default euclidean), list of target colors and source color.
(noticable-different? c1 c2)
(noticable-different? s p c1 c2)
Returns noticable difference (true/false) between colors.
Defined in: https://research.tableau.com/sites/default/files/2014CIC_48_Stone_v3.pdf
Implementation from: https://github.com/connorgr/d3-jnd/blob/master/src/jnd.js
Returns noticable difference (true/false) between colors. Defined in: https://research.tableau.com/sites/default/files/2014CIC_48_Stone_v3.pdf Implementation from: https://github.com/connorgr/d3-jnd/blob/master/src/jnd.js
Create paletton palette.
Input:
:monochromatic
(one hue), :triad
(three hues), :tetrad
(four hues)paletton-rgb-to-hue
to get hue value).Configuration consist:
:preset
- one of paletton-presets-list
, default :full
.:compl
- generate complementary color?, default false
. Works with :monochromatic
and :triad
:angle
- hue angle for additional colors for :triad
and :tetrad
.:adj
- for :triad
only, generate adjacent (default true
) values or not.Create [paletton](http://paletton.com/) palette. Input: * type - one of: `:monochromatic` (one hue), `:triad` (three hues), `:tetrad` (four hues) * hue - paletton version of hue (use [[paletton-rgb-to-hue]] to get hue value). * configuration as a map Configuration consist: * `:preset` - one of [[paletton-presets-list]], default `:full`. * `:compl` - generate complementary color?, default `false`. Works with `:monochromatic` and `:triad` * `:angle` - hue angle for additional colors for `:triad` and `:tetrad`. * `:adj` - for `:triad` only, generate adjacent (default `true`) values or not.
(paletton-rgb-to-hue c)
(paletton-rgb-to-hue r g b)
Convert color to paletton HUE (which is different than hexagon or polar conversion).
Convert color to paletton HUE (which is different than hexagon or polar conversion).
(random-gradient)
Generate random gradient function.
Generate random gradient function.
(random-palette)
Generate random palette from all collections defined in clojure2d.color namespace.
Generate random palette from all collections defined in clojure2d.color namespace.
(reduce-colors xs number-of-colors)
(reduce-colors colorspace xs number-of-colors)
Reduce colors using x-means clustering in given colorspace
(default :RGB
).
Use for long sequences.
Reduce colors using x-means clustering in given `colorspace` (default `:RGB`). Use for long sequences.
(resample number-of-colors palette & gradient-params)
Resample palette.
Internally it's done by creating gradient and sampling back to colors. You can pass gradient
parameters like colorspace, interpolator name and domain.
Resample palette. Internally it's done by creating gradient and sampling back to colors. You can pass [[gradient]] parameters like colorspace, interpolator name and domain.
Scaling factor to convert color value from range [0-255]
to [0-1]
.
Scaling factor to convert color value from range `[0-255]` to `[0-1]`.
(set-alpha c a)
Set alpha channel and return new color
Set alpha channel and return new color
(set-awt-alpha c a)
Set alpha channel and return Color
representation.
Set alpha channel and return `Color` representation.
(set-ch0 c val)
Set alpha channel and return new color.
Set alpha channel and return new color.
(set-ch1 c val)
Set alpha channel and return new color.
Set alpha channel and return new color.
(set-ch2 c val)
Set alpha channel and return new color
Set alpha channel and return new color
(to-Cubehelix* c)
RGB -> Cubehelix, normalized
RGB -> Cubehelix, normalized
RGB -> HSB(V), normalized (see to-HSV
)
RGB -> HSB(V), normalized (see [[to-HSV]])
(to-HunterLAB* c)
RGB -> HunterLAB, normalized
RGB -> HunterLAB, normalized
(to-linear v)
Gamma correction (gamma=2.4), darken
Gamma correction (gamma=2.4), darken
(to-thing-rgba c)
Convert Clojure2d color to thi.ng RGBA.
Convert Clojure2d color to thi.ng RGBA.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close