Wrappers
Classesβ
Colorβ
Creates a lazy chain wrapper over a single color token that has all the functions that take a ColorToken
as their first argument.
Exampleβ
import { Color } from 'huetiful-js'
let wrapper = new Color('pink');
console.log(wrapper.color2hex());
// #ffc0cb
Constructorsβ
new Color()β
new Color(
c
,options
?):Color
Parametersβ
β’ c: ColorToken
= 'cyan'
The color to bind.
β’ options?: ColorOptions
Optional overrides and properties for the bound color.
Returnsβ
Defined inβ
Methodsβ
achromatic()β
achromatic():
boolean
Returns true
if the bound color has hue or is grayscale elsColorspaces} [colorspace='lch'] The colorspace to use when checking if the color
is grayscale or not.
Returnsβ
boolean
Exampleβ
import { color } from "huetiful-js";
import { formatHex8, interpolate, samples } from "culori"
var test = c => color(c).isAchromatic()
test('pink')
// false
let sample = [
"#164100",
"#ffff00",
"#310000",
'pink'
];
console.log(sample.map(test));
// [false, false, false,false]
test('gray')
// true
// we create an interpolation using black and white
let f = interpolate(["black", "white"]);
//We then create 12 evenly spaced samples and pass them to f as the `t` param required by an interpolating function.
// Lastly we convert the color to hex for brevity for this example (otherwise color objects work fine too.)
let grays = samples(12).map((c) => formatHex8(f(c)));
console.log(grays.map(test));
//
[ false, true, true,
true, true, true,
true, true, true,
true, true, false
]
Defined inβ
alpha()β
alpha(
amount
?):ColorToken
Sets/Gets the opacity or alpha
channel of a color. If the value
parameter is omitted it gets the bound color's alpha
value.
Parametersβ
β’ amount?: string
| number
The value to apply to the opacity channel. The value is normalized to the range [0,1]
Returnsβ
Exampleβ
import { color } from 'huetiful-js';
// Getting the alpha
console.log(color('#a1bd2f0d').alpha())
// 0.050980392156862744
// Setting the alpha
let myColor = color('b2c3f1')alpha(0.5)
console.log(myColor)
// #b2c3f180
Defined inβ
complimentary()β
complimentary():
ColorToken
Returns the complementary hue of the bound color. The function returns 'gray'
when you pass in an achromatic color.
Returnsβ
Exampleβ
import { color } from "huetiful-js";
console.log(color("pink").getComplimentaryHue(true))
// { hue: 'blue-green', color: '#97dfd7ff' }
Defined inβ
contrast()β
contrast(
against
?):number
Gets the contrast value between the bound and comparison ( or against
) color.
Parametersβ
β’ against?: ColorToken
The color to use for comparison. The default is 'black'
.
Returnsβ
number
Exampleβ
import { color } from 'huetiful-js'
console.log(color('pink').contrast('yellow'))
// 1.4322318222624262
Defined inβ
deficiency()β
deficiency(
options
?):ColorToken
Simulates how a color may be perceived by people with color vision deficiency.
To avoid writing the long types, the expected parameters for the kind
of blindness are simply the colors that are hard to perceive for the type of color blindness:
- 'tritanopia' - An inability to distinguish the color 'blue'. The
kind
is'blue'
. - 'deuteranopia' - An inability to distinguish the color 'green'.. The
kind
is'green'
. - 'protanopia' - An inability to distinguish the color 'red'. The
kind
is'red'
.
Parametersβ
β’ options?: DeficiencyOptions
Returnsβ
Exampleβ
import { color } from 'huetiful-js'
// Here we are simulating color blindness of tritanomaly or we can't see 'blue'.
// We are passing in our color as an array of channel values in the mode "rgb". The severity is set to 0.1
let tritanomaly = color(['rgb', 230, 100, 50, 0.5]).colorDeficiency('blue', 0.1)
console.log(tritanomaly)
// #dd663680
// Here we are simulating color blindness of tritanomaly or we can't see 'red'. The severity is not explicitly set so it defaults to 1
let protanopia = color({ h: 20, w: 50, b: 30, mode: 'hwb' }).colorDeficiency('red')
console.log(protanopia)
// #9f9f9f
Defined inβ
earthtone()β
earthtone(
options
?):string
|number
|boolean
|object
|ColorTuple
|ColorToken
[] |Map
<string
|number
,ColorToken
> |Set
<ColorToken
>
Creates a color scale between an earth tone and any color token using spline interpolation.
Parametersβ
β’ options?: EarthtoneOptions
Returnsβ
string
| number
| boolean
| object
| ColorTuple
| ColorToken
[] | Map
<string
| number
, ColorToken
> | Set
<ColorToken
>
Exampleβ
import { color } from 'huetiful-js'
let base = 'purple'
console.log(color(base).earthtone({num:8}))
ColorArray {
colors: [
'#352a21', '#3e2825',
'#4c2624', '#5f2028',
'#741033', '#860040',
'#940049', '#99004b'
]
}
console.log(color(base).earthtone({ iterations:8 }).output())
// call output() to only get results array
// [
'#352a21', '#3e2825',
'#4c2624', '#5f2028',
'#741033', '#860040',
'#940049', '#99004b'
]
Defined inβ
family()β
family():
never
Gets the hue family which a color belongs to with the overtone included (if it has one.).
For example 'red'
or 'blue-green'
. If the color is achromatic it returns the string 'gray'
.
Returnsβ
never
Exampleβ
import { color } from 'huetiful-js'
console.log(color("#310000").family())
// 'red'
Defined inβ
hueshift()β
hueshift(
options
?):Collection
- Creates a palette of hue shifted colors from the passed in color.
Hue shifting means that:
- As a color becomes lighter, its hue shifts up (increases).
- As a color becomes darker its hue shifts down (decreases).
The minLightness
and maxLightness
values determine how dark or light our color will be at either extreme respectively.
The length of the resultant array is the number of samples (num
) multiplied by 2 plus the base color passed in or (num * 2) + 1
.
Parametersβ
β’ options?: HueshiftOptions
The optional overrides object to customize the HueShiftOptions
like easing function.
Returnsβ
Exampleβ
import { color } from "huetiful-js";
let hueShiftedPalette = color("#3e0000").hueShift({ iterations:1 });
console.log(hueShiftedPalette);
// [
'#ffffe1', '#ffdca5',
'#ca9a70', '#935c40',
'#5c2418', '#3e0000',
'#310000', '#34000f',
'#38001e', '#3b002c',
'#3b0c3a'
]
Defined inβ
lightness()β
lightness(
amount
?,darken
?):ColorToken
Darkens the bound color by reducing the lightness
channel by amount
of the channel. For example 0.3
means reduce the lightness by 0.3
of the channel's current value.
Parametersβ
β’ amount?: number
The amount to darken with. The value is expected to be in the range [0,1]
. Default is 0.1
.
β’ darken?: any
= undefined
Returnsβ
Exampleβ
import { color } from "huetiful-js";
console.log(color('blue').darken(0.3));
//#464646
Defined inβ
luminance()β
luminance(
amount
?):ColorToken
Gets the luminance of the passed in color token.
If the amount
parameter is not passed in else it will adjust the luminance by interpolating the color with black (to decrease luminance) or white (to increase the luminance) by the specified amount
.
Parametersβ
β’ amount?: number
The luminance
value to set on the bound color.
Returnsβ
Exampleβ
import { color } from 'huetiful-js'
let myColor = 'green';
console.log(color(myColor).luminance());
// 0.1543834296814607
console.log(color(myColor)._luminance);
// 0.1543834296814607
console.log(color(myColor).luminance(0.5));
Defined inβ
mc()β
mc(
modeChannel
,value
?):ColorToken
Sets the value of the specified channel on the passed in color.
If the amount
parameter is undefined
it gets the value of the specified channel.
*
Parametersβ
β’ modeChannel: string
The mode and channel to be retrieved. For example rgb.b
will return the value of the blue channel's value in the RGB color space of that color.
*
β’ value?: string
| number
The value to set on the queried channel. Also supports expressions as strings e.g "#fc23a1"
"*0.5"
*
- The supported symbols
*
+
-
/
Returnsβ
Exampleβ
import { color } from 'huetiful-js'
console.log(color('#a1bd2f').mc('rgb.g'))
// 0.7411764705882353
Defined inβ
output()β
output():
ColorToken
Returns the final value from the chain.
Returnsβ
Exampleβ
import { color } from 'huetiful-js'
let myOutput = color(['rgb',200,34,65]).output()
console.log(myOutput)
// ['rgb',200,34,65]
Defined inβ
overtone()β
overtone():
ColorFamily
Returns the name of the hue family which is biasing the passed in color.
- If an achromatic color is passed in it returns the string
'gray'
- If the color has no bias it returns
false
.
Returnsβ
Exampleβ
console.log(color("fefefe").overtone())
// 'gray'
console.log(color("cyan").overtone())
// 'green'
console.log(color("blue").overtone())
// false
Defined inβ
pair()β
pair(
options
?):string
|number
|boolean
|object
|ColorTuple
|ColorToken
[] |Map
<string
|number
,ColorToken
> |Set
<ColorToken
>
Creates a palette that consists of a baseColor
that is incremented by a hueStep
to get the final color to pair with.
The colors are then spline interpolated via white or black.
A negative hueStep
will pick a color that is hueStep
degrees behind the base color.
Parametersβ
β’ options?: PairedSchemeOptions
The optional overrides object to customize per channel options like interpolation methods and channel fixups.
Returnsβ
string
| number
| boolean
| object
| ColorTuple
| ColorToken
[] | Map
<string
| number
, ColorToken
> | Set
<ColorToken
>
Exampleβ
import { color } from 'huetiful-js'
console.log(color("green").pairedScheme({hueStep:6,samples:4,tone:'dark'}))
// [ '#008116ff', '#006945ff', '#184b4eff', '#007606ff' ]
Defined inβ
pastel()β
pastel():
ColorToken
Returns a randomized pastel variant of the bound color token. Preserves the bound ColorToken
type.
Returnsβ
Exampleβ
import { color } from 'huetiful-js';
console.log(color("green").pastel())
// #036103ff
Defined inβ
saturation()β
saturation(
amount
?):ColorToken
Sets/Gets the saturation value of the bound color.
Parametersβ
β’ amount?: number
The amount of saturation
to set on the bound color token. Also supports string expressions.
Returnsβ
Exampleβ
import { color } from 'huetiful-js'
let myColor = ['lch',60,13,0.5]
console.log(color(myColor).saturation())
// 13
console.log(color(myColor).saturation("*0.3"))
// ['lch',60,19.9,0.5]
Defined inβ
scheme()β
scheme(
options
?):Collection
Returns a randomised classic color scheme from the bound color.
Parametersβ
β’ options?: SchemeOptions
Returnsβ
Exampleβ
import { color } from 'huetiful-js'
console.log(color("#a1bd2f").scheme("triadic"))
// [ '#a1bd2fff', '#00caffff', '#ff78c9ff' ]
Defined inβ
temp()β
temp():
"warm"
|"cool"
Returns a rough estimation of a color's temperature as either 'cool'
or 'warm'
.
Returnsβ
"warm"
| "cool"
Exampleβ
import { color } from 'huetiful-js'
let sample = [
"#00ffdc",
"#00ff78",
"#00c000"
];
console.log(color(sample[2]).temp());
// 'warm'
Defined inβ
token()β
token(
options
?):ColorToken
Parses any recognizable color to the specified kind
of ColorToken
type.
The kind
option supports the following types as options:
-
'array'
- Parses the color token to an array of channel values with thecolorspace
as the first element if theomitMode
parameter is set tofalse
in theoptions
object. -
'number'
- Parses the color token to its numerical equivalent to a number between0
and16,777,215
.
The numberType
can be used to specify which type of number to return if the kind
option is set to 'number'
:
'hexadecimal'
'binary'
'octal'
'decimal'
exponential notation'hex'
- Parses the color token to its hexadecimal string equivalent.
If the color token has an explicit alpha
(specified by the alpha
key in color objects and as the fourth and last number in a color array) the string will be 8 characters long instead of 6.
'object'
- Parses the color token to a plain color object in themode
specified by thetargetMode
parameter in theoptions
object.
Parametersβ
β’ options?: TokenOptions
Returnsβ
Defined inβ
via()β
via(
origin
):ColorToken
Interpolates the bound color via the origin
with the point t
at 0.5
.
Parametersβ
β’ origin: ColorToken
The color to interpolate via. value is in the range [0,1] the easing and the interpolation methods /fixups.
Returnsβ
Defined inβ
ColorArray<C, Options>β
Creates a lazy chain wrapper over a collection of colors that has all the array methods (functions that take a collection of colors as their first argument).
The ColorArray
class is also exposed via a wrapper function load()
,
if you prefer not to explicitly instantiate a new ColorArray
.
Exampleβ
import { ColorArray } from 'huetiful-js';
let sample = ['blue', 'pink', 'yellow', 'green'];
let wrapper = new ColorArray(sample);
// We can even chain the methods and get the result by calling `.output()`
// [ 'blue', 'green', 'yellow', 'pink' ]
Type Parametersβ
β’ C extends Collection
β’ Options extends object
Constructorsβ
new ColorArray()β
new ColorArray<
C
,Options
>(colors
,implicitReturn
?):ColorArray
<C
,Options
>
Parametersβ
β’ colors: C
The collection of colors to bind.
β’ implicitReturn?: boolean
Returnsβ
ColorArray
<C
, Options
>
Defined inβ
Methodsβ
discover()β
discover(
options
?):Collection
|ColorArray
<C
,Options
>
Takes a collection of colors and finds the nearest matches using the differenceHyab()
color difference metric for a set of predefined palettes.
The function returns different values based on the kind
parameter passed in:
- An array of colors for the
kind
of scheme, if thekind
parameter is specified. - Else it returns an object of all the palette types as keys and their values as an array of colors.
If no colors are valid for the palette types it returns an empty array for the palette results. It does not work with achromatic colors thus they're excluded from the resulting collection.
Parametersβ
β’ options?: DiscoverOptions
Returnsβ
Collection
| ColorArray
<C
, Options
>
Exampleβ
import { load } from 'huetiful-js';
let sample = [
"#ffff00",
"#00ffdc",
"#00ff78",
"#00c000",
"#007e00",
"#164100",
"#720000",
"#600000",
"#4e0000",
"#3e0000",
"#310000",
]
console.log(load(sample).discover({kind:'tetradic'}).output())
// [ '#ffff00ff', '#00ffdcff', '#310000ff', '#720000ff' ]
Defined inβ
filterBy()β
filterBy(
options
?):Collection
|ColorArray
<C
,Options
>
Filters a collection of colors using the specified factor
as the criterion. The supported options are:
-
'contrast'
- Returns colors with the specified contrast range. The contrast is tested against a comparison color (the 'against' param) and the specified contrast ranges. -
'lightness'
- Returns colors in the specified lightness range. -
'chroma'
- Returns colors in the specifiedsaturation
orchroma
range. The range is internally normalized to the supported ranges by thecolorspace
in use if it is out of range. -
'distance'
- Returns colors with the specifieddistance
range. Thedistance
is tested against a comparison color (the 'against' param) and the specifieddistance
ranges. Uses thedifferenceHyab
metric for calculating the distances. -
luminance
- Returns colors in the specified luminance range. -
'hue'
- Returns colors in the specified hue ranges between 0 to 360.
For the chroma
and lightness
factors, the range is internally normalized to the supported ranges by the colorspace
in use if it is out of range.
This means a value in the range [0,1]
will return, for example if you pass startLightness
as 0.3
it means 0.3 (or 30%)
of the channel's supported range.
But if the value of either start or end is above 1 AND the colorspace
in use has an end range higher than 1 then the value is treated as is else the value is treated as if in the range [0,100]
and will return the normalized value.
Parametersβ
β’ options?: FilterByOptions
Returnsβ
Collection
| ColorArray
<C
, Options
>
Seeβ
https://culorijs.org/color-spaces/ For the expected ranges per colorspace.
Supports expression strings e.g '>=0.5'
. The supported symbols are == | === | != | !== | >= | <= | < | >
Exampleβ
import { filterBy } from 'huetiful-js';
let sample = [
'#00ffdc',
'#00ff78',
'#00c000',
'#007e00',
'#164100',
'#ffff00',
'#310000',
'#3e0000',
'#4e0000',
'#600000',
'#720000',
]
// Filtering colors by their relative contrast against 'green'.
// The collection will include colors with a relative contrast equal to 3 or greater.
console.log(load(sample).filterBy({start:'>=3', factor:'contrast',against:'green' }))
// [ '#00ffdc', '#00ff78', '#ffff00', '#310000', '#3e0000', '#4e0000' ]
Defined inβ
interpolator()β
interpolator(
options
?):Collection
|ColorArray
<C
,Options
>
Interpolates the passed in colors and returns a collection of colors from the interpolation.
- To create a color scale for cyclic values pass
true
to theclosed
parameter in theoptions
object. - If
num
is 1 then a single color is returned from the resulting interpolation with the internalt
value at0.5
else a collection of thenum
of color scales is returned. - If the collection of colors contains an achromatic color, the resulting samples may all be grayscale or pure black.
Parametersβ
β’ options?: InterpolatorOptions
Optional channel specific overrides.
Returnsβ
Collection
| ColorArray
<C
, Options
>
Exampleβ
import { load } from 'huetiful-js';
*
Defined inβ
nearest()β
nearest(
options
?):Collection
|ColorArray
<C
,Options
>
Returns the nearest color(s) in the bound collection against
the entire collection is returned with colors ordered in ascending order using the differenceHyab
metric.
Parametersβ
β’ options?: NearestOptions
Returnsβ
Collection
| ColorArray
<C
, Options
>
Exampleβ
import { load,colors } from 'huetiful-js';
let cols = colors('all', '500')
console.log(load(cols).nearest('blue', 3));
// [ '#a855f7', '#8b5cf6', '#d946ef' ]
Defined inβ
output()β
output():
Collection
Returns the result value from the chain.
Can be omitted from invocation when implicitReturn
is set to true.
Returnsβ
Defined inβ
sortBy()β
sortBy(
options
?):Collection
|ColorArray
<C
,Options
>
Sorts colors according to the specified factor
. The supported options are:
'contrast'
- Sorts colors according to their contrast value as defined by WCAG. The contrast is testedagainst
a comparison color which can be specified in theoptions
object.'lightness'
- Sorts colors according to their lightness.'chroma'
- Sorts colors according to the intensity of theirchroma
in thecolorspace
specified in theoptions
object.'distance'
- Sorts colors according to their distance. The distance is computed from theagainst
color token which is used for comparison for all the colors in thecollection
.luminance
- Sorts colors according to their relative brightness as defined by the WCAG3 definition.
The return type is determined by the type of collection
:
- Plain objects are returned as
Map
objects because they remember insertion order.Map
objects are returned as is. ArrayLike
objects are returned as plain arrays. Plain arrays are returned as is.
Parametersβ
β’ options?: SortByOptions
Returnsβ
Collection
| ColorArray
<C
, Options
>
Exampleβ
import { sortBy } from 'huetiful-js';
let sample = ['purple', 'green', 'red', 'brown']
console.log(
load(sample).sortBy({ against:'yellow' factor:'distance',order:'desc'})
)
// [ 'brown', 'red', 'green', 'purple' ]
Defined inβ
stats()β
stats(
options
?):Collection
|ColorArray
<C
,Options
>
Computes statistical values about the passed in color collection.
The topmost properties from each returned factor
object are:
against
- The color being used for comparison.
Required for the distance
and contrast
factors.
If relativeMean
is false
, other factors that take the comparison color token as an overload will have this property's value as null
.
-
colorspace
- The colorspace in which the factors were computed in. It has no effect on thecontrast
ordistance
factors (for now). -
extremums
- An array of the minimum and the maximum value (respectively) of thefactor
. -
colors
- An array of color tokens that have the minimum and maximumextremum
values respectively. -
mean
- The average value for thefactor
. -
displayable
- The percentage of the displayable or colors with channel ranges that can be rendered in that colorspace when converted to RGB.
The mean
property can be overloaded by the relativeMean
option:
- If
relativeMean
istrue
, theagainst
option will be used as a subtrahend for calculating the distance between eachextremum
. For example, it will mean "Get the largest/smallest distance betweenfactor
as comparedagainst
this color token otherwise just get the smallest/largestfactor
from thr passed in collection."
Parametersβ
β’ options?: StatsOptions
Optional parameters to specify how the data should be computed.
Returnsβ
Collection
| ColorArray
<C
, Options
>