RGJS6 Color module.
Methods
-
<static> contrast(color [, dark] [, light] [, threshold])
-
Get which of two colors provides the greatest contrast with another.
Parameters:
Name Type Argument Default Description colorobject | string A color object to compare against.
darkobject | string <optional>
'#000' optional - A designated dark color (defaults to black).
lightobject | string <optional>
'#fff' optional - A designated light color (defaults to white).
thresholdnumber <optional>
0.43 optional
-
<static> darken(color, factor [, relative])
-
Darken a color.
Convertscolorto hsl, then recalculateshsl.lightnesswithlightness *= 1.0 - factor;Parameters:
Name Type Argument Description colorobject | string An object with r, g and b values ({r:x, g:y, b:z}). Hexadecimal color values (#000000) are allowed.
factornumber The factor of darkening (0.0 - 1.0).
relativeboolean <optional>
Optional, set to
truefor the adjustment to be relative to the current value.Returns:
The resulting color. Returns RGB object or hex, depends on supplied argument.
- Type
- object | string
Example
rgjs.clr.darken('#999', 0.25) // darkens #999 by 25%, returns `#737373` -
<static> fade(color, factor)
-
Fade a color.
Parameters:
Name Type Description colorobject | string An object with r, g and b values ({r:x, g:y, b:z}). Hexadecimal color values (#000000) are allowed.
factornumber The factor of fading (0.0 - 1.0).
Returns:
The resulting color as a rgba css string (rgba(x,y,z,a)).
- Type
- string
Example
rgjs.clr.fade('#f00', 0.25); // 'rgba(255, 0, 0, 0.25)' -
<static> getAvgLuminance(color)
-
Get average luminance.
Parameters:
Name Type Description colorobject | string An object with r, g and b values ({r:x, g:y, b:z}). Hexadecimal color values (#000000) are allowed.
Returns:
A number indicating the average luminance (0-255).
- Type
- number
Example
rgjs.clr.getAvgLuminance('#333'); // ~ 50.999 -
<static> getClosest(color, colors)
-
Get closest color.
Parameters:
Name Type Description colorobject | string An object with r, g and b values ({r:x, g:y, b:z}). Hexadecimal color values (#000000) are allowed.
colorsarray An array of colors to compare the color to.
Returns:
The resulting color. Returns RGB object or hex, depends on supplied argument.
- Type
- object | string
Example
rgjs.clr.getClosest('#f00', ['#300', '#900']); // '#900' -
<static> getClrComplementary()
-
Get complemantary color.
ALias of getComplementary().- Deprecated:
-
- Yes
- See:
-
- rgjs/clr/getComplementary
-
<static> getComplementary(color)
-
Get complemantary color.
Parameters:
Name Type Description colorobject | string An object with r, g and b values ({r:x, g:y, b:z}). Hexadecimal color values (#000000) are allowed.
Returns:
The resulting color. Returns RGB object or hex, depends on supplied argument.
- Type
- object | string
Example
rgjs.clr.getComplementary('#f00'); // '#00ffff' rgjs.clr.getComplementary({r: 255, g: 0, b: 0}); // {r: 0, g: 255, b: 255} -
<static> getCssGradient()
-
Get a linear css gradient scrim.
ALias of getLinearGradientScrim().- Deprecated:
-
- Yes
- See:
-
- rgjs/clr/getLinearGradientScrim
-
<static> getLinearGradient(color [, color2] [, angle] [, fade1] [, fade2])
-
Get a linear css gradient between two colors.
Parameters:
Name Type Argument Default Description colorobject | string An object with r, g and b values ({r:x, g:y, b:z}). Hexadecimal color values (#000000) are allowed.
color2object | string <optional>
'transparent' An object with r, g and b values ({r:x, g:y, b:z}). Hexadecimal color values (#000000) are allowed.
angleNumber <optional>
0 Optional angle for the gradient.
fade1Number <optional>
1.0 Optional opacity multiplier for color1.
fade2Number <optional>
1.0 Optional opacity multiplier for color2.
Returns:
The resulting gradient as a css string.
- Type
- string
-
<static> getLinearGradientScrim(color [, opac] [, angle])
-
Get a linear css gradient scrim.
Parameters:
Name Type Argument Default Description colorobject | string [description]
opacNumber <optional>
1.0 [description]
angleNumber <optional>
0 [description]
Returns:
The resulting gradient as a css string.
- Type
- string
-
<static> hexToHsl(hex)
-
Hex to hsl.
Parameters:
Name Type Description hexobject Hexidecimal color value (#000000). Shorthand is allowed (#000).
Returns:
A hsl object ({hue:x, saturation:y, lightness:z}).
- Type
- object
Example
rgjs.clr.hexToHsl('#ff0000'); // {hue: 0, saturation: 1, lightness: 0.5} -
<static> hexToHsv(hex)
-
Hex to hsv.
Parameters:
Name Type Description hexstring Hexidecimal color value (#000000). Shorthand is allowed (#000).
Returns:
A hsv object ({hue:x, saturation:y, value:z}).
- Type
- object
Example
rgjs.clr.hexToHsv('#f00'); // {hue: 0, saturation: 100, value: 100} -
<static> hexToRgb(hex)
-
Hex to rgb.
Parameters:
Name Type Description hexstring Hexidecimal color value (#000000). Shorthand is allowed (#000).
Returns:
An object with r, g, and b values ({r:x, g:y, b:z}) or null on failure.
- Type
- object
Example
rgjs.clr.hexToRgb('#f00'); // {r: 255, g: 0, b: 0} -
<static> hslToHex(hsl)
-
Hsl to hex.
Parameters:
Name Type Description hslobject A hsl object ({hue:x, saturation:y, lightness:z}).
Returns:
Hexidecimal color value (#000000). Shorthand is allowed (#000).
- Type
- object
Example
rgjs.clr.hslToHex({hue: 0, saturation: 1, lightness: 0.5}); // '#ff0000' -
<static> hslToRgb(hsl)
-
Hsl to rgb.
Based on https://gist.github.com/mjackson/5311256Parameters:
Name Type Description hslobject A hsl object ({hue:x, saturation:y, lightness:z}).
Returns:
An object with r, g and b values ({r:x, g:y, b:z}).
- Type
- object
Example
rgjs.clr.hslToRgb({hue: 0, saturation: 1, lightness: 0.5}); // {r: 255, g: 0, b: 0} -
<static> hsvToHex(hsv)
-
Hsv to hex.
Parameters:
Name Type Description hsvobject A hsv object ({hue:x, saturation:y, value:z}).
Returns:
Hexadecimal color value (#000000).
- Type
- string
Example
rgjs.clr.hsvToHex({hue: 0, saturation: 100, value: 100}); // '#ff0000' -
<static> hsvToRgb(hsv)
-
Hsv to rgb.
Parameters:
Name Type Description hsvobject A hsv object ({hue:x, saturation:y, value:z}).
Returns:
An object with r, g and b values ({r:x, g:y, b:z}).
- Type
- object
Example
rgjs.clr.hsvToRgb({hue: 0, saturation: 100, value: 100}); // {r: 255, g: 0, b: 0} -
<static> hueShift(h, s)
-
Hue shift.
Parameters:
Name Type Description hnumber HSV hue value
snumber Shift
Returns:
Shifted hue
- Type
- number
Example
// Invert '#f00' by shifting the hue by 180 degrees const hex = '#f00'; const hsv = hexToHsv(hex); rgjs.clr.hueShift(hsv.hue); // 180
-
<static> lighten(color, factor [, relative])
-
Lighten a color.
Convertscolorto hsl, then recalculateshsl.lightnesswithlightness *= 1.0 + factor;Parameters:
Name Type Argument Description colorobject | string An object with r, g and b values ({r:x, g:y, b:z}). Hexadecimal color values (#000000) are allowed.
factornumber The factor of lighting (0.0 - 1.0).
relativeboolean <optional>
Optional, set to
truefor the adjustment to be relative to the current value.Returns:
The resulting color. Returns RGB object or hex, depends on supplied argument.
- Type
- object | string
Example
rgjs.clr.lighten('#999', 0.25) // lightens #999 by 25%, returns `#bfbfbf`. -
<static> rgbToHex(rgb)
-
Rgb to hex.
Parameters:
Name Type Description rgbfloat An object with r, g and b values ({r:x, g:y, b:z}).
Returns:
Hexadecimal color value (#000000).
- Type
- string
Example
rgjs.clr.hexToRgb({r: 255, g: 0, b: 0}); // '#f00' -
<static> rgbToHsl(rgb [, toFixed])
-
Rgb to hsl.
Based on https://gist.github.com/mjackson/5311256Parameters:
Name Type Argument Default Description rgbobject An object with r, g and b values ({r:x, g:y, b:z}).
toFixedboolean <optional>
true Limits results to 2 decimals. Defaults to TRUE.
Returns:
A hsl object ({hue:x, saturation:y, lightness:z}).
- Type
- object
Example
rgjs.clr.rgbToHsl({r: 255, g: 0, b: 0}); // {hue: 0, saturation: 1, lightness: 0.5} -
<static> rgbToHsv(rgb)
-
Rgb to Hsv.
Parameters:
Name Type Description rgbobject An object with r, g and b values ({r:x, g:y, b:z}).
Returns:
A hsv object ({hue:x, saturation:y, value:z}).
- Type
- object
Example
rgjs.clr.rgbToHsv({r: 255, g: 0, b: 0}); // {hue: 0, saturation: 100, value: 100}