Collection
Functionsβ
filterBy()β
filterBy(
collection
,options
):Collection
Filters a collection of colors using the specified factor
as the criterion.
- The
factor
is expected to be an array of strings with each element being a validfactor
. Every key is afactor
, with the filtered collection as the value in the result object.
To get all the factors in the result object pass undefined
to factor
.
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.
See the color spaces page for the expected ranges.
Supports expression strings e.g '>=0.5'
. The supported symbols are == | === | != | !== | >= | <= | < | >
Parametersβ
β’ collection: Collection
= []
The collection of colors to filter.
β’ options: FilterByOptions
= ...
Options to customise filtering behaviour.
Returnsβ
Exampleβ
import { filterBy } from 'huetiful-js'
let sample = [
'#00ffdc',
'#00ff78',
'#00c000',
'#007e00',
'#164100',
'#ffff00',
'#310000',
'#3e0000',
'#4e0000',
'#600000',
'#720000',
]
Defined inβ
sortBy()β
sortBy(
collection
,options
?):Collection
Sorts colors according to the specified factor
. The supported options are:
- The
factor
is expected to be an array of strings with each element being a validfactor
. Every key is afactor
, with the sorted collection as the value in the result object.
To get all the factors in the result object pass undefined
to factor
.
'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β
β’ collection: Collection
= []
The collection
of colors to sort.
β’ options?: SortByOptions
The optional overrides to customize the sorting behaviour.
Returnsβ
Exampleβ
import { sortBy } from 'huetiful-js'
let sample = ['purple', 'green', 'red', 'brown']
console.log(
sortBy(sample,{ against:'yellow' kind:['distance'],order:'desc',})
)
// [ 'brown', 'red', 'green', 'purple' ]
Defined inβ
stats()β
stats(
collection
,options
):Stats
Computes statistical values about the factors the passed in collection.
The properties from each returned factor
object are:
-
against
- The color being used for comparison. Required for thedistance
andcontrast
factors. IfrelativeMean
isfalse
, other factors that take the comparison color token as an overload will have this property's value asnull
. -
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
.
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."
These properties are available at the topmost level of the resultant object:
achromatic
- The amount of colors which are gray out of the total colors in the collection as a value in the range [0,1].colorspace
- The colorspace in which the values were computed from, expected to be hue based. Defaults tolch
if an invalid mode likergb
is used.
- The
factor
is expected to be an array of strings with each element being a validfactor
. Every key is afactor
, with the stats of thatfactor
as the value in the result object.
To get all the factors in the result object pass undefined
to factor
.
Parametersβ
β’ collection: Collection
= []
The collection to compute stats from. Any collection with color tokens as values will work.
β’ options: StatsOptions
= ...
The optional overrides to customize the computing behaviour for the factors.